creates a custom helper store for fetching data
This commit is contained in:
parent
cb78e68d99
commit
ae2148e481
|
@ -0,0 +1,20 @@
|
|||
import { writable } from 'svelte/store'
|
||||
import api from "builderStore/api"
|
||||
|
||||
export function fetchData (url) {
|
||||
const store = writable({status: 'LOADING', data: {}, error: {}})
|
||||
|
||||
async function get() {
|
||||
store.update(u => ({...u, status: 'SUCCESS'}))
|
||||
try {
|
||||
const response = await api.get(url)
|
||||
store.set({data: await response.json(), status: 'SUCCESS'})
|
||||
} catch(e) {
|
||||
store.set({data: {}, error: e, status: 'ERROR'})
|
||||
}
|
||||
}
|
||||
|
||||
get()
|
||||
|
||||
return [store, get]
|
||||
}
|
|
@ -56,6 +56,10 @@ export default ({ mode }) => {
|
|||
find: "actions",
|
||||
replacement: path.resolve("./src/actions"),
|
||||
},
|
||||
{
|
||||
find: "helperStores",
|
||||
replacement: path.resolve("./src/helperStores"),
|
||||
},
|
||||
{
|
||||
find: "helpers",
|
||||
replacement: path.resolve("./src/helpers"),
|
||||
|
|
Loading…
Reference in New Issue