2020-11-17 13:08:24 +01:00
|
|
|
import { writable, derived } from "svelte/store"
|
|
|
|
import { routeStore } from "./routes"
|
2020-11-19 19:39:22 +01:00
|
|
|
import * as API from "../api"
|
|
|
|
import { getAppId } from "../utils"
|
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: {},
|
|
|
|
})
|
|
|
|
const store = derived([config, routeStore], ([$config, $routeStore]) => {
|
|
|
|
const { screens, page } = $config
|
2020-11-18 22:06:12 +01:00
|
|
|
const activeScreen =
|
2020-11-19 19:39:22 +01:00
|
|
|
screens.length === 1
|
|
|
|
? screens[0]
|
|
|
|
: screens.find(
|
|
|
|
screen => screen.routing.route === $routeStore.activeRoute
|
|
|
|
)
|
2020-11-17 13:08:24 +01:00
|
|
|
return {
|
2020-11-19 19:39:22 +01:00
|
|
|
screens,
|
|
|
|
page,
|
2020-11-17 13:08:24 +01:00
|
|
|
activeScreen,
|
|
|
|
}
|
2020-11-13 16:42:32 +01:00
|
|
|
})
|
|
|
|
|
2020-11-19 19:39:22 +01:00
|
|
|
const fetchScreens = async () => {
|
|
|
|
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()
|