5htp-core 0.4.4-3 → 0.4.4-4

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "5htp-core",
3
3
  "description": "Convenient TypeScript framework designed for Performance and Productivity.",
4
- "version": "0.4.4-3",
4
+ "version": "0.4.4-4",
5
5
  "author": "Gaetan Le Gac (https://github.com/gaetanlegac)",
6
6
  "repository": "git://github.com/gaetanlegac/5htp-core.git",
7
7
  "license": "MIT",
@@ -150,11 +150,6 @@ pre {
150
150
 
151
151
  --cTxtBase: #555;
152
152
 
153
- .card > & {
154
-
155
- padding: 3vh 0 !important;
156
- }
157
-
158
153
  display: flex;
159
154
  flex-direction: column;
160
155
  gap: 1em;
@@ -62,17 +62,18 @@ export default ({ service: clientRouter }: { service?: ClientRouter }) => {
62
62
 
63
63
  const context = useContext();
64
64
 
65
- // Bind context object to client router
66
- if (clientRouter !== undefined)
67
- clientRouter.context = context;
68
-
69
65
  const [currentPage, setCurrentPage] = React.useState<undefined | Page>(context.page);
70
66
 
67
+ // Bind context object to client router
68
+ if (clientRouter !== undefined) {
69
+ clientRouter.context = context;
70
+ clientRouter.navigate = changePage;
71
+ }
71
72
 
72
73
  /*----------------------------------
73
74
  - ACTIONS
74
75
  ----------------------------------*/
75
- const resolvePage = async (request: ClientRequest, locationUpdate?: Update) => {
76
+ const resolvePage = async (request: ClientRequest) => {
76
77
 
77
78
  if (!clientRouter) return;
78
79
 
@@ -98,22 +99,22 @@ export default ({ service: clientRouter }: { service?: ClientRouter }) => {
98
99
  clientRouter.setLoading(true);
99
100
  const newpage = context.page = await clientRouter.resolve(request);
100
101
 
101
- // Page not found: Directly load with the browser
102
- if (newpage === undefined) {
103
- window.location.replace(request.url);
104
- console.error("not found");
105
- return;
106
102
  // Unable to load (no connection, server error, ....)
107
- } else if (newpage === null) {
103
+ if (newpage === null) {
108
104
  return;
109
105
  }
110
106
 
107
+ return await changePage(newpage);
108
+ }
109
+
110
+ async function changePage(newpage: Page, request?: ClientRequest) {
111
+
111
112
  // Fetch API data to hydrate the page
112
113
  try {
113
114
  await newpage.preRender();
114
115
  } catch (error) {
115
116
  console.error(LogPrefix, "Unable to fetch data:", error);
116
- clientRouter.setLoading(false);
117
+ clientRouter?.setLoading(false);
117
118
  return;
118
119
  }
119
120
 
@@ -133,7 +134,7 @@ export default ({ service: clientRouter }: { service?: ClientRouter }) => {
133
134
  // But when we call setLayout, the style of the previous layout are still oaded and applied
134
135
  // Find a way to unload the previous layout / page resources before to load the new one
135
136
  console.log(LogPrefix, `Changing layout. Before:`, curLayout, 'New layout:', newLayout);
136
- window.location.replace(request.url);
137
+ window.location.replace( request ? request.url : location.href );
137
138
  return { ...page }
138
139
 
139
140
  context.app.setLayout(newLayout);
@@ -145,6 +145,7 @@ export default class ClientRouter<
145
145
  public context!: ClientContext;
146
146
 
147
147
  public setLoading!: React.Dispatch< React.SetStateAction<boolean> >;
148
+ public navigate!: (page: ClientPage) => void;
148
149
 
149
150
  public constructor(app: TApplication, config: Config<TAdditionnalContext>) {
150
151
 
@@ -161,10 +162,18 @@ export default class ClientRouter<
161
162
  public url = (path: string, params: {} = {}, absolute: boolean = true) =>
162
163
  buildUrl(path, params, this.domains, absolute);
163
164
 
164
- public go( url: string, data: {} = {}, opt: {
165
+ public go( url: string | number, data: {} = {}, opt: {
165
166
  newTab?: boolean
166
167
  } = {}) {
167
168
 
169
+ // Error code
170
+ if (typeof url === 'number') {
171
+ this.createResponse( this.errors[url], this.context.request ).then(( page ) => {
172
+ this.navigate(page);
173
+ })
174
+ return;
175
+ }
176
+
168
177
  url = this.url(url, data, false);
169
178
 
170
179
  if (opt.newTab)
@@ -304,7 +313,7 @@ export default class ClientRouter<
304
313
  /*----------------------------------
305
314
  - RESOLUTION
306
315
  ----------------------------------*/
307
- public async resolve(request: ClientRequest<this>): Promise<ClientPage | undefined | null> {
316
+ public async resolve(request: ClientRequest<this>): Promise<ClientPage> {
308
317
 
309
318
  debug && console.log(LogPrefix, 'Resolving request', request.path, Object.keys(request.data));
310
319
 
@@ -326,7 +335,10 @@ export default class ClientRouter<
326
335
 
327
336
  };
328
337
 
329
- return undefined;
338
+ console.log("404 error page not found.", this.errors, this.routes);
339
+
340
+ const notFoundRoute = this.errors[404];
341
+ return await this.createResponse(notFoundRoute, request);
330
342
  }
331
343
 
332
344
  private async load(route: TUnresolvedNormalRoute): Promise<TRoute>;
@@ -407,7 +419,7 @@ export default class ClientRouter<
407
419
  }
408
420
 
409
421
  private async createResponse(
410
- route: TUnresolvedRoute | TRoute,
422
+ route: TUnresolvedRoute | TErrorRoute | TRoute,
411
423
  request: ClientRequest<this>,
412
424
  pageData: {} = {}
413
425
  ): Promise<ClientPage> {