@arc-js/core 0.0.11 → 0.0.13

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.
package/rooting.hooks.jsx CHANGED
@@ -1,205 +1,279 @@
1
- import { r as reactExports } from './_virtual/index';
2
- import { d as distExports } from './_virtual/index2';
3
- import { getLang } from './src/utils';
4
- import qust from './node_modules/@arc-js/qust/index';
1
+ import { useEffect } from 'react';
2
+ import { useLocation, useNavigate, useParams, generatePath, createSearchParams, matchPath } from 'react-router-dom';
3
+ import { getLang } from './utils';
4
+ import { qust } from '@arc-js/qust';
5
+ import React from "react";
5
6
 
7
+
8
+ /**
9
+ * Interface du paramètre de configuration de la fonction "goAndReloadRoute" .
10
+ */
11
+
12
+ ;
13
+
14
+ /**
15
+ * Cette fonction permet de recuperer tous les parameters de l'url
16
+ */
6
17
  function getParams() {
7
- const queryString = window.location.search;
8
- const urlParams = new URLSearchParams(queryString);
9
- return urlParams;
18
+ const queryString = window.location.search;
19
+ const urlParams = new URLSearchParams(queryString);
20
+ return urlParams;
10
21
  }
11
- function nativeResolveRoute(config, incorrectUrl = "") {
12
- try {
13
- const params = (typeof config?.params === 'object' &&
14
- Array.isArray(config?.params) === false) ? config?.params : {};
15
- const queries = (typeof config?.queries === 'object' &&
16
- Array.isArray(config?.queries) === false) ? config?.queries : {};
17
- const path = (typeof config?.path === 'string') ? config?.path : incorrectUrl;
18
- if (!config?.queries) {
19
- config.queries = {};
20
- }
21
- let lang = getLang(config?.queries?.lang);
22
- if (!!config?.queries?.lang && config?.queries?.lang != 'fr') {
23
- lang = getLang(config?.queries?.lang || getParams().get('lang') || 'fr');
24
- }
25
- config.queries = {
26
- ...config.queries,
27
- lang,
28
- };
29
- let res = distExports.generatePath(path, params);
30
- if (Object.keys(queries).length > 0) {
31
- res = `${res}?${distExports.createSearchParams(queries)}`;
32
- }
33
- return res;
34
- }
35
- catch (error) {
36
- if (process.env?.NODE_ENV === 'development') {
37
- console.log(error);
38
- }
39
- return incorrectUrl;
22
+ ;
23
+ /**
24
+ * Interface du paramètre de configuration de la fonction "goAndReloadRoute" .
25
+ */
26
+
27
+ ;
28
+ /**
29
+ * Interface du paramètre de configuration de la fonction "resolveRoute" .
30
+ */
31
+
32
+ ;
33
+ /**
34
+ * Interface du paramètre de configuration de la fonction "goToRoute" .
35
+ */
36
+
37
+ ;
38
+ /**
39
+ * Interface de retour du hooks "useRootingActions".
40
+ */
41
+
42
+ /**
43
+ * Cette fonction permet de recuperer une route en fonction de son path, ses paramètres et ses query parameters.
44
+ * @param config - la donnée de la route à ressortir
45
+ * @param incorrectUrl - la route qui sera prise en compte si le path n'existe pas
46
+ * @returns string
47
+ */
48
+ export function nativeResolveRoute(config, incorrectUrl = "") {
49
+ try {
50
+ const params = typeof config?.params === 'object' && Array.isArray(config?.params) === false ? config?.params : {};
51
+ const queries = typeof config?.queries === 'object' && Array.isArray(config?.queries) === false ? config?.queries : {};
52
+ const path = typeof config?.path === 'string' ? config?.path : incorrectUrl;
53
+ if (!config?.queries) {
54
+ config.queries = {};
40
55
  }
41
- }
42
- const useRootingActions = () => {
43
- const location = distExports.useLocation();
44
- const pathname = location.pathname;
45
- const params = distExports.useParams();
46
- const pathName = distExports.useLocation().pathname;
47
- const urlSearch = distExports.useLocation().search;
48
- const paramsData = new URLSearchParams(distExports.useLocation().search);
49
- let queries = {};
50
- for (const key of paramsData.keys()) {
51
- const valueParamsData = (paramsData.getAll(key).length === 1) ? paramsData.getAll(key)[0] : paramsData.getAll(key);
52
- queries = {
53
- ...queries,
54
- [key]: valueParamsData,
55
- };
56
+ let lang = getLang(config?.queries?.lang);
57
+ if (!!config?.queries?.lang && config?.queries?.lang != 'fr') {
58
+ lang = getLang(config?.queries?.lang || getParams().get('lang') || 'fr');
56
59
  }
57
- const navigate = distExports.useNavigate();
58
- const resolveRoute = (config) => {
59
- const incorrectUrl = pathName;
60
- return nativeResolveRoute(config, incorrectUrl);
60
+ config.queries = {
61
+ ...config.queries,
62
+ lang
61
63
  };
62
- const goToRoute = (config, loaderHandler = () => { }) => {
63
- loaderHandler = !!loaderHandler ? loaderHandler : () => { };
64
- if (!config?.queries) {
65
- config.queries = {};
66
- }
67
- let lang = getLang(config?.queries?.lang);
68
- if (!!config?.queries?.lang && config?.queries?.lang != 'fr') {
69
- lang = getLang(config?.queries?.lang || getParams().get('lang') || 'fr');
70
- }
71
- config.queries = {
72
- ...config.queries,
73
- lang,
74
- };
75
- config.queries = Object.fromEntries(Object.entries(config.queries || {}).filter(([key, value]) => value !== undefined && value !== null));
76
- config.params = Object.fromEntries(Object.entries(config.params || {}).filter(([key, value]) => value !== undefined && value !== null));
77
- if (typeof !!config?.enableLoader === 'undefined' &&
78
- (typeof !!config?.enableLoader === 'boolean' &&
79
- !!config?.enableLoader)) {
80
- loaderHandler();
81
- }
82
- const refreshPage = (typeof config?.refreshPage === 'boolean') ? config?.refreshPage : false;
83
- const pathF = resolveRoute({
84
- path: config?.path,
85
- params: config?.params,
86
- queries: config?.queries,
87
- });
88
- if (!!refreshPage) {
89
- window.location = pathF;
90
- }
91
- else {
92
- navigate(pathF, {
93
- replace: config?.replace,
94
- });
95
- }
64
+ let res = generatePath(path, params);
65
+ if (Object.keys(queries).length > 0) {
66
+ res = `${res}?${createSearchParams(queries)}`;
67
+ }
68
+ return res;
69
+ } catch (error) {
70
+ if (process.env?.NODE_ENV === 'development') {
71
+ console.log(error);
72
+ }
73
+ return incorrectUrl;
74
+ }
75
+ }
76
+ ;
77
+
78
+ /**
79
+ * Ce hooks contient toutes les actions necessaires pour le routing.
80
+ * - recuperer les paramètres de la route
81
+ * - Recuperer les query parameters de la route
82
+ * - naviguer au travers des routes
83
+ * - recuperer une route en fonction de son path, ses paramètres et ses query parameters
84
+ * - naviguer vers une route en fonction de son path, ses paramètres et ses query parameters
85
+ * - naviguer vers une route en fonction de son path, ses paramètres et ses query parameters en rafraîchissant le navigateur
86
+ * - recuperer l'url de la route courante
87
+ * - recuperer la chaîne de caractères contenant les query parameters
88
+ * @type {() => RootingActionReturns}
89
+ * @returns {RootingActionReturns} Resultat du hooks.
90
+ */
91
+ export const useRootingActions = () => {
92
+ const location = useLocation();
93
+ const pathname = location.pathname;
94
+ /**
95
+ * L'ensemble des parametres de la route
96
+ * @type {Readonly<Params<string>>}
97
+ */
98
+ const params = useParams();
99
+ /**
100
+ * L'url de la route courante
101
+ * @type {string}
102
+ */
103
+ const pathName = useLocation().pathname;
104
+ /**
105
+ * La chaîne de caractères contenant les query parameters
106
+ * @type {string}
107
+ */
108
+ const urlSearch = useLocation().search;
109
+ const paramsData = new URLSearchParams(useLocation().search);
110
+
111
+ /**
112
+ * L'ensemble des query parameters de la route
113
+ * @type {any}
114
+ */
115
+ let queries = {};
116
+ for (const key of paramsData.keys()) {
117
+ const valueParamsData = paramsData.getAll(key).length === 1 ? paramsData.getAll(key)[0] : paramsData.getAll(key);
118
+ queries = {
119
+ ...queries,
120
+ [key]: valueParamsData
96
121
  };
97
- const goAndReloadRoute = (config, loaderHandler = () => { }) => {
98
- loaderHandler = !!loaderHandler ? loaderHandler : () => { };
99
- if (!config?.queries) {
100
- config.queries = {};
101
- }
102
- let lang = getLang(config?.queries?.lang);
103
- if (!!config?.queries?.lang && config?.queries?.lang != 'fr') {
104
- lang = getLang(config?.queries?.lang || getParams().get('lang') || 'fr');
105
- }
106
- config.queries = {
107
- ...config.queries,
108
- lang,
109
- };
110
- config.queries = Object.fromEntries(Object.entries(config.queries || {}).filter(([key, value]) => value !== undefined && value !== null));
111
- config.params = Object.fromEntries(Object.entries(config.params || {}).filter(([key, value]) => value !== undefined && value !== null));
112
- const url = resolveRoute(config);
113
- window.location = url;
114
- if (typeof !!config?.enableLoader === 'undefined' &&
115
- (typeof !!config?.enableLoader === 'boolean' &&
116
- !!config?.enableLoader)) {
117
- loaderHandler();
118
- }
122
+ }
123
+ const navigate = useNavigate();
124
+
125
+ /**
126
+ * Cette fonction permet de recuperer une route en fonction de son path, ses paramètres et ses query parameters.
127
+ * @param {ConfigResolveRoute} config - Paramètre de configuration ou de paramètrage.
128
+ * @type {(config: ConfigResolveRoute) => string}
129
+ * @returns {string} Resultat de la fonction.
130
+ */
131
+ const resolveRoute = config => {
132
+ const incorrectUrl = pathName;
133
+ return nativeResolveRoute(config, incorrectUrl);
134
+ };
135
+ /**
136
+ * Cette fonction permet de naviguer vers une route en fonction de son path, ses paramètres et ses query parameters.
137
+ * @param {ConfigGoToRoute} config - Paramètre de configuration ou de paramètrage.
138
+ * @type {(config: ConfigGoToRoute) => void}
139
+ */
140
+ const goToRoute = (config, loaderHandler = () => {}) => {
141
+ loaderHandler = !!loaderHandler ? loaderHandler : () => {};
142
+ if (!config?.queries) {
143
+ config.queries = {};
144
+ }
145
+ let lang = getLang(config?.queries?.lang);
146
+ if (!!config?.queries?.lang && config?.queries?.lang != 'fr') {
147
+ lang = getLang(config?.queries?.lang || getParams().get('lang') || 'fr');
148
+ }
149
+ config.queries = {
150
+ ...config.queries,
151
+ lang
119
152
  };
120
- const getUrlData = (url) => {
121
- let urlF = undefined;
122
- if (typeof url === 'string' &&
123
- url.length > 0) {
124
- urlF = new URL(url);
125
- }
126
- else if (url instanceof URL) {
127
- urlF = url;
128
- }
129
- const paramsData = urlF?.search ? new URLSearchParams(urlF?.search) : undefined;
130
- let queries = undefined;
131
- if (!!paramsData) {
132
- queries = {};
133
- for (const key of paramsData.keys()) {
134
- const valueParamsData = (paramsData.getAll(key).length === 1) ? paramsData.getAll(key)[0] : paramsData.getAll(key);
135
- queries = {
136
- ...queries,
137
- [key]: valueParamsData,
138
- };
139
- }
140
- }
141
- return urlF ? {
142
- host: urlF?.host,
143
- hostname: urlF?.hostname,
144
- pathname: urlF?.pathname,
145
- search: urlF?.search,
146
- queries,
147
- } : undefined;
153
+ config.queries = Object.fromEntries(Object.entries(config.queries || {}).filter(([key, value]) => value !== undefined && value !== null));
154
+ config.params = Object.fromEntries(Object.entries(config.params || {}).filter(([key, value]) => value !== undefined && value !== null));
155
+ if (typeof !!config?.enableLoader === 'undefined' && typeof !!config?.enableLoader === 'boolean' && !!config?.enableLoader) {
156
+ loaderHandler();
157
+ }
158
+ const refreshPage = typeof config?.refreshPage === 'boolean' ? config?.refreshPage : false;
159
+ const pathF = resolveRoute({
160
+ path: config?.path,
161
+ params: config?.params,
162
+ queries: config?.queries
163
+ });
164
+ if (!!refreshPage) {
165
+ window.location = pathF;
166
+ } else {
167
+ navigate(pathF, {
168
+ replace: config?.replace
169
+ });
170
+ }
171
+ };
172
+ /**
173
+ * Cette fonction permet de naviguer vers une route en fonction de son path, ses paramètres et ses query parameters en rafraîchissant le navigateur.
174
+ * @param {ConfigGoAndReloadRoute} config - Paramètre de configuration ou de paramètrage.
175
+ * @type {(config: ConfigGoAndReloadRoute) => void}
176
+ */
177
+ const goAndReloadRoute = (config, loaderHandler = () => {}) => {
178
+ loaderHandler = !!loaderHandler ? loaderHandler : () => {};
179
+ if (!config?.queries) {
180
+ config.queries = {};
181
+ }
182
+ let lang = getLang(config?.queries?.lang);
183
+ if (!!config?.queries?.lang && config?.queries?.lang != 'fr') {
184
+ lang = getLang(config?.queries?.lang || getParams().get('lang') || 'fr');
185
+ }
186
+ config.queries = {
187
+ ...config.queries,
188
+ lang
148
189
  };
149
- const goToUrl = (config) => {
150
- const incorrectUrl = "";
151
- const queries = (typeof config?.queries === 'object' &&
152
- Array.isArray(config?.queries) === false) ? config?.queries : {};
153
- const path = (typeof config?.path === 'string') ? config?.path : incorrectUrl;
154
- if (!config?.queries) {
155
- config.queries = {};
156
- }
157
- let lang = getLang(config?.queries?.lang);
158
- if (!!config?.queries?.lang && config?.queries?.lang != 'fr') {
159
- lang = getLang(config?.queries?.lang || getParams().get('lang') || 'fr');
160
- }
161
- config.queries = {
162
- ...config.queries,
163
- lang,
190
+ config.queries = Object.fromEntries(Object.entries(config.queries || {}).filter(([key, value]) => value !== undefined && value !== null));
191
+ config.params = Object.fromEntries(Object.entries(config.params || {}).filter(([key, value]) => value !== undefined && value !== null));
192
+ // goToRoute({
193
+ // ...config,
194
+ // refreshPage: false,
195
+ // })
196
+ const url = resolveRoute(config);
197
+ window.location = url;
198
+ if (typeof !!config?.enableLoader === 'undefined' && typeof !!config?.enableLoader === 'boolean' && !!config?.enableLoader) {
199
+ loaderHandler();
200
+ }
201
+ };
202
+ const getUrlData = url => {
203
+ let urlF = undefined;
204
+ if (typeof url === 'string' && url.length > 0) {
205
+ urlF = new URL(url);
206
+ } else if (url instanceof URL) {
207
+ urlF = url;
208
+ }
209
+ const paramsData = urlF?.search ? new URLSearchParams(urlF?.search) : undefined;
210
+ let queries = undefined;
211
+ if (!!paramsData) {
212
+ queries = {};
213
+ for (const key of paramsData.keys()) {
214
+ const valueParamsData = paramsData.getAll(key).length === 1 ? paramsData.getAll(key)[0] : paramsData.getAll(key);
215
+ queries = {
216
+ ...queries,
217
+ [key]: valueParamsData
164
218
  };
165
- const uncleanUrl = qust.stringify(queries);
166
- const qsValue = decodeURI(uncleanUrl);
167
- const url = `${path}?${qsValue}`;
168
- if (!!config?.refreshPage) {
169
- window.location = url;
170
- }
171
- else {
172
- window.location.assign(url);
173
- }
174
- };
175
- const checkIfIsCurrentRoute = (path, exact = true, strict = true) => {
176
- const pathF = (typeof path === 'string' &&
177
- path.length > 0) ? path : '';
178
- const exactF = (typeof exact === 'boolean') ? exact : true;
179
- const strictF = (typeof strict === 'boolean') ? strict : true;
180
- const matchAbout = distExports.matchPath({
181
- path: pathF,
182
- end: exactF,
183
- caseSensitive: strictF,
184
- }, pathname);
185
- return !!matchAbout;
186
- };
187
- reactExports.useEffect(() => {
188
- }, []);
189
- return {
190
- params,
191
- queries,
192
- navigate,
193
- resolveRoute,
194
- goToRoute,
195
- goAndReloadRoute,
196
- pathName,
197
- urlSearch,
198
- getUrlData,
199
- goToUrl,
200
- getParams,
201
- checkIfIsCurrentRoute,
219
+ }
220
+ }
221
+ return urlF ? {
222
+ host: urlF?.host,
223
+ hostname: urlF?.hostname,
224
+ pathname: urlF?.pathname,
225
+ search: urlF?.search,
226
+ queries
227
+ } : undefined;
228
+ };
229
+ const goToUrl = config => {
230
+ const incorrectUrl = "";
231
+ const queries = typeof config?.queries === 'object' && Array.isArray(config?.queries) === false ? config?.queries : {};
232
+ const path = typeof config?.path === 'string' ? config?.path : incorrectUrl;
233
+ if (!config?.queries) {
234
+ config.queries = {};
235
+ }
236
+ let lang = getLang(config?.queries?.lang);
237
+ if (!!config?.queries?.lang && config?.queries?.lang != 'fr') {
238
+ lang = getLang(config?.queries?.lang || getParams().get('lang') || 'fr');
239
+ }
240
+ config.queries = {
241
+ ...config.queries,
242
+ lang
202
243
  };
203
- };
204
-
205
- export { nativeResolveRoute, useRootingActions };
244
+ const uncleanUrl = qust.stringify(queries);
245
+ const qsValue = decodeURI(uncleanUrl);
246
+ const url = `${path}?${qsValue}`;
247
+ if (!!config?.refreshPage) {
248
+ window.location = url;
249
+ } else {
250
+ window.location.assign(url);
251
+ }
252
+ };
253
+ const checkIfIsCurrentRoute = (path, exact = true, strict = true) => {
254
+ const pathF = typeof path === 'string' && path.length > 0 ? path : '';
255
+ const exactF = typeof exact === 'boolean' ? exact : true;
256
+ const strictF = typeof strict === 'boolean' ? strict : true;
257
+ const matchAbout = matchPath({
258
+ path: pathF,
259
+ end: exactF,
260
+ caseSensitive: strictF
261
+ }, pathname);
262
+ return !!matchAbout;
263
+ };
264
+ useEffect(() => {}, []);
265
+ return {
266
+ params,
267
+ queries,
268
+ navigate,
269
+ resolveRoute,
270
+ goToRoute,
271
+ goAndReloadRoute,
272
+ pathName,
273
+ urlSearch,
274
+ getUrlData,
275
+ goToUrl,
276
+ getParams,
277
+ checkIfIsCurrentRoute
278
+ };
279
+ };