Refactor store actions to allow self reference

This commit is contained in:
Rory Powell 2021-12-08 11:01:12 +00:00
parent 52ea1fe1da
commit 4f642a03a4
3 changed files with 11 additions and 8 deletions

View File

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

View File

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

View File

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