From d0f2c6d6994cbd7c673b526ae4d669b6bf14138b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 6 Mar 2023 12:17:02 +0000 Subject: [PATCH 1/2] Fix not enhanced route modules --- resources/ts/modules/route.ts | 97 ++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 47 deletions(-) diff --git a/resources/ts/modules/route.ts b/resources/ts/modules/route.ts index 2ddc37b..955b9aa 100644 --- a/resources/ts/modules/route.ts +++ b/resources/ts/modules/route.ts @@ -1,73 +1,76 @@ const route = { methods: { /** - * Define a route by the data given, - * ex: route('name', { 'parameter': 'value' }). + * Same behavior as laravel route + * + * ! window.__SYSTEM._routes has to be defined + * + * @param routeName The route name. + * @param parameters Parameters, if a parameter name matches a route path level, + * it will be replaced, otherwise it will appear in query. + * @param keepActualQueries Pass this to keep current url queries. + * @param keepActualHashes Pass this to keep current url fragments + * @returns The newly formated string */ route( routeName: string, parameters: Record< string, string | number | Array - > = {} + > = {}, + keepActualQueries: boolean = false, + keepActualHashes: boolean = false ): string | null { const routes = route.flattenProperties(window.__SYSTEM._routes); const routeUrl = Object.keys(routes).indexOf(routeName) !== -1 ? (routes[routeName] as string) : null; + + // * Return null if route url could not be found if (!routeUrl) { - return routeUrl; + return null; } - // flatten parameter array - for (const parameterIndex in parameters) { - const parameter = parameters[parameterIndex]; - if (!Array.isArray(parameter)) { - continue; + + const queryParameters: Record = {}, + url = new URL(window.location.origin), + searchParams = new URL(String(window.location)).searchParams, + hash = window.location.hash; + let pathname = routeUrl.replace(origin, ""); + + // * Replace parameters in path name. + for (const paramName in parameters) { + const paramValue = String(parameters[paramName]); + if (pathname.indexOf(paramName) !== -1) { + // * Replace pathname part with parameter + pathname = pathname.replace(paramName, paramValue); + } else { + // * push parameter to be in queries + queryParameters[paramName] = paramValue; } - delete parameters[parameterIndex]; - parameter.forEach( - (parameter, index) => - (parameters[`${parameterIndex}[${index}]`] = parameter) + } + url.pathname = pathname; + + // * Build query + if (keepActualQueries) { + // * Push actual queries + for (const param of searchParams) { + url.searchParams.set(param[0], param[1]); + } + } + // * Push request queries + for (const paramName in queryParameters) { + url.searchParams.set( + paramName, + String(queryParameters[paramName]) ); - // still append empty entry if empty array - if (!parameter.length) { - parameters[`${parameterIndex}`] = ""; - } } - const flattenParameters = parameters as Record< - string, - string | number - >; - - // copy parameters - const iParameters = Object.assign({}, flattenParameters), - origin = window.location.origin, - url = new URL( - `${origin}${routeUrl.replace(origin, "")}${ - window.location.search - }${window.location.hash}` - ); - - // Replace in path name. - for (const paramName in iParameters) { - const paramValue = flattenParameters[paramName]; - if (url.pathname.indexOf(paramName) !== -1) { - url.pathname = url.pathname.replace( - paramName, - paramValue as string - ); - delete flattenParameters[paramName]; - } + if (keepActualHashes) { + url.hash = hash; } - // Append remaining parameters to url - for (const paramName in flattenParameters) { - const paramValue = flattenParameters[paramName]; - url.searchParams.set(paramName, String(paramValue)); - } - return url.toString(); + return String(url); }, }, flattenProperties( From 78e7e57f97166fb70e397ea2336a94c40619b3db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 6 Mar 2023 12:25:47 +0000 Subject: [PATCH 2/2] fix ts bs import --- resources/ts/utilities/bootstrap.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/resources/ts/utilities/bootstrap.ts b/resources/ts/utilities/bootstrap.ts index e4323f0..0cdbcf9 100644 --- a/resources/ts/utilities/bootstrap.ts +++ b/resources/ts/utilities/bootstrap.ts @@ -1,8 +1,7 @@ import axios from "axios"; +import * as bootstrap from "bootstrap"; import bsTooltips from "../modules/bsTooltips"; -const bootstrap = require("bootstrap"); - /** * We'll load the axios HTTP library which allows us to easily issue requests * to our Laravel back-end. This library automatically handles sending the