|
|
import Memory = require("dojo/store/Memory");
|
|
|
import Observable = require("dojo/store/Observable");
|
|
|
import { Appointment, AppointmentRole, Member } from "./Appointment";
|
|
|
import { Contact } from "./Contact";
|
|
|
import { Uuid } from "@implab/core-amd/Uuid";
|
|
|
import { IDestroyable } from "@implab/core-amd/interfaces";
|
|
|
import { delay } from "@implab/core-amd/safe";
|
|
|
import { query } from "@implab/djx/store";
|
|
|
|
|
|
type AppointmentRecord = Omit<Appointment, "getMembers"> & { id: string };
|
|
|
|
|
|
type ContactRecord = Contact;
|
|
|
|
|
|
type MemberRecord = Member & { appointmentId: string; };
|
|
|
|
|
|
const item = <T, T2>(map: (x: T) => T2) => <U extends { item: T }>({ item, ...props }: U) => ({ item: map(item), ...props });
|
|
|
|
|
|
|
|
|
export class MainContext implements IDestroyable {
|
|
|
private readonly _appointments = new Observable(new Memory<AppointmentRecord>());
|
|
|
|
|
|
private readonly _contacts = new Observable(new Memory<ContactRecord>());
|
|
|
|
|
|
private readonly _members = new Observable(new Memory<MemberRecord>());
|
|
|
|
|
|
async createAppointment(title: string, startAt: Date, duration: number, members: Member[]) {
|
|
|
await delay(1000);
|
|
|
const id = Uuid();
|
|
|
this._appointments.add({
|
|
|
id,
|
|
|
startAt,
|
|
|
duration,
|
|
|
title
|
|
|
});
|
|
|
|
|
|
members.forEach(member =>
|
|
|
this._members.add({
|
|
|
appointmentId: id,
|
|
|
...member
|
|
|
}, { id: Uuid() }) as void
|
|
|
);
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
async load() {
|
|
|
await delay(10);
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
const id = Uuid();
|
|
|
this._appointments.add({
|
|
|
id,
|
|
|
startAt: new Date(),
|
|
|
duration: 30,
|
|
|
title: `Hello ${i + 1}`
|
|
|
});
|
|
|
|
|
|
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() });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private readonly _queryAppointmentsRx = query(this._appointments);
|
|
|
|
|
|
private readonly _queryMembersRx = query(this._members);
|
|
|
|
|
|
queryAppointments({ dateFrom, dateTo }: { dateFrom?: Date; dateTo?: Date; } = {}) {
|
|
|
return this._queryAppointmentsRx(({ startAt }) =>
|
|
|
(!dateFrom || dateFrom <= startAt) &&
|
|
|
(!dateTo || startAt <= dateTo)
|
|
|
).map(item(this._mapAppointment));
|
|
|
}
|
|
|
|
|
|
async addMember(appointmentId: string, member: Member) {
|
|
|
await delay(1000);
|
|
|
this._members.add({
|
|
|
appointmentId,
|
|
|
...member
|
|
|
});
|
|
|
}
|
|
|
|
|
|
private readonly _mapAppointment = ({ startAt, title, duration, id }: AppointmentRecord) => ({
|
|
|
id,
|
|
|
title,
|
|
|
startAt,
|
|
|
duration,
|
|
|
getMembers: (role?: AppointmentRole) => this._queryMembersRx(role ? { appointmentId: id, role } : { appointmentId: id })
|
|
|
});
|
|
|
|
|
|
destroy() {
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|