@sprucelabs/spruce-calendar-components 21.0.3 → 21.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/build/.spruce/errors/errors.types.d.ts +129 -0
  2. package/build/.spruce/errors/errors.types.js +4 -0
  3. package/build/.spruce/errors/options.types.d.ts +28 -0
  4. package/build/.spruce/errors/options.types.js +2 -0
  5. package/build/constants.d.ts +5 -0
  6. package/build/constants.js +9 -0
  7. package/build/errors/SpruceError.d.ts +5 -0
  8. package/build/errors/SpruceError.js +45 -0
  9. package/build/esm/.spruce/errors/errors.types.d.ts +129 -0
  10. package/build/esm/.spruce/errors/errors.types.js +3 -0
  11. package/build/esm/.spruce/errors/options.types.d.ts +28 -0
  12. package/build/esm/.spruce/errors/options.types.js +1 -0
  13. package/build/esm/constants.d.ts +5 -0
  14. package/build/esm/constants.js +6 -0
  15. package/build/esm/errors/SpruceError.d.ts +5 -0
  16. package/build/esm/errors/SpruceError.js +39 -0
  17. package/build/esm/index-module.d.ts +30 -0
  18. package/build/esm/index-module.js +7 -0
  19. package/build/esm/skillViewControllers/Root.svc.d.ts +73 -0
  20. package/build/esm/skillViewControllers/Root.svc.js +404 -0
  21. package/build/esm/toolBelt/CalendarToolTestFactory.d.ts +5 -0
  22. package/build/esm/toolBelt/CalendarToolTestFactory.js +28 -0
  23. package/build/esm/toolBelt/states/PrerequisitesToolBeltState.d.ts +37 -0
  24. package/build/esm/toolBelt/states/PrerequisitesToolBeltState.js +190 -0
  25. package/build/esm/toolBelt/states/RootToolBeltState.d.ts +37 -0
  26. package/build/esm/toolBelt/states/RootToolBeltState.js +122 -0
  27. package/build/esm/toolBelt/states/makeEventTyped.d.ts +2 -0
  28. package/build/esm/toolBelt/states/makeEventTyped.js +8 -0
  29. package/build/esm/types/calendar.types.d.ts +2 -0
  30. package/build/esm/utilities/CalendarEventManager.d.ts +73 -0
  31. package/build/esm/utilities/CalendarEventManager.js +331 -0
  32. package/build/esm/utilities/CalendarPeopleManager.d.ts +56 -0
  33. package/build/esm/utilities/CalendarPeopleManager.js +182 -0
  34. package/build/esm/utilities/calendarShiftGenerator.d.ts +8 -0
  35. package/build/esm/utilities/calendarShiftGenerator.js +24 -0
  36. package/build/esm/viewControllers/CalendarSelectTool.vc.d.ts +28 -0
  37. package/build/esm/viewControllers/CalendarSelectTool.vc.js +103 -0
  38. package/build/esm/viewControllers/DateSelectCard.vc.d.ts +29 -0
  39. package/build/esm/viewControllers/DateSelectCard.vc.js +77 -0
  40. package/build/esm/viewControllers/PersonSelectTool.vc.d.ts +36 -0
  41. package/build/esm/viewControllers/PersonSelectTool.vc.js +156 -0
  42. package/build/esm/viewControllers/SelectUpdateRepeatingStrategyCard.vc.d.ts +18 -0
  43. package/build/esm/viewControllers/SelectUpdateRepeatingStrategyCard.vc.js +85 -0
  44. package/build/index-module.d.ts +30 -0
  45. package/build/index-module.js +32 -0
  46. package/build/skillViewControllers/Root.svc.d.ts +73 -0
  47. package/build/skillViewControllers/Root.svc.js +359 -0
  48. package/build/toolBelt/CalendarToolTestFactory.d.ts +5 -0
  49. package/build/toolBelt/CalendarToolTestFactory.js +34 -0
  50. package/build/toolBelt/states/PrerequisitesToolBeltState.d.ts +37 -0
  51. package/build/toolBelt/states/PrerequisitesToolBeltState.js +172 -0
  52. package/build/toolBelt/states/RootToolBeltState.d.ts +37 -0
  53. package/build/toolBelt/states/RootToolBeltState.js +113 -0
  54. package/build/toolBelt/states/makeEventTyped.d.ts +2 -0
  55. package/build/toolBelt/states/makeEventTyped.js +14 -0
  56. package/build/types/calendar.types.d.ts +2 -0
  57. package/build/utilities/CalendarEventManager.d.ts +73 -0
  58. package/build/utilities/CalendarEventManager.js +289 -0
  59. package/build/utilities/CalendarPeopleManager.d.ts +56 -0
  60. package/build/utilities/CalendarPeopleManager.js +163 -0
  61. package/build/utilities/calendarShiftGenerator.d.ts +8 -0
  62. package/build/utilities/calendarShiftGenerator.js +26 -0
  63. package/build/viewControllers/CalendarSelectTool.vc.d.ts +28 -0
  64. package/build/viewControllers/CalendarSelectTool.vc.js +93 -0
  65. package/build/viewControllers/DateSelectCard.vc.d.ts +29 -0
  66. package/build/viewControllers/DateSelectCard.vc.js +69 -0
  67. package/build/viewControllers/PersonSelectTool.vc.d.ts +36 -0
  68. package/build/viewControllers/PersonSelectTool.vc.js +140 -0
  69. package/build/viewControllers/SelectUpdateRepeatingStrategyCard.vc.d.ts +18 -0
  70. package/build/viewControllers/SelectUpdateRepeatingStrategyCard.vc.js +88 -0
  71. package/package.json +99 -30
