@servicetitan/standalone-root 1.2.0 → 1.4.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 (38) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/components/standalone-root-provider/standalone-root-provider.d.ts +4 -2
  3. package/dist/components/standalone-root-provider/standalone-root-provider.d.ts.map +1 -1
  4. package/dist/components/standalone-root-provider/standalone-root-provider.js +9 -12
  5. package/dist/components/standalone-root-provider/standalone-root-provider.js.map +1 -1
  6. package/dist/enums/index.d.ts +2 -0
  7. package/dist/enums/index.d.ts.map +1 -0
  8. package/dist/enums/index.js +2 -0
  9. package/dist/enums/index.js.map +1 -0
  10. package/dist/enums/root-section.d.ts +4 -0
  11. package/dist/enums/root-section.d.ts.map +1 -0
  12. package/dist/enums/root-section.js +5 -0
  13. package/dist/enums/root-section.js.map +1 -0
  14. package/dist/index.d.ts +1 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +1 -0
  17. package/dist/index.js.map +1 -1
  18. package/dist/stores/standalone-root.store.d.ts +14 -6
  19. package/dist/stores/standalone-root.store.d.ts.map +1 -1
  20. package/dist/stores/standalone-root.store.js +63 -10
  21. package/dist/stores/standalone-root.store.js.map +1 -1
  22. package/dist/utils/symbols.d.ts +1 -1
  23. package/dist/utils/symbols.d.ts.map +1 -1
  24. package/dist/utils/symbols.js +2 -1
  25. package/dist/utils/symbols.js.map +1 -1
  26. package/dist/utils/tokens.d.ts +3 -0
  27. package/dist/utils/tokens.d.ts.map +1 -0
  28. package/dist/utils/tokens.js +3 -0
  29. package/dist/utils/tokens.js.map +1 -0
  30. package/package.json +2 -2
  31. package/src/components/standalone-root-provider/standalone-root-provider.tsx +13 -15
  32. package/src/enums/index.ts +1 -0
  33. package/src/enums/root-section.ts +3 -0
  34. package/src/index.ts +1 -0
  35. package/src/stores/standalone-root.store.ts +50 -14
  36. package/src/utils/symbols.ts +2 -1
  37. package/src/utils/tokens.ts +7 -0
  38. package/tsconfig.tsbuildinfo +1 -1
@@ -6,7 +6,7 @@ import {
6
6
  PartitionsClientApi,
7
7
  UserDetails,
8
8
  } from '@servicetitan/marketing-services-api/identity';
9
- import { LoadStatus } from '@servicetitan/standalone-ui';
9
+ import { LoadStatus, SymbolTokenDataType } from '@servicetitan/standalone-ui';
10
10
  import { inject, injectable } from '@servicetitan/react-ioc';
