@zauru-sdk/redux 1.0.10

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 (43) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/LICENCE.md +11 -0
  3. package/dist/slices/agency.slice.d.ts +8 -0
  4. package/dist/slices/agency.slice.js +20 -0
  5. package/dist/slices/automaticNumbers.slice.d.ts +17 -0
  6. package/dist/slices/automaticNumbers.slice.js +20 -0
  7. package/dist/slices/catalogs.slice.d.ts +50 -0
  8. package/dist/slices/catalogs.slice.js +61 -0
  9. package/dist/slices/formValidation.slice.d.ts +14 -0
  10. package/dist/slices/formValidation.slice.js +16 -0
  11. package/dist/slices/index.d.ts +11 -0
  12. package/dist/slices/index.js +11 -0
  13. package/dist/slices/profile.slice.d.ts +26 -0
  14. package/dist/slices/profile.slice.js +22 -0
  15. package/dist/slices/reception.slice.d.ts +43 -0
  16. package/dist/slices/reception.slice.js +57 -0
  17. package/dist/slices/session.slice.d.ts +9 -0
  18. package/dist/slices/session.slice.js +13 -0
  19. package/dist/slices/tables.slice.d.ts +12 -0
  20. package/dist/slices/tables.slice.js +17 -0
  21. package/dist/slices/templates.slice.d.ts +13 -0
  22. package/dist/slices/templates.slice.js +19 -0
  23. package/dist/slices/user.slice.d.ts +12 -0
  24. package/dist/slices/user.slice.js +38 -0
  25. package/dist/slices/webapp-tables.slice.d.ts +8 -0
  26. package/dist/slices/webapp-tables.slice.js +20 -0
  27. package/dist/store.d.ts +8 -0
  28. package/dist/store.js +125 -0
  29. package/package.json +31 -0
  30. package/src/slices/agency.slice.ts +32 -0
  31. package/src/slices/automaticNumbers.slice.ts +41 -0
  32. package/src/slices/catalogs.slice.ts +178 -0
  33. package/src/slices/formValidation.slice.ts +32 -0
  34. package/src/slices/index.ts +11 -0
  35. package/src/slices/profile.slice.ts +49 -0
  36. package/src/slices/reception.slice.ts +118 -0
  37. package/src/slices/session.slice.ts +25 -0
  38. package/src/slices/tables.slice.ts +33 -0
  39. package/src/slices/templates.slice.ts +34 -0
  40. package/src/slices/user.slice.ts +64 -0
  41. package/src/slices/webapp-tables.slice.ts +35 -0
  42. package/src/store.ts +152 -0
  43. package/tsconfig.json +24 -0
