MainContext.ts
106 lines
| 3.2 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r107 | import Memory = require("dojo/store/Memory"); | ||
| import Observable = require("dojo/store/Observable"); | ||||
|
|
r110 | import { Appointment, AppointmentRole, Member } from "./Appointment"; | ||
|
|
r107 | import { Contact } from "./Contact"; | ||
| import { Uuid } from "@implab/core-amd/Uuid"; | ||||
|
|
r110 | import { IDestroyable } from "@implab/core-amd/interfaces"; | ||
| import { delay } from "@implab/core-amd/safe"; | ||||
|
|
r116 | import { query } from "@implab/djx/store"; | ||
|
|
r107 | |||
|
|
r110 | type AppointmentRecord = Omit<Appointment, "getMembers"> & { id: string }; | ||
|
|
r107 | |||
| type ContactRecord = Contact; | ||||
| type MemberRecord = Member & { appointmentId: string; }; | ||||
|
|
r110 | const item = <T, T2>(map: (x: T) => T2) => <U extends { item: T }>({ item, ...props }: U) => ({ item: map(item), ...props }); | ||
|
|
r107 | |||
|
|
r110 | export class MainContext implements IDestroyable { | ||
|
|
r109 | private readonly _appointments = new Observable(new Memory<AppointmentRecord>()); | ||
|
|
r107 | |||
|
|
r109 | private readonly _contacts = new Observable(new Memory<ContactRecord>()); | ||
|
|
r107 | |||
|
|
r109 | private readonly _members = new Observable(new Memory<MemberRecord>()); | ||
|
|
r107 | |||
|
|
r110 | async createAppointment(title: string, startAt: Date, duration: number, members: Member[]) { | ||
| await delay(1000); | ||||
|
|
r107 | const id = Uuid(); | ||
| this._appointments.add({ | ||||
|
|
r110 | id, | ||
|
|
r107 | startAt, | ||
| duration, | ||||
| title | ||||
| }); | ||||
| members.forEach(member => | ||||
| this._members.add({ | ||||
| appointmentId: id, | ||||
| ...member | ||||
|
|
r110 | }, { id: Uuid() }) as void | ||
|
|
r107 | ); | ||
| } | ||||
|
|
r146 | async removeAppointment(appointmentId: string) { | ||
| await delay(10); | ||||
| this._members.query({ appointmentId }) | ||||
| .map(m => this._members.getIdentity(m)) | ||||
| .forEach(id => this._members.remove(id)); | ||||
| this._appointments.remove(appointmentId); | ||||
| } | ||||
|
|
r118 | async load() { | ||
|
|
r146 | await delay(10); | ||
| for (let i = 0; i < 5; i++) { | ||||
|
|
r118 | const id = Uuid(); | ||
| this._appointments.add({ | ||||
| id, | ||||
| startAt: new Date(), | ||||
| duration: 30, | ||||
|
|
r146 | title: `Hello ${i + 1}` | ||
|
|
r118 | }); | ||
|
|
r146 | |||
| for (let ii = 0; ii < 3; ii++) | ||||
| this._members.add({ | ||||
| appointmentId: id, | ||||
| email: "some@no.mail", | ||||
| name: `Peter ${ii}`, | ||||
| position: "Manager", | ||||
| role: "participant" | ||||
| }, { id: Uuid() }); | ||||
|
|
r118 | } | ||
| } | ||||
|
|
r116 | private readonly _queryAppointmentsRx = query(this._appointments); | ||
| private readonly _queryMembersRx = query(this._members); | ||||
|
|
r110 | queryAppointments({ dateFrom, dateTo }: { dateFrom?: Date; dateTo?: Date; } = {}) { | ||
|
|
r116 | return this._queryAppointmentsRx(({ startAt }) => | ||
|
|
r110 | (!dateFrom || dateFrom <= startAt) && | ||
| (!dateTo || startAt <= dateTo) | ||||
| ).map(item(this._mapAppointment)); | ||||
|
|
r107 | } | ||
|
|
r110 | async addMember(appointmentId: string, member: Member) { | ||
| await delay(1000); | ||||
| this._members.add({ | ||||
| appointmentId, | ||||
| ...member | ||||
| }); | ||||
| } | ||||
|
|
r107 | |||
|
|
r110 | private readonly _mapAppointment = ({ startAt, title, duration, id }: AppointmentRecord) => ({ | ||
| id, | ||||
| title, | ||||
| startAt, | ||||
| duration, | ||||
|
|
r116 | getMembers: (role?: AppointmentRole) => this._queryMembersRx(role ? { appointmentId: id, role } : { appointmentId: id }) | ||
|
|
r109 | }); | ||
|
|
r107 | |||
|
|
r110 | destroy() { | ||
| } | ||||
|
|
r107 | } | ||
