2022-01-20 10:40:53 +01:00
|
|
|
import { API } from "../api"
|
2021-06-30 21:35:02 +02:00
|
|
|
import { get, writable } from "svelte/store"
|
|
|
|
|
|
|
|
const createAppStore = () => {
|
2022-01-20 10:40:53 +01:00
|
|
|
const store = writable(null)
|
2021-06-30 21:35:02 +02:00
|
|
|
|
|
|
|
// Fetches the app definition including screens, layouts and theme
|
|
|
|
const fetchAppDefinition = async () => {
|
2021-07-25 14:43:07 +02:00
|
|
|
const appId = get(store)?.appId
|
|
|
|
if (!appId) {
|
|
|
|
throw "Cannot fetch app definition without app ID set"
|
|
|
|
}
|
2022-01-20 10:40:53 +01:00
|
|
|
try {
|
|
|
|
const appDefinition = await API.fetchAppPackage(appId)
|
|
|
|
store.set({
|
|
|
|
...appDefinition,
|
|
|
|
appId: appDefinition?.application?.appId,
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
store.set(null)
|
|
|
|
}
|
2021-06-30 21:35:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the initial app ID
|
|
|
|
const setAppID = id => {
|
|
|
|
store.update(state => {
|
2022-01-20 10:40:53 +01:00
|
|
|
if (state) {
|
|
|
|
state.appId = id
|
|
|
|
} else {
|
|
|
|
state = { appId: id }
|
|
|
|
}
|
2021-06-30 21:35:02 +02:00
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
|
|
|
actions: { setAppID, fetchAppDefinition },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const appStore = createAppStore()
|