extract out select and fetch methods in tables store
This commit is contained in:
parent
4eb0a505bd
commit
6afd886755
|
@ -1,4 +1,5 @@
|
||||||
import { writable } from "svelte/store"
|
import { writable, get } from "svelte/store"
|
||||||
|
import { views } from './'
|
||||||
import { cloneDeep } from "lodash/fp"
|
import { cloneDeep } from "lodash/fp"
|
||||||
import api from "../../api"
|
import api from "../../api"
|
||||||
|
|
||||||
|
@ -7,33 +8,38 @@ function createTablesStore() {
|
||||||
list: [],
|
list: [],
|
||||||
selected: {},
|
selected: {},
|
||||||
draft: {},
|
draft: {},
|
||||||
view: {}
|
|
||||||
})
|
})
|
||||||
const { subscribe, update, set } = store
|
const { subscribe, update, set } = store
|
||||||
|
|
||||||
return {
|
async function fetch() {
|
||||||
subscribe,
|
|
||||||
set,
|
|
||||||
fetch: async () => {
|
|
||||||
const tablesResponse = await api.get(`/api/tables`)
|
const tablesResponse = await api.get(`/api/tables`)
|
||||||
const tables = await tablesResponse.json()
|
const tables = await tablesResponse.json()
|
||||||
update(state => ({...state, list: tables}))
|
update(state => ({...state, list: tables}))
|
||||||
},
|
}
|
||||||
select: table => {
|
|
||||||
|
async function select(table) {
|
||||||
if (!table) {
|
if (!table) {
|
||||||
|
console.log('Setting selected to null')
|
||||||
update(state => ({
|
update(state => ({
|
||||||
...state,
|
...state,
|
||||||
selected: {}
|
selected: {}
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
|
console.log('Setting selected to null')
|
||||||
update(state => ({
|
update(state => ({
|
||||||
...state,
|
...state,
|
||||||
selected: table,
|
selected: table,
|
||||||
draft: cloneDeep(table),
|
draft: cloneDeep(table),
|
||||||
view: { name: `all_${table._id}` }
|
|
||||||
}))
|
}))
|
||||||
|
views.select({ name: `all_${table._id}` })
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
subscribe,
|
||||||
|
set,
|
||||||
|
fetch,
|
||||||
|
select,
|
||||||
save: async table => {
|
save: async table => {
|
||||||
const updatedTable = cloneDeep(table)
|
const updatedTable = cloneDeep(table)
|
||||||
const oldTable = get(store).list.filter(t => t._id === table._id)[0]
|
const oldTable = get(store).list.filter(t => t._id === table._id)[0]
|
||||||
|
@ -64,8 +70,8 @@ function createTablesStore() {
|
||||||
|
|
||||||
const response = await api.post(`/api/tables`, updatedTable)
|
const response = await api.post(`/api/tables`, updatedTable)
|
||||||
const savedTable = await response.json()
|
const savedTable = await response.json()
|
||||||
await store.fetch()
|
await fetch()
|
||||||
await store.select(savedTable)
|
await select(savedTable)
|
||||||
return savedTable
|
return savedTable
|
||||||
},
|
},
|
||||||
delete: async table => {
|
delete: async table => {
|
||||||
|
|
Loading…
Reference in New Issue