reorder with hub

This commit is contained in:
José Salazar
2026-01-29 16:22:00 -05:00
parent 7b57009586
commit 9e8c7db3f9

View File

@@ -60,6 +60,8 @@ class _ShiftOrderFormSheetState extends State<ShiftOrderFormSheet> {
List<_VendorOption> _vendors = const <_VendorOption>[];
List<_RoleOption> _roles = const <_RoleOption>[];
String? _selectedVendorId;
List<dc.ListTeamHubsByOwnerIdTeamHubs> _hubs = const <dc.ListTeamHubsByOwnerIdTeamHubs>[];
dc.ListTeamHubsByOwnerIdTeamHubs? _selectedHub;
bool _showSuccess = false;
Map<String, dynamic>? _submitData;
@@ -99,6 +101,7 @@ class _ShiftOrderFormSheetState extends State<ShiftOrderFormSheet> {
];
_loadVendors();
_loadHubs();
_loadOrderDetails();
}
@@ -190,6 +193,10 @@ class _ShiftOrderFormSheetState extends State<ShiftOrderFormSheet> {
if (businessId == null || businessId.isEmpty) {
return;
}
final dc.ListTeamHubsByOwnerIdTeamHubs? selectedHub = _selectedHub;
if (selectedHub == null) {
return;
}
final DateTime date = DateTime.parse(_dateController.text);
final DateTime dateOnly = DateTime.utc(date.year, date.month, date.day);
@@ -202,9 +209,9 @@ class _ShiftOrderFormSheetState extends State<ShiftOrderFormSheet> {
.createOrder(
businessId: businessId,
orderType: orderType,
teamHubId: selectedHub.id,
)
.vendorId(_selectedVendorId)
.location(_globalLocationController.text)
.status(dc.OrderStatus.POSTED)
.date(orderTimestamp)
.execute();
@@ -226,8 +233,15 @@ class _ShiftOrderFormSheetState extends State<ShiftOrderFormSheet> {
shiftResult = await _dataConnect
.createShift(title: shiftTitle, orderId: orderId)
.date(orderTimestamp)
.location(_globalLocationController.text)
.locationAddress(_globalLocationController.text)
.location(selectedHub.hubName)
.locationAddress(selectedHub.address)
.latitude(selectedHub.latitude)
.longitude(selectedHub.longitude)
.placeId(selectedHub.placeId)
.city(selectedHub.city)
.state(selectedHub.state)
.street(selectedHub.street)
.country(selectedHub.country)
.status(dc.ShiftStatus.PENDING)
.workersNeeded(workersNeeded)
.filled(0)
@@ -315,6 +329,35 @@ class _ShiftOrderFormSheetState extends State<ShiftOrderFormSheet> {
}
}
Future<void> _loadHubs() async {
final String? businessId = dc.ClientSessionStore.instance.session?.business?.id;
if (businessId == null || businessId.isEmpty) {
return;
}
try {
final fdc.QueryResult<
dc.ListTeamHubsByOwnerIdData,
dc.ListTeamHubsByOwnerIdVariables> result =
await _dataConnect.listTeamHubsByOwnerId(ownerId: businessId).execute();
final List<dc.ListTeamHubsByOwnerIdTeamHubs> hubs = result.data.teamHubs;
if (!mounted) return;
setState(() {
_hubs = hubs;
_selectedHub = hubs.isNotEmpty ? hubs.first : null;
if (_selectedHub != null) {
_globalLocationController.text = _selectedHub!.address;
}
});
} catch (_) {
if (!mounted) return;
setState(() {
_hubs = const <dc.ListTeamHubsByOwnerIdTeamHubs>[];
_selectedHub = null;
});
}
}
Future<void> _loadRolesForVendor(String vendorId) async {
try {
final fdc.QueryResult<dc.ListRolesByVendorIdData, dc.ListRolesByVendorIdVariables>
@@ -366,10 +409,13 @@ class _ShiftOrderFormSheetState extends State<ShiftOrderFormSheet> {
final dc.ListShiftRolesByBusinessAndOrderShiftRolesShift firstShift =
shiftRoles.first.shift;
_globalLocationController.text = firstShift.order.location ??
firstShift.locationAddress ??
firstShift.location ??
_globalLocationController.text;
final dc.ListShiftRolesByBusinessAndOrderShiftRolesShiftOrderTeamHub?
teamHub = firstShift.order.teamHub;
await _loadHubsAndSelect(
placeId: teamHub?.placeId,
hubName: teamHub?.hubName,
address: teamHub?.address,
);
final String? vendorId = firstShift.order.vendorId;
if (mounted) {
@@ -403,6 +449,70 @@ class _ShiftOrderFormSheetState extends State<ShiftOrderFormSheet> {
}
}
Future<void> _loadHubsAndSelect({
String? placeId,
String? hubName,
String? address,
}) async {
final String? businessId = dc.ClientSessionStore.instance.session?.business?.id;
if (businessId == null || businessId.isEmpty) {
return;
}
try {
final fdc.QueryResult<
dc.ListTeamHubsByOwnerIdData,
dc.ListTeamHubsByOwnerIdVariables> result =
await _dataConnect.listTeamHubsByOwnerId(ownerId: businessId).execute();
final List<dc.ListTeamHubsByOwnerIdTeamHubs> hubs = result.data.teamHubs;
dc.ListTeamHubsByOwnerIdTeamHubs? selected;
if (placeId != null && placeId.isNotEmpty) {
for (final dc.ListTeamHubsByOwnerIdTeamHubs hub in hubs) {
if (hub.placeId == placeId) {
selected = hub;
break;
}
}
}
if (selected == null && hubName != null && hubName.isNotEmpty) {
for (final dc.ListTeamHubsByOwnerIdTeamHubs hub in hubs) {
if (hub.hubName == hubName) {
selected = hub;
break;
}
}
}
if (selected == null && address != null && address.isNotEmpty) {
for (final dc.ListTeamHubsByOwnerIdTeamHubs hub in hubs) {
if (hub.address == address) {
selected = hub;
break;
}
}
}
selected ??= hubs.isNotEmpty ? hubs.first : null;
if (!mounted) return;
setState(() {
_hubs = hubs;
_selectedHub = selected;
if (selected != null) {
_globalLocationController.text = selected.address;
}
});
} catch (_) {
if (!mounted) return;
setState(() {
_hubs = const <dc.ListTeamHubsByOwnerIdTeamHubs>[];
_selectedHub = null;
});
}
}
String _formatTimeForField(fdc.Timestamp? value) {
if (value == null) return '';
try {
@@ -570,8 +680,8 @@ class _ShiftOrderFormSheetState extends State<ShiftOrderFormSheet> {
_buildDateField(),
const SizedBox(height: UiConstants.space4),
_buildSectionHeader('LOCATION'),
_buildLocationField(),
_buildSectionHeader('HUB'),
_buildHubField(),
const SizedBox(height: UiConstants.space5),
Row(
@@ -803,7 +913,7 @@ class _ShiftOrderFormSheetState extends State<ShiftOrderFormSheet> {
);
}
Widget _buildLocationField() {
Widget _buildHubField() {
return Container(
padding: const EdgeInsets.symmetric(horizontal: UiConstants.space3),
decoration: BoxDecoration(
@@ -811,21 +921,33 @@ class _ShiftOrderFormSheetState extends State<ShiftOrderFormSheet> {
borderRadius: UiConstants.radiusMd,
border: Border.all(color: UiColors.border),
),
child: Row(
children: <Widget>[
const Icon(UiIcons.mapPin, size: 20, color: UiColors.iconSecondary),
const SizedBox(width: UiConstants.space2),
Expanded(
child: TextField(
controller: _globalLocationController,
decoration: const InputDecoration(
hintText: 'Enter location address',
border: InputBorder.none,
),
style: UiTypography.body2r.textPrimary,
),
child: DropdownButtonHideUnderline(
child: DropdownButton<dc.ListTeamHubsByOwnerIdTeamHubs>(
isExpanded: true,
value: _selectedHub,
icon: const Icon(
UiIcons.chevronDown,
size: 18,
color: UiColors.iconSecondary,
),
],
onChanged: (dc.ListTeamHubsByOwnerIdTeamHubs? hub) {
if (hub != null) {
setState(() {
_selectedHub = hub;
_globalLocationController.text = hub.address;
});
}
},
items: _hubs.map((dc.ListTeamHubsByOwnerIdTeamHubs hub) {
return DropdownMenuItem<dc.ListTeamHubsByOwnerIdTeamHubs>(
value: hub,
child: Text(
hub.hubName,
style: UiTypography.body2r.textPrimary,
),
);
}).toList(),
),
),
);
}