@@ -0,0 +1,37 @@
1
+ import { SpruceSchemas, ToolBeltState } from '@sprucelabs/heartwood-view-controllers';
2
+ import { CalendarToolBeltStateMachine } from '../../types/calendar.types';
3
+ import { CalendarEventManager } from '../../utilities/CalendarEventManager';
4
+ import CalendarPeopleManager from '../../utilities/CalendarPeopleManager';
5
+ import CalendarSelectCardViewController from '../../viewControllers/CalendarSelectTool.vc';
6
+ import DateSelectCardViewController from '../../viewControllers/DateSelectCard.vc';
7
+ import PersonSelectToolViewController from '../../viewControllers/PersonSelectTool.vc';
8
+ export declare class RootToolBeltState implements ToolBeltState {
9
+ readonly id = "root";
10
+ private calendarSelectCardVc?;
11
+ private toolBeltVc;
12
+ private personSelectVc;
13
+ private sm;
14
+ private scopeSelectionTool?;
15
+ private dateSelectVc;
16
+ private lastSelectedDate?;
17
+ private people;
18
+ private events;
19
+ constructor(options: {
20
+ people: CalendarPeopleManager;
21
+ events: CalendarEventManager;
22
+ });
23
+ getCalendarSelectCardVc(): CalendarSelectCardViewController | undefined;
24
+ private CalendarSelectVc;
25
+ load(stateMachine: CalendarToolBeltStateMachine): Promise<void>;
26
+ private optionallyClearStickyTool;
27
+ private handleSelectDate;
28
+ private resetToolBelt;
29
+ private optionallyDropInScopeSelectionTool;
30
+ private setupCalendarSelectVc;
31
+ private optionallyAddPeopleCard;
32
+ getPersonSelectCardVc(): PersonSelectToolViewController;
33
+ getDateSelectVc(): DateSelectCardViewController;
34
+ getSelectedDate(): SpruceSchemas.HeartwoodViewControllers.v2021_02_11.CalendarSelectedDate;
35
+ setSelectedDate(year: number, month: number, day: number): void;
36
+ private PersonSelectVc;
37
+ }
@@ -0,0 +1,122 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { dateUtil } from '@sprucelabs/calendar-utils';
11
+ import { assertOptions } from '@sprucelabs/schema';
12
+ export class RootToolBeltState {
13
+ constructor(options) {
14
+ this.id = 'root';
15
+ const { people, events } = assertOptions(options, ['people', 'events']);
16
+ this.people = people;
17
+ this.events = events;
18
+ }
19
+ getCalendarSelectCardVc() {
20
+ return this.calendarSelectCardVc;
21
+ }
22
+ CalendarSelectVc() {
23
+ var _a;
24
+ return (_a = this.sm) === null || _a === void 0 ? void 0 : _a.Controller('calendar.calendar-select-tool', {
25
+ events: this.events,
26
+ });
27
+ }
28
+ load(stateMachine) {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ this.sm = stateMachine;
31
+ this.toolBeltVc = stateMachine.getToolBeltVc();
32
+ this.resetToolBelt();
33
+ this.dateSelectVc = this.sm.Controller('calendar.date-select-card', {
34
+ onSelectDate: (date) => this.handleSelectDate(date),
35
+ startDate: dateUtil.getStartOfDay(new Date().getTime()),
36
+ selectedDate: this.lastSelectedDate,
37
+ });
38
+ this.toolBeltVc.addTool({
39
+ id: 'date-selection',
40
+ lineIcon: 'calendar',
41
+ card: this.dateSelectVc.render(),
42
+ });
43
+ yield Promise.all([
44
+ this.setupCalendarSelectVc(stateMachine),
45
+ this.optionallyAddPeopleCard(stateMachine.getContext().scope),
46
+ ]);
47
+ this.optionallyDropInScopeSelectionTool();
48
+ this.optionallyClearStickyTool();
49
+ });
50
+ }
51
+ optionallyClearStickyTool() {
52
+ if (this.toolBeltVc.getStickyTools().bottom) {
53
+ this.toolBeltVc.removeStickyTool('bottom');
54
+ }
55
+ }
56
+ handleSelectDate(date) {
57
+ const { calendarVc } = this.sm.getContext();
58
+ calendarVc.setStartDate(date);
59
+ }
60
+ resetToolBelt() {
61
+ var _a;
62
+ if (!this.scopeSelectionTool) {
63
+ this.scopeSelectionTool = (_a = this.toolBeltVc) === null || _a === void 0 ? void 0 : _a.getTool('scope-selection');
64
+ }
65
+ this.toolBeltVc.clearTools();
66
+ }
67
+ optionallyDropInScopeSelectionTool() {
68
+ var _a;
69
+ if (this.scopeSelectionTool) {
70
+ (_a = this.toolBeltVc) === null || _a === void 0 ? void 0 : _a.addTool(this.scopeSelectionTool);
71
+ }
72
+ }
73
+ setupCalendarSelectVc(stateMachine) {
74
+ var _a, _b, _c;
75
+ this.calendarSelectCardVc = this.CalendarSelectVc();
76
+ (_a = this.toolBeltVc) === null || _a === void 0 ? void 0 : _a.addTool({
77
+ lineIcon: 'calendar',
78
+ id: 'calendars',
79
+ card: (_b = this.calendarSelectCardVc) === null || _b === void 0 ? void 0 : _b.render(),
80
+ });
81
+ return (_c = this.calendarSelectCardVc) === null || _c === void 0 ? void 0 : _c.load(stateMachine.getContext());
82
+ }
83
+ optionallyAddPeopleCard(scope) {
84
+ var _a, _b;
85
+ return __awaiter(this, void 0, void 0, function* () {
86
+ const location = yield scope.getCurrentLocation();
87
+ if (location && !((_a = this.toolBeltVc) === null || _a === void 0 ? void 0 : _a.getTool('people'))) {
88
+ this.personSelectVc = this.PersonSelectVc();
89
+ (_b = this.toolBeltVc) === null || _b === void 0 ? void 0 : _b.addTool({
90
+ lineIcon: 'user',
91
+ id: 'people',
92
+ card: this.personSelectVc.render(),
93
+ });
94
+ yield this.personSelectVc.load();
95
+ }
96
+ });
97
+ }
98
+ getPersonSelectCardVc() {
99
+ return this.personSelectVc;
100
+ }
101
+ getDateSelectVc() {
102
+ return this.dateSelectVc;
103
+ }
104
+ getSelectedDate() {
105
+ return this.dateSelectVc.getSelectedDate();
106
+ }
107
+ setSelectedDate(year, month, day) {
108
+ var _a;
109
+ this.lastSelectedDate = {
110
+ year,
111
+ month,
112
+ day,
113
+ };
114
+ (_a = this.dateSelectVc) === null || _a === void 0 ? void 0 : _a.setSelectedDate(year, month, day);
115
+ }
116
+ PersonSelectVc() {
117
+ var _a;
118
+ return (_a = this.sm) === null || _a === void 0 ? void 0 : _a.Controller('calendar.person-select-tool', {
119
+ people: this.people,
120
+ });
121
+ }
122
+ }
@@ -0,0 +1,2 @@
1
+ import { SpruceSchemas } from '@sprucelabs/heartwood-view-controllers';
2
+ export default function makeEventTyped(event: SpruceSchemas.HeartwoodViewControllers.v2021_02_11.CalendarEvent, type: SpruceSchemas.Calendar.v2021_05_19.CalendarEventType): SpruceSchemas.HeartwoodViewControllers.v2021_02_11.CalendarEvent;
@@ -0,0 +1,8 @@
1
+ import draftEventGenerator from '../../utilities/draftGenerator.js';
2
+ export default function makeEventTyped(event, type) {
3
+ var _a;
4
+ const { timeBlocks } = event;
5
+ timeBlocks[0].title = draftEventGenerator.generateTitle(type.name);
6
+ timeBlocks[0].leftIcons = [{ icon: (_a = type.lineIcon) !== null && _a !== void 0 ? _a : 'coffee', hint: null }];
7
+ return event;
8
+ }
@@ -1,6 +1,7 @@
1
1
  import { AbstractViewController, LineIcon, SkillViewControllerLoadOptions, ToolBeltState, ToolBeltStateMachine } from '@sprucelabs/heartwood-view-controllers';
