Type routes
This commit is contained in:
parent
6e816fb6a2
commit
1b6dc51f01
|
@ -4,8 +4,24 @@ import { API } from "api"
|
||||||
import { peekStore } from "./peek"
|
import { peekStore } from "./peek"
|
||||||
import { builderStore } from "./builder"
|
import { builderStore } from "./builder"
|
||||||
|
|
||||||
|
interface Route {
|
||||||
|
path: string
|
||||||
|
screenId: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface StoreType {
|
||||||
|
routes: Route[]
|
||||||
|
routeParams: {}
|
||||||
|
activeRoute?: Route | null
|
||||||
|
routeSessionId: number
|
||||||
|
routerLoaded: boolean
|
||||||
|
queryParams?: {
|
||||||
|
peek?: boolean
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const createRouteStore = () => {
|
const createRouteStore = () => {
|
||||||
const initialState = {
|
const initialState: StoreType = {
|
||||||
routes: [],
|
routes: [],
|
||||||
routeParams: {},
|
routeParams: {},
|
||||||
activeRoute: null,
|
activeRoute: null,
|
||||||
|
@ -22,7 +38,7 @@ const createRouteStore = () => {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
routeConfig = null
|
routeConfig = null
|
||||||
}
|
}
|
||||||
let routes = []
|
const routes: Route[] = []
|
||||||
Object.values(routeConfig?.routes || {}).forEach(route => {
|
Object.values(routeConfig?.routes || {}).forEach(route => {
|
||||||
Object.entries(route.subpaths || {}).forEach(([path, config]) => {
|
Object.entries(route.subpaths || {}).forEach(([path, config]) => {
|
||||||
routes.push({
|
routes.push({
|
||||||
|
@ -43,13 +59,13 @@ const createRouteStore = () => {
|
||||||
return state
|
return state
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const setRouteParams = routeParams => {
|
const setRouteParams = (routeParams: StoreType["routeParams"]) => {
|
||||||
store.update(state => {
|
store.update(state => {
|
||||||
state.routeParams = routeParams
|
state.routeParams = routeParams
|
||||||
return state
|
return state
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const setQueryParams = queryParams => {
|
const setQueryParams = (queryParams: { peek?: boolean }) => {
|
||||||
store.update(state => {
|
store.update(state => {
|
||||||
state.queryParams = {
|
state.queryParams = {
|
||||||
...queryParams,
|
...queryParams,
|
||||||
|
@ -60,13 +76,13 @@ const createRouteStore = () => {
|
||||||
return state
|
return state
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const setActiveRoute = route => {
|
const setActiveRoute = (route: string) => {
|
||||||
store.update(state => {
|
store.update(state => {
|
||||||
state.activeRoute = state.routes.find(x => x.path === route)
|
state.activeRoute = state.routes.find(x => x.path === route)
|
||||||
return state
|
return state
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const navigate = (url, peek, externalNewTab) => {
|
const navigate = (url: string, peek: boolean, externalNewTab: boolean) => {
|
||||||
if (get(builderStore).inBuilder) {
|
if (get(builderStore).inBuilder) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -93,7 +109,7 @@ const createRouteStore = () => {
|
||||||
const setRouterLoaded = () => {
|
const setRouterLoaded = () => {
|
||||||
store.update(state => ({ ...state, routerLoaded: true }))
|
store.update(state => ({ ...state, routerLoaded: true }))
|
||||||
}
|
}
|
||||||
const createFullURL = relativeURL => {
|
const createFullURL = (relativeURL: string) => {
|
||||||
if (!relativeURL?.startsWith("/")) {
|
if (!relativeURL?.startsWith("/")) {
|
||||||
return relativeURL
|
return relativeURL
|
||||||
}
|
}
|
|
@ -36,6 +36,7 @@ export type ScreenRoutingJson = Record<
|
||||||
subpaths: Record<
|
subpaths: Record<
|
||||||
string,
|
string,
|
||||||
{
|
{
|
||||||
|
screenId: string
|
||||||
screens: Record<string, string>
|
screens: Record<string, string>
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
|
|
Loading…
Reference in New Issue