2021-06-21 10:56:46 +02:00
|
|
|
import { writable, get } from "svelte/store"
|
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
showConfirmation: false,
|
2021-06-21 11:46:55 +02:00
|
|
|
title: null,
|
2021-06-21 10:56:46 +02:00
|
|
|
text: null,
|
|
|
|
callback: null,
|
|
|
|
}
|
|
|
|
|
|
|
|
const createConfirmationStore = () => {
|
|
|
|
const store = writable(initialState)
|
|
|
|
|
2021-06-21 11:46:55 +02:00
|
|
|
const showConfirmation = (title, text, callback) => {
|
2021-06-21 10:56:46 +02:00
|
|
|
store.set({
|
|
|
|
showConfirmation: true,
|
2021-06-21 11:46:55 +02:00
|
|
|
title,
|
2021-06-21 10:56:46 +02:00
|
|
|
text,
|
|
|
|
callback,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
const confirm = async () => {
|
|
|
|
const state = get(store)
|
|
|
|
if (!state.showConfirmation || !state.callback) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
store.set(initialState)
|
|
|
|
await state.callback()
|
|
|
|
}
|
|
|
|
const cancel = () => {
|
|
|
|
store.set(initialState)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
|
|
|
actions: { showConfirmation, confirm, cancel },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const confirmationStore = createConfirmationStore()
|