@@ -0,0 +1,34 @@
1
+ import type { PayloadAction } from "@reduxjs/toolkit";
2
+ import { createSlice } from "@reduxjs/toolkit";
3
+
4
+ export type TEMPLATE_NAMES = "receptionTemplate";
5
+
6
+ export interface TemplateState {
7
+ receptionTemplate: { loading: boolean; data: string };
8
+ }
9
+
10
+ const initialState: TemplateState = {
11
+ receptionTemplate: { data: "", loading: false },
12
+ };
13
+
14
+ const templateSlice = createSlice({
15
+ name: "template",
16
+ initialState,
17
+ reducers: {
18
+ templateFetchStart: (state, action: PayloadAction<TEMPLATE_NAMES>) => {
19
+ state[action.payload].loading = true;
20
+ },
21
+ templateFetchSuccess: (
22
+ state,
23
+ action: PayloadAction<{ name: TEMPLATE_NAMES; data: any }>
24
+ ) => {
25
+ state[action.payload.name].data = action.payload.data;
26
+ state[action.payload.name].loading = false;
27
+ },
28
+ },
29
+ });
30
+
31
+ export const { templateFetchStart, templateFetchSuccess } =
32
+ templateSlice.actions;
33
+
34
+ export default templateSlice.reducer;
@@ -0,0 +1,64 @@
1
+ import type { PayloadAction } from "@reduxjs/toolkit";
2
+ import { createSlice } from "@reduxjs/toolkit";
3
+ import {
4
+ EmployeeGraphQL,
5
+ OauthProfile,
6
+ ProfileResponse,
7
+ } from "@zauru-sdk/types";
8
+
9
+ export interface UserState {
10
+ userProfile: ProfileResponse;
11
+ loadingUserProfile: boolean;
12
+ oauthProfile: OauthProfile;
13
+ loadingOauthProfile: boolean;
14
+ employeeProfile: EmployeeGraphQL;
15
+ loadingEmployeeInfo: boolean;
16
+ }
17
+
18
+ const initialState: UserState = {
19
+ userProfile: {} as ProfileResponse,
20
+ loadingUserProfile: false,
21
+ oauthProfile: {} as OauthProfile,
22
+ loadingOauthProfile: false,
23
+ employeeProfile: {} as EmployeeGraphQL,
24
+ loadingEmployeeInfo: false,
25
+ };
26
+
27
+ const userSlice = createSlice({
28
+ name: "user",
29
+ initialState,
30
+ reducers: {
31
+ setUserProfile: (state, action: PayloadAction<ProfileResponse>) => {
32
+ state.userProfile = action.payload;
33
+ state.loadingUserProfile = false;
34
+ },
35
+ setLoadingUserProfile: (state, action: PayloadAction<boolean>) => {
36
+ state.loadingUserProfile = action.payload;
37
+ },
38
+ setOauthProfile: (state, action: PayloadAction<OauthProfile>) => {
39
+ state.oauthProfile = action.payload;
40
+ state.loadingOauthProfile = false;
41
+ },
42
+ setLoadingOauthProfile: (state, action: PayloadAction<boolean>) => {
43
+ state.loadingOauthProfile = action.payload;
44
+ },
45
+ setEmployeeProfile: (state, action: PayloadAction<EmployeeGraphQL>) => {
46
+ state.employeeProfile = action.payload;
47
+ state.loadingEmployeeInfo = false;
48
+ },
49
+ setLoadingEmployeeProfile: (state, action: PayloadAction<boolean>) => {
50
+ state.loadingEmployeeInfo = action.payload;
51
+ },
52
+ },
53
+ });
54
+
55
+ export const {
56
+ setUserProfile,
57
+ setEmployeeProfile,
58
+ setOauthProfile,
59
+ setLoadingEmployeeProfile,
60
+ setLoadingOauthProfile,
61
+ setLoadingUserProfile,
62
+ } = userSlice.actions;
63
+
64
+ export default userSlice.reducer;
@@ -0,0 +1,35 @@
1
+ import type { PayloadAction } from "@reduxjs/toolkit";
2
+ import { createSlice } from "@reduxjs/toolkit";
3
+ import { SpecialItem, WebAppRowGraphQL } from "@zauru-sdk/types";
4
+
5
+ export interface WebAppTableState {
6
+ specialItems: WebAppRowGraphQL<SpecialItem>[];
7
+ loadingSpecialItems: boolean;
8
+ }
9
+
10
+ const initialState: WebAppTableState = {
11
+ specialItems: [],
12
+ loadingSpecialItems: false,
13
+ };
14
+
15
+ const webappTablesSlice = createSlice({
16
+ name: "webappTables",
17
+ initialState,
18
+ reducers: {
19
+ setLoadingSpecialItems: (state, action: PayloadAction<boolean>) => {
20
+ state.loadingSpecialItems = action.payload;
21
+ },
22
+ setSpecialItems: (
23
+ state,
24
+ action: PayloadAction<WebAppRowGraphQL<SpecialItem>[]>
25
+ ) => {
26
+ state.specialItems = action.payload;
27
+ state.loadingSpecialItems = false;
28
+ },
29
+ },
30
+ });
31
+
32
+ export const { setLoadingSpecialItems, setSpecialItems } =
33
+ webappTablesSlice.actions;
34
+
35
+ export default webappTablesSlice.reducer;
package/src/store.ts ADDED
@@ -0,0 +1,152 @@
1
+ import type { MiddlewareAPI } from "@reduxjs/toolkit";
2
+ import { Tuple, configureStore } from "@reduxjs/toolkit";
3
+ import catalogsReducer from "./slices/catalogs.slice.js";
4
+ import profilesReducer from "./slices/profile.slice.js";
5
+ import webappTablesReducer from "./slices/webapp-tables.slice.js";
6
+ import receptionsReducer from "./slices/reception.slice.js";
7
+ import sessionReducer from "./slices/session.slice.js";
8
+ import templateReducer from "./slices/templates.slice.js";
9
+ import automaticNumberReducer from "./slices/automaticNumbers.slice.js";
10
+ import tableReducer from "./slices/tables.slice.js";
11
+ import formValidationReducer from "./slices/formValidation.slice.js";
12
+
13
+ export const LOCAL_STORAGE_REDUX_NAME = "___redux__state__v2";
14
+
15
+ const persistanceLocalStorageMiddleware =
16
+ (store: MiddlewareAPI) => (next: (action: any) => any) => (action: any) => {
17
+ const result = next(action);
18
+ if (!(typeof document === "undefined")) {
19
+ localStorage.setItem(
20
+ LOCAL_STORAGE_REDUX_NAME,
21
+ JSON.stringify(store.getState() as RootState)
22
+ );
23
+ }
24
+ return result;
25
+ };
26
+
27
+ const preloadedState = (() => {
28
+ try {
29
+ if (!(typeof document === "undefined")) {
30
+ const savedState = localStorage.getItem(LOCAL_STORAGE_REDUX_NAME);
31
+ if (savedState) {
32
+ return JSON.parse(savedState);
33
+ }
34
+ }
35
+ } catch (e) {
36
+ // Ignorar los errores, se utiliza el estado inicial definido en cada reducer
37
+ }
38
+ })();
39
+
40
+ type Whitelist = {
41
+ [K in keyof RootState]?: string[];
42
+ };
43
+
44
+ export const cleanLocalStorage = () => {
45
+ const savedState = localStorage.getItem(LOCAL_STORAGE_REDUX_NAME);
46
+ const state = JSON.parse(savedState ?? "{}") as RootState;
47
+ try {
48
+ //delete all cookies
49
+ // const deleteAllCookies = () => {
50
+ // const cookies = document.cookie.split(";");
51
+ // for (const cookie of cookies) {
52
+ // const eqPos = cookie.indexOf("=");
53
+ // const name = eqPos > -1 ? cookie.substring(0, eqPos) : cookie;
54
+ // document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
55
+ // }
56
+ // };
57
+
58
+ // deleteAllCookies();
59
+
60
+ // //delete caché
61
+ // caches.keys().then((names) => {
62
+ // for (const name of names) {
63
+ // caches.delete(name);
64
+ // }
65
+ // });
66
+
67
+ //unregister service workers
68
+ // navigator.serviceWorker.getRegistrations().then((registrations) => {
69
+ // for (const registration of registrations) {
70
+ // registration.unregister();
71
+ // }
72
+ // });
73
+
74
+ // Limpiar todo el localStorage
75
+ localStorage.clear();
76
+ } catch (error) {
77
+ console.error(error);
78
+ }
79
+
80
+ try {
81
+ const whitelist: Whitelist = {
82
+ automaticNumbers: ["purchaseOrderIdNumber", "dischargeIdNumber"],
83
+ receptions: ["queueNewReceptions"],
84
+ };
85
+
86
+ if (!(typeof document === "undefined") && Object.keys(state)?.length > 0) {
87
+ const initialState: RootState = {
88
+ catalogs: catalogsReducer(undefined, { type: "" }),
89
+ profiles: profilesReducer(undefined, { type: "" }),
90
+ webappTables: webappTablesReducer(undefined, { type: "" }),
91
+ receptions: receptionsReducer(undefined, { type: "" }),
92
+ session: sessionReducer(undefined, { type: "" }),
93
+ templates: templateReducer(undefined, { type: "" }),
94
+ automaticNumbers: automaticNumberReducer(undefined, { type: "" }),
95
+ tables: tableReducer(undefined, { type: "" }),
96
+ formValidation: formValidationReducer(undefined, { type: "" }),
97
+ };
98
+
99
+ const newState: RootState = { ...initialState };
100
+
101
+ for (const reducerName in whitelist) {
102
+ if (state.hasOwnProperty(reducerName)) {
103
+ const key = reducerName as keyof RootState;
104
+ const reducerState = state[key];
105
+ const whitelistKey = whitelist[key as any];
106
+
107
+ if (whitelistKey && whitelistKey.length > 0) {
108
+ newState[key] = newState[key] ? { ...newState[key] } : ({} as any);
109
+
110
+ for (const propertyName of whitelistKey) {
111
+ if (reducerState?.hasOwnProperty(propertyName)) {
112
+ const propKey = propertyName as keyof typeof reducerState;
113
+ if (newState[key] && reducerState) {
114
+ newState[key][propKey] = reducerState[propKey];
115
+ }
116
+ }
117
+ }
118
+ } else if (reducerState) {
119
+ newState[key] = reducerState as any;
120
+ }
121
+ }
122
+ }
123
+
124
+ // Guarda el nuevo estado en el almacenamiento local
125
+ localStorage.setItem(LOCAL_STORAGE_REDUX_NAME, JSON.stringify(newState));
126
+ }
127
+ } catch (e) {
128
+ console.error(
129
+ "Ocurrió un error al clonar y eliminar el viejo localStorage"
130
+ );
131
+ }
132
+ };
133
+
134
+ export const store = configureStore({
135
+ reducer: {
136
+ catalogs: catalogsReducer,
137
+ profiles: profilesReducer,
138
+ webappTables: webappTablesReducer,
139
+ receptions: receptionsReducer,
140
+ session: sessionReducer,
141
+ templates: templateReducer,
142
+ automaticNumbers: automaticNumberReducer,
143
+ tables: tableReducer,
144
+ formValidation: formValidationReducer,
145
+ } as any,
146
+ preloadedState,
147
+ middleware: (getDefaultMiddleware) =>
148
+ new Tuple(persistanceLocalStorageMiddleware),
149
+ });
150
+
151
+ export type RootState = ReturnType<typeof store.getState>;
152
+ export type AppDispatch = typeof store.dispatch;
package/tsconfig.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "include": ["env.d.ts", "**/*.ts", "**/*.tsx"],
3
+ "compilerOptions": {
4
+ "allowSyntheticDefaultImports": true,
5
+ "lib": ["DOM", "DOM.Iterable", "ES2022"],
6
+ "isolatedModules": true,
7
+ "moduleResolution": "NodeNext",
8
+ "module": "NodeNext",
9
+ "esModuleInterop": true,
10
+ "target": "ES2022",
11
+ "strict": true,
12
+ "allowJs": true,
13
+ "skipLibCheck": true,
14
+ "forceConsistentCasingInFileNames": true,
15
+ "baseUrl": ".",
16
+ "outDir": "./dist",
17
+ "rootDir": "./src",
18
+ "declaration": true,
19
+ "declarationDir": "./dist",
20
+ "paths": {
21
+ "~/*": ["./src/*"]
22
+ }
23
+ }
24
+ }