updateSlot method
- Slot slot
Updates the given slot
.
Implementation
Future<void> updateSlot(Slot slot) async {
if (!state.hasData) {
log('Cannot update slot: No data');
return;
}
log('Updating slot with id ${slot.id}');
try {
final oldSlot = state.requireData.firstWhere((s) => s.id == slot.id);
await _datasource.updateSlot(
token: _auth.state.requireData.pick(Webservice.lb_planner_api),
slot: slot,
);
log('Updated slot with id ${slot.id}');
await captureEvent(
'slot_updated',
properties: {
'weekday': slot.weekday,
'size': slot.size,
'start': slot.startUnit,
'end': slot.endUnit,
},
);
final supervisorDiff = calculateListDiff(oldSlot.supervisors, slot.supervisors, detectMoves: false);
final mappingDiff = calculateListDiff(oldSlot.mappings, slot.mappings, detectMoves: false);
for (final diff in supervisorDiff.getUpdatesWithData()) {
await diff.when(
insert: (index, data) async {
log('New supervisor $data added to slot ${slot.id}');
await addSlotSupervisor(slotId: slot.id, supervisorId: data);
},
remove: (index, data) async {
log('Supervisor $data removed from slot ${slot.id}');
await removeSlotSupervisor(slotId: slot.id, supervisorId: data);
},
change: (index, oldData, newData) async {
log('Supervisor $oldData changed to $newData in slot ${slot.id}');
await removeSlotSupervisor(slotId: slot.id, supervisorId: oldData);
await addSlotSupervisor(slotId: slot.id, supervisorId: newData);
},
move: (from, to, data) {},
);
}
for (final diff in mappingDiff.getUpdatesWithData()) {
await diff.when(
insert: (index, data) async {
log('New mapping $data added to slot ${slot.id}');
await addMapping(slotId: slot.id, mapping: data);
},
remove: (index, data) async {
log('Mapping $data removed from slot ${slot.id}');
await deleteMapping(slotId: slot.id, mappingId: data.id);
},
change: (index, oldData, newData) async {
log('Mapping $oldData changed to $newData in slot ${slot.id}');
await deleteMapping(slotId: slot.id, mappingId: oldData.id);
await addMapping(slotId: slot.id, mapping: newData);
},
move: (from, to, data) {},
);
}
await build(this);
} catch (e, s) {
log('Failed to update slot', e, s);
}
}