UNPKG

@eotl/core

Version:

Assortment of data structures, Vue.js components, and utilities across EOTL apps and sites.

264 lines (263 loc) 7.18 kB
import { defineStore } from "pinia"; import "vue"; const useEotlCore = defineStore("eotlCore", { state: () => ({ config: { mapboxToken: "", url_sextant: "", url_api: "", api_ending: "" }, alert: { show: false, style: "primary", title: "Hello", message: "This is just a default status message. Nothing to see here" }, activeMapItemByAddress: null }), actions: { init(data) { this.config.mapboxToken = (data == null ? void 0 : data.mapboxToken) || ""; this.config.url_sextant = (data == null ? void 0 : data.url_sextant) || ""; this.config.url_api = (data == null ? void 0 : data.url_api) || ""; this.config.api_ending = (data == null ? void 0 : data.api_ending) || ""; }, // ALERT alertMsg(alert) { this.alert.message = (alert == null ? void 0 : alert.message) || ""; this.alert.show = (alert == null ? void 0 : alert.show) || false; this.alert.style = (alert == null ? void 0 : alert.style) || ""; this.alert.title = (alert == null ? void 0 : alert.title) || ""; }, alertReset() { this.alert.message = ""; this.alert.show = false; this.alert.style = ""; this.alert.title = ""; }, alertPrimary: function(message, title) { this.alertMsg({ show: true, style: "primary", title: title || "Notification", message }); }, alertSuccess: function(message, title) { this.alertMsg({ show: true, style: "success", title: title || "Success", message }); }, alertInfo: function(message, title) { this.alertMsg({ show: true, style: "info", title: title || "Info", message }); }, alertWarning: function(message, title) { this.alertMsg({ show: true, style: "warning", title: title || "Warning", message }); }, alertDanger: function(message, title) { this.alertMsg({ show: true, style: "danger", title: title || "Error", message }); }, // API async fetchGet(endpoint, queryString = "") { let data = null; let pending = true; let errorMessage = null; let status = "pending"; const api_request = this.config.url_api + endpoint + this.config.api_ending + queryString; try { const response = await fetch(api_request, { method: "GET", headers: { "Accept": "application/json", "Content-Type": "application/json" } }); if (!response.ok) { const resError = await response.text(); const errorMessage2 = JSON.parse(resError); this.alertDanger(errorMessage2.detail); throw new Error(errorMessage2.detail); } const text = await response.text(); data = JSON.parse(text); status = "success"; } catch (error) { this.alertWarning(error); errorMessage = error; status = "error"; return { status, error: errorMessage, pending, data }; } finally { pending = false; status = "success"; } return { status, pending, data, error: errorMessage }; }, async fetchPost(endpoint, record) { let data = null; let pending = true; let errorMessage = null; let status = "pending"; const api_request = this.config.url_api + endpoint + this.config.api_ending; try { const response = await fetch(api_request, { method: "POST", headers: { "Accept": "application/json", "Content-Type": "application/json" }, body: JSON.stringify(record) }); if (!response.ok) { const resError = await response.text(); const errorMessage2 = JSON.parse(resError); this.alertDanger(errorMessage2.detail); throw new Error(errorMessage2.detail); } const text = await response.text(); data = JSON.parse(text); status = "success"; } catch (error) { this.alertWarning(error); errorMessage = error; status = "error"; return { status, error: errorMessage, pending, data }; } finally { pending = false; status = "success"; } return { status, pending, data, error: errorMessage }; }, async fetchPatch(endpoint, record) { let data = null; let pending = true; let errorMessage = null; let status = "pending"; const api_request = this.config.url_api + endpoint + this.config.api_ending; try { const response = await fetch(api_request, { method: "PATCH", headers: { "Accept": "application/json", "Content-Type": "application/json" }, body: JSON.stringify(record) }); if (!response.ok) { const resError = await response.text(); const errorMessage2 = JSON.parse(resError); this.alertDanger(errorMessage2.detail); throw new Error(errorMessage2.detail); } const text = await response.text(); data = JSON.parse(text); status = "success"; } catch (error) { this.alertWarning(error); errorMessage = error; status = "error"; return { status, error: errorMessage, pending, data }; } finally { pending = false; status = "success"; } return { status, pending, data, error: errorMessage }; }, async fetchDelete(endpoint) { let data = null; let pending = true; let errorMessage = null; let status = "pending"; const api_request = this.config.url_api + endpoint + this.config.api_ending; try { const response = await fetch(api_request, { method: "DELETE", headers: { "Accept": "application/json", "Content-Type": "application/json" } }); if (!response.ok) { const resError = await response.text(); const errorMessage2 = JSON.parse(resError); this.alertDanger(errorMessage2.detail); throw new Error(errorMessage2.detail); } const text = await response.text(); data = JSON.parse(text); status = "success"; } catch (error) { this.alertWarning(error); errorMessage = error; status = "error"; return { status, error: errorMessage, pending, data }; } finally { pending = false; status = "success"; } return { status, pending, data, error: errorMessage }; } } }); export { useEotlCore }; //# sourceMappingURL=store.js.map