@masterteam/work-center 0.0.102 → 0.0.104

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.
@@ -3,7 +3,7 @@ import { inject, Injectable, computed } from '@angular/core';
3
3
  import { Action, Selector, State, Store, select } from '@ngxs/store';
4
4
  import { HttpClient } from '@angular/common/http';
5
5
  import { handleApiRequest } from '@masterteam/components';
6
- import { catchError, EMPTY, map } from 'rxjs';
6
+ import { Subject, takeUntil, filter, catchError, EMPTY, map } from 'rxjs';
7
7
 
8
8
  class EnterArea {
9
9
  area;
@@ -30,6 +30,19 @@ class SetParams {
30
30
  this.inputs = inputs;
31
31
  }
32
32
  }
33
+ /**
34
+ * The page showing this area was destroyed. The next `SetParams` for the area
35
+ * reloads even when its context is unchanged: a host page that initializes
36
+ * again must not show the rows it left as current. Within one page visit an
37
+ * unchanged context does not reload.
38
+ */
39
+ class LeavePage {
40
+ area;
41
+ static type = '[WorkCenter] Leave Page';
42
+ constructor(area) {
43
+ this.area = area;
44
+ }
45
+ }
33
46
  class SetLookups {
34
47
  lookups;
35
48
  static type = '[WorkCenter] Set Lookups';
@@ -606,6 +619,18 @@ function isSameContext(left, right) {
606
619
  JSON.stringify(left.sort) === JSON.stringify(right.sort) &&
607
620
  JSON.stringify(left.runtimeFilters) === JSON.stringify(right.runtimeFilters));
608
621
  }
622
+ /**
623
+ * Same workspace scope: rows loaded for one template or record must not be
624
+ * shown, even briefly, under another.
625
+ */
626
+ function isSameScope(left, right) {
627
+ return (left.templateId === right.templateId &&
628
+ left.levelDataId === right.levelDataId);
629
+ }
630
+ /** The area without the rows, KPIs and total of its last load. */
631
+ function withoutLoadedRows(areaState) {
632
+ return { ...areaState, rows: [], kpis: [], totalCount: 0 };
633
+ }
609
634
  function mapRuntimeResponse(current, response) {
610
635
  const selectedCard = response.selectedCard;
611
636
  const menuItems = buildMenuItems(response.cards);
@@ -667,6 +692,10 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
667
692
  };
