Add editing of client schedule entities
Add utility methods to more easily edit the fields of a single entity in the schedule, along with a modification flag and is modified utility to check for changes having been made.
This commit is contained in:
parent
faffe48706
commit
61734d4152
2 changed files with 192 additions and 84 deletions
|
@ -424,6 +424,7 @@ export class ClientSchedule extends ClientEntity {
|
|||
originalEvents: Map<Id, ClientScheduleEvent>;
|
||||
originalRoles: Map<Id, ClientScheduleRole>;
|
||||
originalShifts: Map<Id, ClientScheduleShift>;
|
||||
modified: boolean;
|
||||
|
||||
constructor(
|
||||
id: 111,
|
||||
|
@ -439,12 +440,33 @@ export class ClientSchedule extends ClientEntity {
|
|||
this.originalEvents = new Map(events);
|
||||
this.originalRoles = new Map(roles);
|
||||
this.originalShifts = new Map(shifts);
|
||||
this.modified = false;
|
||||
}
|
||||
|
||||
equals(other: ClientSchedule): boolean {
|
||||
throw new Error("ClientSchedule.equals not implemented")
|
||||
}
|
||||
|
||||
private recalcModified() {
|
||||
function mapEquals<K, V>(a: Map<K, V>, b: Map<K, V>) {
|
||||
if (a.size !== b.size) {
|
||||
return false;
|
||||
}
|
||||
for (const [key, value] of a) {
|
||||
if (!b.has(key) || b.get(key) !== value) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
this.modified = (
|
||||
!mapEquals(this.locations, this.originalLocations)
|
||||
|| !mapEquals(this.events, this.originalEvents)
|
||||
|| !mapEquals(this.roles, this.originalRoles)
|
||||
|| !mapEquals(this.shifts, this.originalShifts)
|
||||
);
|
||||
}
|
||||
|
||||
private fixLocationRefs(locations: Map<Id, ClientScheduleLocation>) {
|
||||
for (const events of [this.events, this.originalEvents]) {
|
||||
for (const event of events.values()) {
|
||||
|
@ -472,6 +494,10 @@ export class ClientSchedule extends ClientEntity {
|
|||
}
|
||||
}
|
||||
|
||||
isModifiedLocation(id: Id) {
|
||||
return this.originalLocations.get(id) !== this.locations.get(id);
|
||||
}
|
||||
|
||||
setLocation(location: ClientScheduleLocation) {
|
||||
if (location.deleted) {
|
||||
this.checkLocationRefsForDeletion(location.id);
|
||||
|
@ -480,6 +506,22 @@ export class ClientSchedule extends ClientEntity {
|
|||
if (!location.deleted) {
|
||||
this.fixLocationRefs(new Map([[location.id, location]]));
|
||||
}
|
||||
this.modified = true;
|
||||
}
|
||||
|
||||
editLocation(
|
||||
location: ClientScheduleLocation,
|
||||
edits: {
|
||||
deleted?: boolean,
|
||||
name?: string,
|
||||
description?: string
|
||||
},
|
||||
) {
|
||||
const copy = location.clone();
|
||||
if (edits.deleted !== undefined) copy.deleted = edits.deleted;
|
||||
if (edits.name !== undefined) copy.name = edits.name;
|
||||
if (edits.description !== undefined) copy.description = edits.description;
|
||||
this.setLocation(copy);
|
||||
}
|
||||
|
||||
restoreLocation(id: Id) {
|
||||
|
@ -491,10 +533,31 @@ export class ClientSchedule extends ClientEntity {
|
|||
this.checkLocationRefsForDeletion(id);
|
||||
this.locations.delete(id);
|
||||
}
|
||||
this.recalcModified();
|
||||
}
|
||||
|
||||
isModifiedEvent(id: Id) {
|
||||
return this.originalEvents.get(id) !== this.events.get(id);
|
||||
}
|
||||
|
||||
setEvent(event: ClientScheduleEvent) {
|
||||
this.events.set(event.id, event);
|
||||
this.modified = true;
|
||||
}
|
||||
|
||||
editEvent(
|
||||
event: ClientScheduleEvent,
|
||||
edits: {
|
||||
deleted?: boolean,
|
||||
name?: string,
|
||||
description?: string
|
||||
},
|
||||
) {
|
||||
const copy = event.clone();
|
||||
if (edits.deleted !== undefined) copy.deleted = edits.deleted;
|
||||
if (edits.name !== undefined) copy.name = edits.name;
|
||||
if (edits.description !== undefined) copy.description = edits.description;
|
||||
this.setEvent(copy);
|
||||
}
|
||||
|
||||
restoreEvent(id: Id) {
|
||||
|
@ -526,6 +589,10 @@ export class ClientSchedule extends ClientEntity {
|
|||
}
|
||||
}
|
||||
|
||||
isModifiedRole(id: Id) {
|
||||
return this.originalRoles.get(id) !== this.roles.get(id);
|
||||
}
|
||||
|
||||
setRole(role: ClientScheduleRole) {
|
||||
if (role.deleted) {
|
||||
this.checkRoleRefsForDeletion(role.id);
|
||||
|
@ -534,6 +601,22 @@ export class ClientSchedule extends ClientEntity {
|
|||
if (!role.deleted) {
|
||||
this.fixRoleRefs(new Map([[role.id, role]]));
|
||||
}
|
||||
this.modified = true;
|
||||
}
|
||||
|
||||
editRole(
|
||||
role: ClientScheduleRole,
|
||||
edits: {
|
||||
deleted?: boolean,
|
||||
name?: string,
|
||||
description?: string
|
||||
},
|
||||
) {
|
||||
const copy = role.clone();
|
||||
if (edits.deleted !== undefined) copy.deleted = edits.deleted;
|
||||
if (edits.name !== undefined) copy.name = edits.name;
|
||||
if (edits.description !== undefined) copy.description = edits.description;
|
||||
this.setRole(copy);
|
||||
}
|
||||
|
||||
restoreRole(id: Id) {
|
||||
|
@ -545,10 +628,31 @@ export class ClientSchedule extends ClientEntity {
|
|||
this.checkRoleRefsForDeletion(id);
|
||||
this.roles.delete(id);
|
||||
}
|
||||
this.recalcModified();
|
||||
}
|
||||
|
||||
isModifiedShift(id: Id) {
|
||||
return this.originalShifts.get(id) !== this.shifts.get(id);
|
||||
}
|
||||
|
||||
setShift(shift: ClientScheduleShift) {
|
||||
this.shifts.set(shift.id, shift);
|
||||
this.modified = true;
|
||||
}
|
||||
|
||||
editShift(
|
||||
shift: ClientScheduleShift,
|
||||
edits: {
|
||||
deleted?: boolean,
|
||||
name?: string,
|
||||
description?: string
|
||||
},
|
||||
) {
|
||||
const copy = shift.clone();
|
||||
if (edits.deleted !== undefined) copy.deleted = edits.deleted;
|
||||
if (edits.name !== undefined) copy.name = edits.name;
|
||||
if (edits.description !== undefined) copy.description = edits.description;
|
||||
this.setShift(copy);
|
||||
}
|
||||
|
||||
restoreShift(id: Id) {
|
||||
|
@ -558,6 +662,7 @@ export class ClientSchedule extends ClientEntity {
|
|||
} else {
|
||||
this.shifts.delete(id);
|
||||
}
|
||||
this.recalcModified();
|
||||
}
|
||||
|
||||
static fromApi(api: Living<ApiSchedule>, opts: { zone: Zone, locale: string }) {
|
||||
|
@ -680,5 +785,6 @@ export class ClientSchedule extends ClientEntity {
|
|||
this.originalShifts,
|
||||
this.shifts,
|
||||
);
|
||||
this.recalcModified();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue