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