1
0
Fork 0
mirror of synced 2024-10-03 02:27:06 +13:00

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