11
11
  import {
12
12
  AppConfigApi,
@@ -26,8 +26,10 @@ import {
26
26
  storageKeyForAppSettings,
27
27
  storageKeyForTenantConfigs,
28
28
  } from '../utils/storage-keys';
29
+ import { ModifiableRootSection } from '../enums';
30
+ import { POSSIBLE_CHANGES_TOKEN } from '../utils/tokens';
29
31
  import { cultureKeyInCoreConfigs, defaultLocale, defaultTimezone } from '../utils/constants';
30
- import { currentInstanceSymbol, eventEmitterSymbol, isInitializedSymbol } from '../utils/symbols';
32
+ import { eventEmitterSymbol, isInitializedSymbol, registeredChangesSymbol } from '../utils/symbols';
31
33
 
32
34
  @injectable()
33
35
  export class StandaloneRootStore {
@@ -42,19 +44,22 @@ export class StandaloneRootStore {
42
44
  @observable locale = defaultLocale;
43
45
 
44
46
  private eventEmitter!: EventEmitter;
47
+ private currentInstanceSymbol = Symbol();
45
48
 
46
49
  constructor(
47
50
  @inject(TenantManagementApi) private readonly tenantManagementApi: TenantManagementApi,
48
51
  @inject(AppConfigApi) private readonly appConfigApi: AppConfigApi,
49
52
  @inject(CurrentUserClientApi) private readonly currentUserClientApi: CurrentUserClientApi,
50
53
  @inject(OnboardingApi) private readonly onboardingApi: OnboardingApi,
51
- @inject(PartitionsClientApi) private partitionsApi: PartitionsClientApi,
54
+ @inject(PartitionsClientApi) private readonly partitionsApi: PartitionsClientApi,
55
+ @inject(POSSIBLE_CHANGES_TOKEN)
56
+ private readonly possibleChanges: SymbolTokenDataType<typeof POSSIBLE_CHANGES_TOKEN>,
52
57
  ) {
53
58
  this.initializeEventEmitter();
54
59
  makeObservable(this);
55
60
  }
56
61
 
57
- initialize = async (forceRefresh: boolean, refreshOnboarding: boolean) => {
62
+ initialize = async (forceRefresh: boolean) => {
58
63
  try {
59
64
  runInAction(() => {
60
65
  this.loadStatus = LoadStatus.Loading;
@@ -95,8 +100,12 @@ export class StandaloneRootStore {
95
100
  const isSuccess = this.syncFromStorage();
96
101
 
97
102
  if (isSuccess) {
98
- if (refreshOnboarding) {
99
- await this.fetchOnboarding();
103
+ if (this.registeredChanges?.length) {
104
+ await Promise.all(
105
+ this.registeredChanges.map(section =>
106
+ this.mapModifiableSectionToFetcher[section](),
107
+ ),
108
+ );
100
109
  }
101
110
  } else {
102
111
  await fetchAll();
@@ -107,11 +116,13 @@ export class StandaloneRootStore {
107
116
  this.setIsInitialized(true);
108
117
  }
109
118
 
119
+ this.removeRegisteredChanges();
120
+
110
121
  const timezoneFetched = this.tenantConfigs.TimeZone;
111
122
 
112
123
  if (timezoneFetched) {
113
124
  runInAction(() => {
114
- this.timezone = this.tenantConfigs.TimeZone;
125
+ this.timezone = timezoneFetched;
115
126
  });
116
127
  }
117
128
 
@@ -146,7 +157,22 @@ export class StandaloneRootStore {
146
157
  };
147
158
  }
148
159
 
149
- unregisterGlobalSyncListener = () => {
160
+ dispose = () => {
161
+ this.registerPossibleChanges();
162
+ this.unregisterGlobalSyncListener();
163
+ };
164
+
165
+ private registerPossibleChanges = () => {
166
+ if (this.possibleChanges?.length) {
167
+ (window as any)[registeredChangesSymbol] = this.possibleChanges;
168
+ }
169
+ };
170
+
171
+ private removeRegisteredChanges = () => {
172
+ (window as any)[registeredChangesSymbol] = undefined;
173
+ };
174
+
175
+ private unregisterGlobalSyncListener = () => {
150
176
  this.eventEmitter.off(Events.SyncFromStorage, this.listenGlobalSync);
151
177
  };
152
178
 
@@ -166,13 +192,13 @@ export class StandaloneRootStore {
166
192
  };
167
193
 
168
194
  private listenGlobalSync = (instanceSymbol: symbol) => {
169
- if (instanceSymbol !== currentInstanceSymbol) {
195
+ if (instanceSymbol !== this.currentInstanceSymbol) {
170
196
  this.syncFromStorage();
171
197
  }
172
198
  };
173
199
 
174
200
  private emitGlobalSync = () => {
175
- this.eventEmitter.emit(Events.SyncFromStorage, currentInstanceSymbol);
201
+ this.eventEmitter.emit(Events.SyncFromStorage, this.currentInstanceSymbol);
176
202
  };
177
203
 
178
204
  private syncFromStorage = () => {
@@ -225,10 +251,6 @@ export class StandaloneRootStore {
225
251
  (window as any)[isInitializedSymbol] = value;
226
252
  };
227
253
 
228
- private get isInitialized() {
229
- return !!(window as any)[isInitializedSymbol];
230
- }
231
-
232
254
  private fetchTenant = async () => {
233
255
  const tenantFetched = await this.tenantManagementApi.client.tenant(undefined);
234
256
 
@@ -295,4 +317,18 @@ export class StandaloneRootStore {
295
317
  this.partitions = partitionsFetched;
296
318
  });
297
319
  };
320
+
321
+ private get isInitialized() {
322
+ return !!(window as any)[isInitializedSymbol];
323
+ }
324
+
325
+ private get registeredChanges(): typeof this.possibleChanges {
326
+ return (window as any)[registeredChangesSymbol];
327
+ }
328
+
329
+ private get mapModifiableSectionToFetcher(): Record<ModifiableRootSection, () => void> {
330
+ return {
331
+ [ModifiableRootSection.Onboarding]: this.fetchOnboarding,
332
+ };
333
+ }
298
334
  }
@@ -1,6 +1,7 @@
1
1
  const rootSymbolKey = 'standalone-root-symbol-key';
2
2
  const eventEmitterSymbolKey = 'standalone-root-event-emitter-symbol-key';
3
+ const registeredChangesSymbolKey = 'standalone-root-modifiable-changes-symbol-key';
3
4
 
4
5
  export const isInitializedSymbol = Symbol.for(rootSymbolKey);
5
6
  export const eventEmitterSymbol = Symbol.for(eventEmitterSymbolKey);
6
- export const currentInstanceSymbol = Symbol();
7
+ export const registeredChangesSymbol = Symbol.for(registeredChangesSymbolKey);
@@ -0,0 +1,7 @@
1
+ import { symbolToken } from '@servicetitan/react-ioc';
2
+ import { ModifiableRootSection } from '../enums';
3
+
4
+ export const POSSIBLE_CHANGES_TOKEN = symbolToken<ModifiableRootSection[] | undefined>(
5
+ 'POSSIBLE_CHANGES_TOKEN',
6
+ false,
7
+ );