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