Refactor store actions to allow self reference

This commit is contained in:
Rory Powell 2021-12-08 11:01:12 +00:00
parent 415462321d
commit 3bd93f2755
3 changed files with 11 additions and 8 deletions

View File

@ -14,7 +14,7 @@
async function duplicateQuery() { async function duplicateQuery() {
try { try {
await queries.duplicate(query, queries.save) await queries.duplicate(query)
} catch (e) { } catch (e) {
notifications.error(e.message) notifications.error(e.message)
} }

View File

@ -13,10 +13,7 @@ export function createQueriesStore() {
const store = writable({ list: [], selected: null }) const store = writable({ list: [], selected: null })
const { subscribe, set, update } = store const { subscribe, set, update } = store
return { const actions = {
subscribe,
set,
update,
init: async () => { init: async () => {
const response = await api.get(`/api/queries`) const response = await api.get(`/api/queries`)
const json = await response.json() const json = await response.json()
@ -86,7 +83,7 @@ export function createQueriesStore() {
}) })
return response return response
}, },
duplicate: async (query, saveFn) => { duplicate: async query => {
let list = get(store).list let list = get(store).list
const newQuery = { ...query } const newQuery = { ...query }
const datasourceId = query.datasourceId const datasourceId = query.datasourceId
@ -98,9 +95,16 @@ export function createQueriesStore() {
list.map(q => q.name) list.map(q => q.name)
) )
saveFn(datasourceId, newQuery) actions.save(datasourceId, newQuery)
}, },
} }
return {
subscribe,
set,
update,
...actions,
}
} }
export const queries = createQueriesStore() export const queries = createQueriesStore()

View File

@ -6,7 +6,6 @@ jest.mock('builderStore/api');
import { SOME_QUERY, SAVE_QUERY_RESPONSE } from './fixtures/queries' import { SOME_QUERY, SAVE_QUERY_RESPONSE } from './fixtures/queries'
import { createQueriesStore } from "../queries" import { createQueriesStore } from "../queries"
import { datasources } from '../datasources'
describe("Queries Store", () => { describe("Queries Store", () => {
let store = createQueriesStore() let store = createQueriesStore()