creates a custom helper store for fetching data

This commit is contained in:
Keviin Åberg Kultalahti 2021-05-07 14:30:51 +02:00
parent cb78e68d99
commit ae2148e481
2 changed files with 24 additions and 0 deletions

View File

@ -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]
}

View File

@ -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"),