668
693
  let WorkCenterState = class WorkCenterState {
669
694
  http = inject(HttpClient);
695
+ /** Emits an area whose in-flight runtime request a newer one replaces. */
696
+ runtimeSuperseded = new Subject();
697
+ /** Areas whose page was left since their last load (see `LeavePage`). */
698
+ areasLeft = new Set();
670
699
  static getState(state) {
671
700
  return state;
672
701
  }
@@ -747,7 +776,7 @@ let WorkCenterState = class WorkCenterState {
747
776
  byArea: {
748
777
  ...state.byArea,
749
778
  [action.area]: {
750
- ...current,
779
+ ...this.keepRowsInScope(current, nextContext),
751
780
  headerFilters: nextHeaderFilters,
752
781
  columnFilters: nextColumnFilters,
753
782
  context: nextContext,
@@ -755,6 +784,9 @@ let WorkCenterState = class WorkCenterState {
755
784
  },
756
785
  });
757
786
  }
787
+ leavePage(_ctx, action) {
788
+ this.areasLeft.add(action.area);
789
+ }
758
790
  setParams(ctx, action) {
759
791
  const state = ctx.getState();
760
792
  const current = state.byArea[action.area];
@@ -775,7 +807,15 @@ let WorkCenterState = class WorkCenterState {
775
807
  const contextChanged = !isSameContext(current.context, nextContext);
776
808
  const groupChanged = nextGroupBy !== current.groupBy;
777
809
  const runtimeLoading = state.loadingActive.includes(WorkCenterActionKey.LoadRuntime);
778
- const shouldLoad = areaChanged || contextChanged || !runtimeLoading;
810
+ // An unchanged context reloads only on a new page visit (`LeavePage`) or
811
+ // before the first load — not on every re-run of the host's effect, such
812
+ // as the route rewrite when the first item opens. A load in flight is
813
+ // already the fresh one.
814
+ const needsFreshRows = this.areasLeft.has(action.area) || current.lastLoadedAt === null;
815
+ if (runtimeLoading) {
816
+ this.areasLeft.delete(action.area);
817
+ }
818
+ const shouldLoad = areaChanged || contextChanged || (needsFreshRows && !runtimeLoading);
779
819
  if (areaChanged || contextChanged || groupChanged) {
780
820
  // URL-derived filters land on the header slice. Anything previously
781
821
  // applied via the in-table column filters is dropped so the BE payload
@@ -791,7 +831,7 @@ let WorkCenterState = class WorkCenterState {
791
831
  byArea: {
792
832
  ...state.byArea,
793
833
  [action.area]: {
794
- ...current,
834
+ ...this.keepRowsInScope(current, nextContext),
795
835
  headerFilters: nextHeaderFilters,
796
836
  columnFilters: nextColumnFilters,
797
837
  groupBy: nextGroupBy,
@@ -921,26 +961,22 @@ let WorkCenterState = class WorkCenterState {
921
961
  });
922
962
  return ctx.dispatch(new LoadRuntime(area, 'filters-change'));
923
963
  }
964
+ /**
965
+ * The previous rows stay on screen (under the grid's loading state) until
966
+ * the response replaces them, so a tab switch no longer blanks the grid.
967
+ * A newer load of the same area cancels the one in flight: a slower old
968
+ * response could otherwise land last and overwrite the newer card.
969
+ */
924
970
  loadRuntime(ctx, action) {
925
- const state = ctx.getState();
926
- const areaState = state.byArea[action.area];
927
- const request = normalizeContext(areaState.context);
928
- const nextAreaState = {
929
- ...areaState,
930
- rows: [],
931
- kpis: [],
932
- totalCount: 0,
933
- };
934
- ctx.patchState({
935
- byArea: {
936
- ...state.byArea,
937
- [action.area]: nextAreaState,
938
- },
939
- });
971
+ // First, so the cancelled request's `finalize` ends its loading flag
972
+ // before this one starts it again.
973
+ this.runtimeSuperseded.next(action.area);
974
+ this.areasLeft.delete(action.area);
975
+ const request = normalizeContext(ctx.getState().byArea[action.area].context);
940
976
  return handleApiRequest({
941
977
  ctx,
942
978
  key: WorkCenterActionKey.LoadRuntime,
943
- request$: this.getRuntime(request),
979
+ request$: this.getRuntime(request).pipe(takeUntil(this.runtimeSuperseded.pipe(filter((area) => area === action.area)))),
944
980
  onSuccess: (response, latestState) => {
945
981
  const current = latestState.byArea[action.area];
946
982
  return {
@@ -950,9 +986,22 @@ let WorkCenterState = class WorkCenterState {
950
986
  },
951
987
  };
952
988
  },
989
+ // The kept rows belong to the previous query; after a failure they
990
+ // would read as the answer to this one.
991
+ onError: (_error, latestState) => ({
992
+ byArea: {
993
+ ...latestState.byArea,
994
+ [action.area]: withoutLoadedRows(latestState.byArea[action.area]),
995
+ },
996
+ }),
953
997
  errorMessage: 'Failed to load Work Center runtime.',
954
998
  }).pipe(catchError(() => EMPTY));
955
999
  }
1000
+ keepRowsInScope(current, nextContext) {
1001
+ return isSameScope(current.context, nextContext)
1002
+ ? current
1003
+ : withoutLoadedRows(current);
1004
+ }
956
1005
  getRuntime(request) {
957
1006
  return this.http
958
1007
  .post('workcenter/runtime', request)
@@ -975,6 +1024,9 @@ __decorate([
975
1024
  __decorate([
976
1025
  Action(HydrateFromContext)
977
1026
  ], WorkCenterState.prototype, "hydrateFromContext", null);
1027
+ __decorate([
1028
+ Action(LeavePage)
1029
+ ], WorkCenterState.prototype, "leavePage", null);
978
1030
  __decorate([
979
1031
  Action(SetParams)
980
1032
  ], WorkCenterState.prototype, "setParams", null);
@@ -1058,7 +1110,7 @@ WorkCenterState = __decorate([
1058
1110
  ], WorkCenterState);
1059
1111
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.8", ngImport: i0, type: WorkCenterState, decorators: [{
1060
1112
  type: Injectable
1061
- }], propDecorators: { enterArea: [], hydrateFromContext: [], setParams: [], setLookups: [], applyHeaderFilters: [], applyColumnFilters: [], selectCard: [], setGroupBy: [], clearAllFilters: [], loadRuntime: [] } });
1113
+ }], propDecorators: { enterArea: [], hydrateFromContext: [], leavePage: [], setParams: [], setLookups: [], applyHeaderFilters: [], applyColumnFilters: [], selectCard: [], setGroupBy: [], clearAllFilters: [], loadRuntime: [] } });
1062
1114
 
