2020-11-13 16:42:32 +01:00
|
|
|
import { writable } from "svelte/store"
|
|
|
|
import { push } from "svelte-spa-router"
|
2020-11-19 18:55:40 +01:00
|
|
|
import * as API from "../api"
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2020-11-18 20:18:18 +01:00
|
|
|
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)
|
|
|
|
|
2020-11-19 18:55:40 +01:00
|
|
|
const fetchRoutes = async () => {
|
|
|
|
const routeConfig = await API.fetchRoutes()
|
|
|
|
let routes = {}
|
|
|
|
Object.values(routeConfig.routes).forEach(pathConfig => {
|
|
|
|
Object.entries(config).forEach(([subPath, subPathConfig]) => {})
|
|
|
|
})
|
|
|
|
|
|
|
|
console.log(routes2)
|
|
|
|
|
2020-11-13 16:42:32 +01:00
|
|
|
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 => {
|
|
|
|
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 },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 20:18:18 +01:00
|
|
|
export const routeStore = createRouteStore()
|