5htp-core 0.4.4-3 → 0.4.4-5

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-5",
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;
@@ -74,7 +74,7 @@ export default ({
74
74
  if (refCommit.current !== null)
75
75
  clearTimeout(refCommit.current);
76
76
 
77
- refCommit.current = setTimeout(commitValue, 500);
77
+ refCommit.current = setTimeout(commitValue, 100);
78
78
 
79
79
  }, [value]);
80
80
 
@@ -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
 
@@ -95,25 +96,29 @@ export default ({ service: clientRouter }: { service?: ClientRouter }) => {
95
96
 
96
97
  // Set loading state
97
98
  clientRouter.runHook('page.change', request);
99
+ window.scrollTo({
100
+ top: 0,
101
+ behavior: 'smooth'
102
+ });
98
103
  clientRouter.setLoading(true);
99
104
  const newpage = context.page = await clientRouter.resolve(request);
100
105
 
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
106
  // Unable to load (no connection, server error, ....)
107
- } else if (newpage === null) {
107
+ if (newpage === null) {
108
108
  return;
109
109
  }
110
110
 
111
+ return await changePage(newpage);
112
+ }
113
+
114
+ async function changePage(newpage: Page, request?: ClientRequest) {
115
+
111
116
  // Fetch API data to hydrate the page
112
117
  try {
113
118
  await newpage.preRender();
114
119
  } catch (error) {
115
120
  console.error(LogPrefix, "Unable to fetch data:", error);
116
- clientRouter.setLoading(false);
121
+ clientRouter?.setLoading(false);
117
122
  return;
118
123
  }
119
124
 
@@ -133,7 +138,7 @@ export default ({ service: clientRouter }: { service?: ClientRouter }) => {
133
138
  // But when we call setLayout, the style of the previous layout are still oaded and applied
134
139
  // Find a way to unload the previous layout / page resources before to load the new one
135
140
  console.log(LogPrefix, `Changing layout. Before:`, curLayout, 'New layout:', newLayout);
136
- window.location.replace(request.url);
141
+ window.location.replace( request ? request.url : location.href );
137
142
  return { ...page }
138
143
 
139
144
  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> {
@@ -27,7 +27,6 @@ export default class ClientRequest<TRouter extends ClientRouter = ClientRouter>
27
27
  public api: ApiClient;
28
28
  public response?: ClientResponse<TRouter>;
29
29
 
30
- public url: string;
31
30
  public hash?: string;
32
31
 
33
32
  public constructor(
@@ -17,6 +17,7 @@ export default abstract class BaseRequest {
17
17
 
18
18
  // Permet d'accèder à l'instance complète via spread
19
19
  public request: this = this;
20
+ public url!: string;
20
21
  public host!: string;
21
22
 
22
23
  public data: TObjetDonnees = {};
@@ -87,6 +87,7 @@ export default class ServerRequest<
87
87
  this.router = router;
88
88
  this.api = new ApiClient(this);
89
89
 
90
+ this.url = this.req.url;
90
91
  this.host = this.req.get('host') as string;
91
92
  this.method = method;
92
93
  this.headers = headers || {};