1063
1115
  class WorkCenterFacade {
1064
1116
  store = inject(Store);
@@ -1099,6 +1151,14 @@ class WorkCenterFacade {
1099
1151
  setParams(area, inputs) {
1100
1152
  return this.store.dispatch(new SetParams(area, inputs));
1101
1153
  }
1154
+ /**
1155
+ * Called by `mt-work-center-page` when it is destroyed: the next
1156
+ * `setParams` for the area is a new page visit and reloads even with an
1157
+ * unchanged context.
1158
+ */
1159
+ leavePage(area) {
1160
+ return this.store.dispatch(new LeavePage(area));
1161
+ }
1102
1162
  setLookups(lookups) {
1103
1163
  return this.store.dispatch(new SetLookups(lookups));
1104
1164
  }
@@ -1283,5 +1343,5 @@ function decodeRouteSafeContextKey(hex) {
1283
1343
  * Generated bundle index. Do not edit.
1284
1344
  */
1285
1345
 
1286
- export { APP_STATES, ApplyColumnFilters, ApplyHeaderFilters, ClearAllFilters, EnterArea, HydrateFromContext, LoadRuntime, SelectCard, SetGroupBy, SetLookups, SetParams, WORK_CENTER_DEFAULT_STATE, WORK_CENTER_EMPTY_LABEL, WORK_CENTER_GROUP_NONE, WORK_CENTER_LEVEL_GROUP_FIELD, WORK_CENTER_LEVEL_NAME_KEY, WORK_CENTER_MAX_FILTERS, WORK_CENTER_MAX_PAGE_SIZE, WORK_CENTER_MAX_SORT, WORK_CENTER_QUERY_VERSION, WorkCenterActionKey, WorkCenterFacade, WorkCenterState, decodeWorkCenterRouteContextKey, encodeWorkCenterRouteContextKey };
1346
+ export { APP_STATES, ApplyColumnFilters, ApplyHeaderFilters, ClearAllFilters, EnterArea, HydrateFromContext, LeavePage, LoadRuntime, SelectCard, SetGroupBy, SetLookups, SetParams, WORK_CENTER_DEFAULT_STATE, WORK_CENTER_EMPTY_LABEL, WORK_CENTER_GROUP_NONE, WORK_CENTER_LEVEL_GROUP_FIELD, WORK_CENTER_LEVEL_NAME_KEY, WORK_CENTER_MAX_FILTERS, WORK_CENTER_MAX_PAGE_SIZE, WORK_CENTER_MAX_SORT, WORK_CENTER_QUERY_VERSION, WorkCenterActionKey, WorkCenterFacade, WorkCenterState, decodeWorkCenterRouteContextKey, encodeWorkCenterRouteContextKey };
1287
1347
  //# sourceMappingURL=masterteam-work-center-state.mjs.map