diff --git a/apps/mobile/packages/features/client/home/lib/src/presentation/widgets/shift_order_form_sheet.dart b/apps/mobile/packages/features/client/home/lib/src/presentation/widgets/shift_order_form_sheet.dart index 4fef193c..666a4971 100644 --- a/apps/mobile/packages/features/client/home/lib/src/presentation/widgets/shift_order_form_sheet.dart +++ b/apps/mobile/packages/features/client/home/lib/src/presentation/widgets/shift_order_form_sheet.dart @@ -60,6 +60,8 @@ class _ShiftOrderFormSheetState extends State { List<_VendorOption> _vendors = const <_VendorOption>[]; List<_RoleOption> _roles = const <_RoleOption>[]; String? _selectedVendorId; + List _hubs = const []; + dc.ListTeamHubsByOwnerIdTeamHubs? _selectedHub; bool _showSuccess = false; Map? _submitData; @@ -99,6 +101,7 @@ class _ShiftOrderFormSheetState extends State { ]; _loadVendors(); + _loadHubs(); _loadOrderDetails(); } @@ -190,6 +193,10 @@ class _ShiftOrderFormSheetState extends State { 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 { .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 { 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 { } } + Future _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 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 []; + _selectedHub = null; + }); + } + } + Future _loadRolesForVendor(String vendorId) async { try { final fdc.QueryResult @@ -366,10 +409,13 @@ class _ShiftOrderFormSheetState extends State { 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 { } } + Future _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 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 []; + _selectedHub = null; + }); + } + } + String _formatTimeForField(fdc.Timestamp? value) { if (value == null) return ''; try { @@ -570,8 +680,8 @@ class _ShiftOrderFormSheetState extends State { _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 { ); } - Widget _buildLocationField() { + Widget _buildHubField() { return Container( padding: const EdgeInsets.symmetric(horizontal: UiConstants.space3), decoration: BoxDecoration( @@ -811,21 +921,33 @@ class _ShiftOrderFormSheetState extends State { borderRadius: UiConstants.radiusMd, border: Border.all(color: UiColors.border), ), - child: Row( - children: [ - 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( + 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( + value: hub, + child: Text( + hub.hubName, + style: UiTypography.body2r.textPrimary, + ), + ); + }).toList(), + ), ), ); }