2020-11-13 16:42:32 +01:00
|
|
|
import { writable } from "svelte/store"
|
|
|
|
import { push } from "svelte-spa-router"
|
|
|
|
|
|
|
|
export const createRouteStore = () => {
|
2020-11-17 13:08:24 +01:00
|
|
|
const initialState = {
|
|
|
|
routes: [],
|
|
|
|
routeParams: {},
|
|
|
|
activeRoute: null,
|
|
|
|
}
|
2020-11-13 16:42:32 +01:00
|
|
|
const store = writable(initialState)
|
|
|
|
|
|
|
|
const fetchRoutes = () => {
|
|
|
|
const frontendDefinition = window["##BUDIBASE_FRONTEND_DEFINITION##"]
|
|
|
|
const routes = frontendDefinition.screens.map(screen => ({
|
|
|
|
path: screen.route,
|
|
|
|
screenId: screen._id,
|
|
|
|
}))
|
2020-11-17 13:08:24 +01:00
|
|
|
store.update(state => {
|
|
|
|
state.routes = routes
|
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
|
|
|
const setRouteParams = routeParams => {
|
|
|
|
console.log("new route params: ")
|
|
|
|
console.log(routeParams)
|
|
|
|
store.update(state => {
|
|
|
|
state.routeParams = routeParams
|
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
|
|
|
const setActiveRoute = route => {
|
|
|
|
store.update(state => {
|
|
|
|
state.activeRoute = route
|
|
|
|
return state
|
|
|
|
})
|
2020-11-13 16:42:32 +01:00
|
|
|
}
|
|
|
|
const navigate = push
|
|
|
|
|
2020-11-17 13:08:24 +01:00
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
|
|
|
actions: { fetchRoutes, navigate, setRouteParams, setActiveRoute },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!window.bbSDKRouteStore) {
|
|
|
|
window.bbSDKRouteStore = createRouteStore()
|
2020-11-13 16:42:32 +01:00
|
|
|
}
|
2020-11-17 13:08:24 +01:00
|
|
|
|
|
|
|
export const routeStore = window.bbSDKRouteStore
|