mealie/frontend/composables/use-notifications.ts
Hayden 788e176b16
Refactor/composables-folder (#787)
* move api clients and rename

* organize recipes composables

* rewrite useRecipeContext

* refactor(frontend): ♻️ abstract common ingredient functionality.

* feat(frontend):  add scale, and back to recipe button + hide ingredients if none

* update regex to mach 11. instead of just 1.

* minor UX improvements

Co-authored-by: Hayden K <hay-kot@pm.me>
2021-11-06 11:28:47 -08:00

90 lines
2.3 KiB
TypeScript

import { useAsync, ref } from "@nuxtjs/composition-api";
import { useAsyncKey } from "./use-utils";
import { CreateEventNotification } from "@/api/class-interfaces/event-notifications";
import { useUserApi } from "~/composables/api";
const notificationTypes = ["General", "Discord", "Gotify", "Pushover", "Home Assistant"];
export const useNotifications = function () {
const api = useUserApi();
const loading = ref(false);
const createNotificationData = ref<CreateEventNotification>({
name: "",
type: "General",
general: true,
recipe: true,
backup: true,
scheduled: true,
migration: true,
group: true,
user: true,
notificationUrl: "",
});
const deleteTarget = ref(0);
function getNotifications() {
loading.value = true;
const notifications = useAsync(async () => {
const { data } = await api.notifications.getAll();
return data;
}, useAsyncKey());
loading.value = false;
return notifications;
}
async function refreshNotifications() {
loading.value = true;
const { data } = await api.notifications.getAll();
if (data) {
notifications.value = data;
}
loading.value = false;
}
async function createNotification() {
if (createNotificationData.value.name === "" || createNotificationData.value.notificationUrl === "") {
return;
}
const { response } = await api.notifications.createOne(createNotificationData.value);
if (response?.status === 200) {
refreshNotifications();
}
}
async function deleteNotification() {
const { response } = await api.notifications.deleteOne(deleteTarget.value);
if (response?.status === 200) {
refreshNotifications();
}
}
async function testById(id: number) {
const { data } = await api.notifications.testNotification(id, null);
console.log(data);
}
async function testByUrl(testUrl: string) {
const { data } = await api.notifications.testNotification(null, testUrl);
console.log(data);
}
const notifications = getNotifications();
return {
createNotification,
deleteNotification,
refreshNotifications,
getNotifications,
testById,
testByUrl,
notifications,
loading,
createNotificationData,
notificationTypes,
deleteTarget,
};
};