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 +1 -1
- package/src/client/assets/css/text/text.less +0 -5
- package/src/client/components/inputv3/index.tsx +1 -1
- package/src/client/services/router/components/router.tsx +18 -13
- package/src/client/services/router/index.tsx +16 -4
- package/src/client/services/router/request/index.ts +0 -1
- package/src/common/router/request/index.ts +1 -0
- package/src/server/services/router/request/index.ts +1 -0
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-
|
|
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",
|
|
@@ -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
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
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> {
|