2
2
  import { SpruceSchemas } from '@sprucelabs/mercury-types';
3
3
  import { EventTarget } from '@sprucelabs/spruce-event-utils';
4
+ import { UpdateRepeatingStrategy } from '../constants';
4
5
  import { CalendarEventManager } from '../utilities/CalendarEventManager';
5
6
  import CalendarPeopleManager from '../utilities/CalendarPeopleManager';
6
7
  import CalendarViewController from '../viewControllers/Calendar.vc';
@@ -49,6 +50,7 @@ export declare type GetCalendarToolBeltContextHandler = () => CalendarToolBeltCo
49
50
  export declare type GetPersonFromEventHandler = () => Promise<Person | null>;
50
51
  export declare type GetHasPendingContextChangesHandler = () => boolean;
51
52
  export declare type CancelEventHandler = (eventId: string) => boolean | Promise<boolean | void> | void;
53
+ export declare type UpdateRepeatingStrategyWithCancel = UpdateRepeatingStrategy | 'cancel';
52
54
  export interface CalendarToolOptions {
53
55
  updateContext: UpdateCalendarToolBeltContextHandler;
54
56
  getContext: GetCalendarToolBeltContextHandler;
@@ -0,0 +1,73 @@
1
+ import { DateUtils } from '@sprucelabs/calendar-utils';
2
+ import { CalendarEvent } from '@sprucelabs/heartwood-view-controllers';
3
+ import { RemoteViewControllerFactory } from '@sprucelabs/spruce-heartwood-utils';
4
+ import RemoteEventStore from '../stores/RemoteEventStore';
5
+ import RemotePreferencesStore from '../stores/RemotePreferencesStore';
6
+ import { CalendarToolBeltStateMachine, UpdateEvent, UpdateRepeatingStrategyWithCancel } from '../types/calendar.types';
7
+ import CalendarViewController from '../viewControllers/Calendar.vc';
8
+ export declare class CalendarEventManager {
9
+ private calendarVc;
10
+ protected allEvents: CalendarEvent[];
11
+ private inclusiveCalendarIds;
12
+ protected events: RemoteEventStore;
13
+ private eventTypes?;
14
+ private remoteVc;
15
+ private hasVcForEventTypeBeenLoaded;
16
+ private sm;
17
+ private shouldIgnoreNextContextUpdate;
18
+ private askForUpdateStrategy;
19
+ private dateToUpdate?;
20
+ private shouldUpdateAllEventsGoingForward?;
21
+ private dates;
22
+ protected prefs: RemotePreferencesStore;
23
+ private calendars;
24
+ private shouldUpdateContextOnNextSave;
25
+ protected get calendarIds(): string[];
26
+ protected get visibleCalendarIds(): string[];
27
+ constructor(options: CalendarEventManagerOptions);
28
+ replaceEventsInRange(events: CalendarEvent[], startMs: number, endMs: number): void;
29
+ private refreshShifts;
30
+ private isCalendarSelected;
31
+ private doesCalenderExist;
32
+ getAllEvents(): CalendarEvent[];
33
+ getCalendars(): import("@sprucelabs/calendar-utils").SpruceSchemas.Calendar.v2021_05_19.Calendar[];
34
+ addDraftEvent(event: CalendarEvent): Promise<void>;
35
+ silentlySwapEvent(oldId: string, event: CalendarEvent): Promise<void>;
36
+ handleDropEvent(id: string, updates: Partial<CalendarEvent>): Promise<boolean>;
37
+ reset(): Promise<void>;
38
+ restoreEventToDraftOnStateLoadError(): Promise<void>;
39
+ getVisibleCalendarIds(): string[];
40
+ removeEvent(id: string): Promise<void>;
41
+ makeCalendarVisible(calendarId: string): Promise<void>;
42
+ makeCalendarHidden(calendarId: string): Promise<void>;
43
+ updateEvent(id: string, updates: Partial<CalendarEvent> & {
44
+ dateToUpdate?: number;
45
+ shouldUpdateAllEventsGoingForward?: boolean;
46
+ }): void;
47
+ loadEvents(startDate: number, endDate: number, peopleIds: string[]): Promise<void>;
48
+ setCalendarVisibility(calendarId: string, shouldBeVisible: boolean): Promise<void>;
49
+ setupVcForEventType(vcId: string, typeSlug: string): Promise<void>;
50
+ private assertValidCalendarId;
51
+ private updateEventInContext;
52
+ private setEventInContext;
53
+ selectEvent(eventId: string): Promise<void>;
54
+ load(): Promise<void>;
55
+ getEvent(id: string): CalendarEvent;
56
+ private loadCalendars;
57
+ private handleWillUpdateContext;
58
+ private handleDidUpdateContext;
59
+ private saveEvent;
60
+ optionallyAskForUpdateRepeatingStrategy(event: UpdateEvent): Promise<boolean>;
61
+ }
62
+ export declare type EventManagerCalendarVc = Pick<CalendarViewController, 'replaceEventsInRange' | 'mixinEvents' | 'removeEvent' | 'addEvent' | 'hasEvent' | 'updateEvent' | 'setShifts' | 'getShifts' | 'getEvent' | 'getPeople' | 'setControllerForEventType' | 'setRemoteStore' | 'getSelectedEvent' | 'selectEvent'>;
63
+ declare type EventManagerRemoteViewControllerFactory = Pick<RemoteViewControllerFactory, 'fetchRemoteController'>;
64
+ export interface CalendarEventManagerOptions {
65
+ calendarVc: EventManagerCalendarVc;
66
+ events: RemoteEventStore;
67
+ remoteVc: EventManagerRemoteViewControllerFactory;
68
+ sm: CalendarToolBeltStateMachine;
69
+ dates: DateUtils;
70
+ preferences: RemotePreferencesStore;
71
+ askForUpdateStrategy: (event: UpdateEvent) => Promise<UpdateRepeatingStrategyWithCancel>;
72
+ }
73
+ export {};
@@ -0,0 +1,331 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { calendarEventSchema, } from '@sprucelabs/heartwood-view-controllers';
11
+ import { assertOptions, normalizeSchemaValues } from '@sprucelabs/schema';
12
+ import SpruceError from '../errors/SpruceError.js';
13
+ import calendarShiftGenerator from './calendarShiftGenerator.js';
14
+ import draftEventGenerator from './draftGenerator.js';
15
+ export class CalendarEventManager {
16
+ constructor(options) {
17
+ this.allEvents = [];
18
+ this.inclusiveCalendarIds = [];
19
+ this.hasVcForEventTypeBeenLoaded = {};
20
+ this.shouldIgnoreNextContextUpdate = false;
21
+ this.calendars = [];
22
+ this.shouldUpdateContextOnNextSave = true;
23
+ const { calendarVc, events, remoteVc, sm, askForUpdateStrategy, dates, preferences, } = assertOptions(options, [
24
+ 'calendarVc',
25
+ 'events',
26
+ 'remoteVc',
27
+ 'sm',
28
+ 'askForUpdateStrategy',
29
+ 'dates',
30
+ 'preferences',
31
+ ]);
32
+ this.calendarVc = calendarVc;
33
+ this.events = events;
34
+ this.remoteVc = remoteVc;
35
+ this.sm = sm;
36
+ this.prefs = preferences;
37
+ this.dates = dates;
38
+ this.askForUpdateStrategy = askForUpdateStrategy;
39
+ this.calendarVc.setRemoteStore(this.events);
40
+ }
41
+ get calendarIds() {
42
+ return this.calendars.map((c) => c.id);
43
+ }
44
+ get visibleCalendarIds() {
45
+ var _a;
46
+ return (_a = this.prefs.getVisibleCalendarIds()) !== null && _a !== void 0 ? _a : this.calendars.map((c) => c.id);
47
+ }
48
+ replaceEventsInRange(events, startMs, endMs) {
49
+ this.calendarVc.replaceEventsInRange(events.filter((e) => this.isCalendarSelected(e.calendarId)), startMs, endMs);
50
+ this.allEvents = this.allEvents.filter((e) => events.findIndex((event) => event.id === e.id) === -1 &&
51
+ !(e.startDateTimeMs >= startMs && e.startDateTimeMs <= endMs));
52
+ this.allEvents.push(...events);
53
+ this.refreshShifts();
54
+ }
55
+ refreshShifts() {
56
+ const inclusiveEvents = this.allEvents.filter((e) => this.inclusiveCalendarIds.indexOf(e.calendarId) > -1);
57
+ const shifts = calendarShiftGenerator.generateFromEvents(inclusiveEvents);
58
+ this.calendarVc.setShifts([...shifts]);
59
+ }
60
+ isCalendarSelected(calendarId) {
61
+ return this.visibleCalendarIds.indexOf(calendarId) > -1;
62
+ }
63
+ doesCalenderExist(calendarId) {
64
+ return this.calendars.findIndex((c) => c.id === calendarId) > -1;
65
+ }
66
+ getAllEvents() {
67
+ return this.allEvents;
68
+ }
69
+ getCalendars() {
70
+ return this.calendars;
71
+ }
72
+ addDraftEvent(event) {
73
+ return __awaiter(this, void 0, void 0, function* () {
74
+ this.calendarVc.selectEvent(event.id);
75
+ this.allEvents.push(event);
76
+ yield this.updateEventInContext(event);
77
+ });
78
+ }
79
+ silentlySwapEvent(oldId, event) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ const events = this.getAllEvents().filter((e) => e.id !== oldId);
82
+ this.allEvents = [...events, event];
83
+ yield this.updateEventInContext(event);
84
+ });
85
+ }
86
+ handleDropEvent(id, updates) {
87
+ return __awaiter(this, void 0, void 0, function* () {
88
+ const event = Object.assign(Object.assign({}, this.calendarVc.getEvent(id)), updates);
89
+ const pass = yield this.optionallyAskForUpdateRepeatingStrategy(event);
90
+ if (pass) {
91
+ yield this.saveEvent(event);
92
+ }
93
+ return pass;
94
+ });
95
+ }
96
+ reset() {
97
+ var _a;
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ void this.setEventInContext(undefined);
100
+ (_a = this.events) === null || _a === void 0 ? void 0 : _a.clearCalendarId();
101
+ });
102
+ }
103
+ restoreEventToDraftOnStateLoadError() {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ const updated = yield this.updateEventInContext({
106
+ timeBlocks: draftEventGenerator.generateTimeBlocks(),
107
+ calendarId: undefined,
108
+ eventTypeSlug: undefined,
109
+ });
110
+ this.updateEvent(updated.id, updated);
111
+ });
112
+ }
113
+ getVisibleCalendarIds() {
114
+ return this.visibleCalendarIds;
115
+ }
116
+ removeEvent(id) {
117
+ var _a;
118
+ return __awaiter(this, void 0, void 0, function* () {
119
+ this.allEvents = this.allEvents.filter((e) => e.id !== id);
120
+ const event = this.calendarVc.getEvent(id);
121
+ this.calendarVc.removeEvent(id);
122
+ try {
123
+ yield this.events.cancelEvent(event.id);
124
+ }
125
+ catch (err) {
126
+ console.error((_a = err.stack) !== null && _a !== void 0 ? _a : err.message);
127
+ this.calendarVc.addEvent(Object.assign(Object.assign({}, event), { error: err }));
128
+ }
129
+ });
130
+ }
131
+ makeCalendarVisible(calendarId) {
132
+ return __awaiter(this, void 0, void 0, function* () {
133
+ this.assertValidCalendarId(calendarId);
134
+ this.calendarVc.mixinEvents(this.allEvents.filter((e) => e.calendarId === calendarId));
135
+ const visibleCalendarIds = [...this.visibleCalendarIds];
136
+ visibleCalendarIds.push(calendarId);
137
+ yield this.prefs.setVisibleCalendarIds(visibleCalendarIds);
138
+ });
139
+ }
140
+ makeCalendarHidden(calendarId) {
141
+ return __awaiter(this, void 0, void 0, function* () {
142
+ this.assertValidCalendarId(calendarId);
143
+ const visibleCalendarIds = this.visibleCalendarIds.filter((id) => id !== calendarId);
144
+ for (const event of this.allEvents) {
145
+ if (event.calendarId === calendarId &&
146
+ this.calendarVc.hasEvent(event.id)) {
147
+ this.calendarVc.removeEvent(event.id);
148
+ }
149
+ }
150
+ yield this.prefs.setVisibleCalendarIds(visibleCalendarIds);
151
+ });
152
+ }
153
+ updateEvent(id, updates) {
154
+ var _a;
155
+ this.calendarVc.updateEvent(id, updates);
156
+ const idx = this.allEvents.findIndex((e) => e.id === id);
157
+ this.allEvents[idx] = Object.assign(Object.assign({}, this.allEvents[idx]), updates);
158
+ this.refreshShifts();
159
+ if (this.shouldUpdateContextOnNextSave &&
160
+ ((_a = this.sm.getContext().event) === null || _a === void 0 ? void 0 : _a.id) === id) {
161
+ this.shouldIgnoreNextContextUpdate = true;
162
+ void this.updateEventInContext(updates);
163
+ }
164
+ this.shouldUpdateContextOnNextSave = true;
165
+ }
166
+ loadEvents(startDate, endDate, peopleIds) {
167
+ return __awaiter(this, void 0, void 0, function* () {
168
+ const events = yield this.events.getSchedules({
169
+ calendarIds: this.calendarIds,
170
+ startDate,
171
+ endDate,
172
+ peopleIds,
173
+ });
174
+ this.replaceEventsInRange(events, startDate, endDate);
175
+ if (!this.eventTypes) {
176
+ this.eventTypes = yield this.events.loadEventTypes();
177
+ }
178
+ yield Promise.all(events.map((e) => __awaiter(this, void 0, void 0, function* () {
179
+ var _a;
180
+ const type = (_a = this.eventTypes) === null || _a === void 0 ? void 0 : _a.find((t) => t.slug === e.eventTypeSlug);
181
+ if (type === null || type === void 0 ? void 0 : type.viewControllerId) {
182
+ yield this.setupVcForEventType(type.viewControllerId, type.slug);
183
+ }
184
+ })));
185
+ });
186
+ }
187
+ setCalendarVisibility(calendarId, shouldBeVisible) {
188
+ return __awaiter(this, void 0, void 0, function* () {
189
+ if (shouldBeVisible) {
190
+ yield this.makeCalendarVisible(calendarId);
191
+ }
192
+ else {
193
+ yield this.makeCalendarHidden(calendarId);
194
+ }
195
+ });
196
+ }
197
+ setupVcForEventType(vcId, typeSlug) {
198
+ return __awaiter(this, void 0, void 0, function* () {
199
+ if (this.hasVcForEventTypeBeenLoaded[vcId]) {
200
+ return;
201
+ }
202
+ this.hasVcForEventTypeBeenLoaded[vcId] = true;
203
+ yield this.remoteVc.fetchRemoteController(vcId);
204
+ this.calendarVc.setControllerForEventType(typeSlug, vcId);
205
+ });
206
+ }
207
+ assertValidCalendarId(calendarId) {
208
+ if (!this.doesCalenderExist(calendarId)) {
209
+ throw new SpruceError({
210
+ code: 'CALENDAR_NOT_FOUND',
211
+ calendarIds: [calendarId],
212
+ });
213
+ }
214
+ }
215
+ updateEventInContext(updates) {
216
+ return __awaiter(this, void 0, void 0, function* () {
217
+ const { event } = this.sm.getContext();
218
+ const updated = Object.assign(Object.assign(Object.assign({}, event), updates), { isBusy: false });
219
+ yield this.setEventInContext(updated);
220
+ return updated;
221
+ });
222
+ }
223
+ setEventInContext(newEvent, options) {
224
+ return __awaiter(this, void 0, void 0, function* () {
225
+ this.shouldIgnoreNextContextUpdate =
226
+ (options === null || options === void 0 ? void 0 : options.shouldHandleDidUpdateContext) !== false;
227
+ yield this.sm.updateContext({
228
+ event: newEvent,
229
+ });
230
+ });
231
+ }
232
+ selectEvent(eventId) {
233
+ return __awaiter(this, void 0, void 0, function* () {
234
+ const lastSelected = this.calendarVc.getSelectedEvent();
235
+ const event = this.calendarVc.getEvent(eventId);
236
+ this.calendarVc.selectEvent(event.id);
237
+ if ((lastSelected === null || lastSelected === void 0 ? void 0 : lastSelected.id) !== event.id) {
238
+ yield this.setEventInContext(event);
239
+ }
240
+ });
241
+ }
242
+ load() {
243
+ return __awaiter(this, void 0, void 0, function* () {
244
+ yield this.sm.on('will-update-context', this.handleWillUpdateContext.bind(this));
245
+ yield this.sm.on('did-update-context', this.handleDidUpdateContext.bind(this));
246
+ yield this.loadCalendars();
247
+ this.inclusiveCalendarIds = this.calendars
248
+ .filter((c) => c.availableTimeSlotBehavior === 'include')
249
+ .map((c) => c.id);
250
+ });
251
+ }
252
+ getEvent(id) {
253
+ const event = this.allEvents.find((e) => e.id === id);
254
+ if (!event) {
255
+ throw new SpruceError({
256
+ code: 'EVENT_NOT_FOUND',
257
+ id,
258
+ });
259
+ }
260
+ return event;
261
+ }
262
+ loadCalendars() {
263
+ return __awaiter(this, void 0, void 0, function* () {
264
+ this.calendars = yield this.events.loadCalendars();
265
+ });
266
+ }
267
+ handleWillUpdateContext(options) {
268
+ return __awaiter(this, void 0, void 0, function* () {
269
+ const { event: originalEvent } = options.current;
270
+ const { event } = options.updates;
271
+ let pass = true;
272
+ if ((event === null || event === void 0 ? void 0 : event.id) &&
273
+ (originalEvent === null || originalEvent === void 0 ? void 0 : originalEvent.id) === (event === null || event === void 0 ? void 0 : event.id) &&
274
+ !this.shouldIgnoreNextContextUpdate) {
275
+ pass = yield this.optionallyAskForUpdateRepeatingStrategy(event);
276
+ }
277
+ return {
278
+ shouldAllowUpdates: pass,
279
+ };
280
+ });
281
+ }
282
+ handleDidUpdateContext() {
283
+ return __awaiter(this, void 0, void 0, function* () {
284
+ if (this.shouldIgnoreNextContextUpdate) {
285
+ this.shouldIgnoreNextContextUpdate = false;
286
+ return;
287
+ }
288
+ const { event } = this.sm.getContext();
289
+ if (event === null || event === void 0 ? void 0 : event.id) {
290
+ this.shouldUpdateContextOnNextSave = false;
291
+ yield this.saveEvent(event);
292
+ }
293
+ });
294
+ }
295
+ saveEvent(event) {
296
+ return __awaiter(this, void 0, void 0, function* () {
297
+ try {
298
+ const cleaned = normalizeSchemaValues(calendarEventSchema, Object.assign({}, event), {});
299
+ this.updateEvent(event.id, Object.assign(Object.assign({}, cleaned), { dateToUpdate: this.dateToUpdate, shouldUpdateAllEventsGoingForward: this.shouldUpdateAllEventsGoingForward }));
300
+ }
301
+ catch (err) {
302
+ console.error('Updating event in context from root failed because:\n\n', err);
303
+ }
304
+ });
305
+ }
306
+ optionallyAskForUpdateRepeatingStrategy(event) {
307
+ var _a;
308
+ return __awaiter(this, void 0, void 0, function* () {
309
+ const cleaned = normalizeSchemaValues(calendarEventSchema, event, {});
310
+ if (((_a = cleaned.totalInRepeating) !== null && _a !== void 0 ? _a : 0) > 1) {
311
+ let strategy = yield this.askForUpdateStrategy(cleaned);
312
+ if (strategy === 'cancel') {
313
+ return false;
314
+ }
315
+ else if (strategy === 'only-this-one') {
316
+ this.dateToUpdate = this.dates.getStartOfDay(event.startDateTimeMs);
317
+ this.shouldUpdateAllEventsGoingForward = false;
318
+ }
319
+ else if (strategy === 'all-going-forward') {
320
+ this.dateToUpdate = this.dates.getStartOfDay(event.startDateTimeMs);
321
+ this.shouldUpdateAllEventsGoingForward = true;
322
+ }
323
+ else {
324
+ this.dateToUpdate = undefined;
325
+ this.shouldUpdateAllEventsGoingForward = false;
326
+ }
327
+ }
328
+ return true;
329
+ });
330
+ }
331
+ }
@@ -0,0 +1,56 @@
1
+ import { MercuryClient } from '@sprucelabs/mercury-client';
2
+ import { AbstractEventEmitter } from '@sprucelabs/mercury-event-emitter';
3
+ import { CalendarPerson } from '../skillViewControllers/Root.svc';
4
+ import RemotePreferencesStore from '../stores/RemotePreferencesStore';
5
+ import { CalendarEvent } from '../types/calendar.types';
6
+ import CalendarViewController from '../viewControllers/Calendar.vc';
7
+ import { PersonMode } from '../viewControllers/PersonSelectTool.vc';
8
+ export default class CalendarPeopleManager extends AbstractEventEmitter<PeopleContract> {
9
+ private connectToApi;
10
+ private prefs;
11
+ private client;
12
+ private loggedInPerson?;
13
+ private locationId;
14
+ private organizationId;
15
+ private team;
16
+ private getVisibleEvents;
17
+ constructor(options: PeopleManagerOptions);
18
+ load(options: {
19
+ loggedInPerson?: CalendarPerson | null;
20
+ locationId: string;
21
+ organizationId: string;
22
+ }): Promise<void>;
23
+ private loadTeam;
24
+ getVisiblePeople(): CalendarPerson[];
25
+ private teammateById;
26
+ getTeam(): import("@sprucelabs/mercury-types").SpruceSchemas.Spruce.v2020_07_22.Person[];
27
+ getLoggedInPerson(): import("@sprucelabs/mercury-types").SpruceSchemas.HeartwoodViewControllers.v2021_02_11.CalendarPerson | undefined;
28
+ protected getVisiblePeopleIds(): string[];
29
+ private getSavedVisiblePeopleIds;
30
+ getVisibilityMode(): PersonMode;
31
+ setVisibilityMode(mode: PersonMode): Promise<void>;
32
+ private setVisibilityToMe;
33
+ private setVisibleToWorking;
34
+ private getWorkingPeopleIds;
35
+ private getWorkingPeople;
36
+ protected setVisiblePeopleIds(visible: string[], options?: SetVisiblePeolpeOptions): Promise<void>;
37
+ setPersonVisibility(id: string, shouldBeVisible: boolean): Promise<void>;
38
+ private emitDidUpdate;
39
+ }
40
+ export interface PeopleManagerOptions {
41
+ connectToApi: () => Promise<MercuryClient>;
42
+ preferences: RemotePreferencesStore;
43
+ calendarVc: PeopleManagerCalendarVc;
44
+ getVisibleEvents: () => CalendarEvent[];
45
+ }
46
+ export declare type PeopleManagerCalendarVc = Pick<CalendarViewController, 'addPerson' | 'getPeople' | 'removePerson' | 'setPeople'>;
47
+ declare const peopleContract: {
48
+ eventSignatures: {
49
+ 'did-update': {};
50
+ };
51
+ };
52
+ declare type PeopleContract = typeof peopleContract;
53
+ export interface SetVisiblePeolpeOptions {
54
+ shouldForceEmitDidChange?: boolean;
55
+ }
56
+ export {};