2020-11-17 13:08:24 +01:00
|
|
|
import { writable, derived } from "svelte/store"
|
|
|
|
import { routeStore } from "./routes"
|
2020-11-24 10:31:54 +01:00
|
|
|
import { builderStore } from "./builder"
|
2020-11-19 19:39:22 +01:00
|
|
|
import * as API from "../api"
|
2020-11-25 10:50:51 +01:00
|
|
|
import { getAppId } from "../utils/getAppId"
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2020-11-18 20:18:18 +01:00
|
|
|
const createScreenStore = () => {
|
2020-11-19 19:39:22 +01:00
|
|
|
const config = writable({
|
|
|
|
screens: [],
|
|
|
|
page: {},
|
|
|
|
})
|
2020-11-24 10:31:54 +01:00
|
|
|
const store = derived(
|
|
|
|
[config, routeStore, builderStore],
|
|
|
|
([$config, $routeStore, $builderStore]) => {
|
|
|
|
let page
|
|
|
|
let activeScreen
|
|
|
|
if ($builderStore.inBuilder) {
|
|
|
|
// Use builder defined definitions if inside the builder preview
|
|
|
|
page = $builderStore.page
|
|
|
|
activeScreen = $builderStore.screen
|
|
|
|
} else {
|
|
|
|
// Otherwise find the correct screen by matching the current route
|
|
|
|
page = $config.page
|
|
|
|
const { screens } = $config
|
|
|
|
if (screens.length === 1) {
|
|
|
|
activeScreen = screens[0]
|
|
|
|
} else {
|
|
|
|
activeScreen = screens.find(
|
2020-11-19 19:39:22 +01:00
|
|
|
screen => screen.routing.route === $routeStore.activeRoute
|
|
|
|
)
|
2020-11-24 10:31:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return { page, activeScreen }
|
2020-11-17 13:08:24 +01:00
|
|
|
}
|
2020-11-24 10:31:54 +01:00
|
|
|
)
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2020-11-19 19:39:22 +01:00
|
|
|
const fetchScreens = async () => {
|
2020-11-24 10:31:54 +01:00
|
|
|
const appDefinition = await API.fetchAppDefinition(getAppId())
|
|
|
|
config.set({
|
|
|
|
screens: appDefinition.screens,
|
|
|
|
page: appDefinition.page,
|
|
|
|
})
|
2020-11-13 16:42:32 +01:00
|
|
|
}
|
2020-11-17 13:08:24 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
|
|
|
actions: { fetchScreens },
|
2020-11-13 16:42:32 +01:00
|
|
|
}
|
2020-11-17 13:08:24 +01:00
|
|
|
}
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2020-11-18 20:18:18 +01:00
|
|
|
export const screenStore = createScreenStore()
|