From e910ead778144bf4ff81ff15617ba3f5b0a110a7 Mon Sep 17 00:00:00 2001 From: ingrid <ingrimeg@stud.ntnu.no> Date: Fri, 21 Apr 2023 09:39:17 +0200 Subject: [PATCH] API --- src/stores/useFoodPreferenceStore.js | 21 +++++++++++++++++++++ src/util/API.js | 15 +++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/stores/useFoodPreferenceStore.js create mode 100644 src/util/API.js diff --git a/src/stores/useFoodPreferenceStore.js b/src/stores/useFoodPreferenceStore.js new file mode 100644 index 0000000..028f2cd --- /dev/null +++ b/src/stores/useFoodPreferenceStore.js @@ -0,0 +1,21 @@ +import {API} from "@/util/API" +import { defineStore} from "pinia"; + +export const useFoodPreferenceStore = defineStore("foodPreference", { + state: () => { + return { + foodPreferences: [], + }; + }, + persist: { + storage: localStorage, + }, + actions: { + fetchAllOptions() { + API.getFoodpreferences() + .then((foodPreferences) => { + this.foodPreferences = foodPreferences; + }) + } + } +}) \ No newline at end of file diff --git a/src/util/API.js b/src/util/API.js new file mode 100644 index 0000000..aeb8780 --- /dev/null +++ b/src/util/API.js @@ -0,0 +1,15 @@ +import axios from "axios"; +export const API = { + /** + * Fetches all available foodpreference options + */ + getFoodpreferences(){ + return axios.get(`${import.meta.env.VITE_BACKEND_URL}/foodpreferences`) + .then((response) => { + return response.data; + }).catch(() => { + throw new Error(); + }); + } + +} -- GitLab