From ae2148e48172425e77ac458b16247e8e8bf39351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Keviin=20=C3=85berg=20Kultalahti?= Date: Fri, 7 May 2021 14:30:51 +0200 Subject: [PATCH] creates a custom helper store for fetching data --- packages/builder/src/helperStores.js | 20 ++++++++++++++++++++ packages/builder/vite.config.js | 4 ++++ 2 files changed, 24 insertions(+) create mode 100644 packages/builder/src/helperStores.js diff --git a/packages/builder/src/helperStores.js b/packages/builder/src/helperStores.js new file mode 100644 index 0000000000..f4eb2020c4 --- /dev/null +++ b/packages/builder/src/helperStores.js @@ -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] +} \ No newline at end of file diff --git a/packages/builder/vite.config.js b/packages/builder/vite.config.js index 69b5730b5b..77d1eaa7bb 100644 --- a/packages/builder/vite.config.js +++ b/packages/builder/vite.config.js @@ -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"),