Tidy up screen replacement logic

This commit is contained in:
Andrew Kingston 2023-07-05 10:02:46 +01:00
parent 86986b17d6
commit 21cbc751a5
1 changed files with 16 additions and 18 deletions

View File

@ -358,30 +358,28 @@ export const getFrontendStore = () => {
if (!screenId) {
return
}
// Handle deletion
if (!screen) {
// Screen deletion
store.update(state => ({
...state,
screens: state.screens.filter(x => x._id !== screenId),
}))
return
} else {
const index = get(store).screens.findIndex(x => x._id === screen._id)
if (index === -1) {
// Screen addition
store.update(state => ({
...state,
screens: [...state.screens, screen],
}))
} else {
// Screen update
store.update(state => {
state.screens[index] = screen
return state
})
}
}
// Add new datasource
const index = get(store).screens.findIndex(x => x._id === screen._id)
if (index === -1) {
store.update(state => ({
...state,
screens: [...state.screens, screen],
}))
}
// Update existing datasource
store.update(state => {
state.screens[index] = screen
return state
})
},
delete: async screens => {
const screensToDelete = Array.isArray(screens) ? screens : [screens]