mirror of
				https://github.com/mealie-recipes/mealie.git
				synced 2025-11-04 03:28:28 -05:00 
			
		
		
		
	* fix type errors on event bus * webhooks fields required for new implementation * db migration * wip: webhook query + tests and stub function * ignore type checker error * type and method cleanup * datetime and time utc validator * update testing code for utc scheduled time * fix file cmp function call * update version_number * add support for translating "time" objects when restoring backup * bump recipe-scrapers * use specific import syntax * generate frontend types * utilize names exports * use utc times * add task to scheduler * implement new scheduler functionality * stub for type annotation * implement meal-plan data getter * add experimental banner
		
			
				
	
	
		
			113 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import { useAsync, ref } from "@nuxtjs/composition-api";
 | 
						|
import { useAsyncKey } from "./use-utils";
 | 
						|
import { useUserApi } from "~/composables/api";
 | 
						|
import { ReadWebhook } from "~/types/api-types/group";
 | 
						|
 | 
						|
export const useGroupWebhooks = function () {
 | 
						|
  const api = useUserApi();
 | 
						|
  const loading = ref(false);
 | 
						|
  const validForm = ref(true);
 | 
						|
 | 
						|
  const actions = {
 | 
						|
    getAll() {
 | 
						|
      loading.value = true;
 | 
						|
      const units = useAsync(async () => {
 | 
						|
        const { data } = await api.groupWebhooks.getAll();
 | 
						|
 | 
						|
        return data;
 | 
						|
      }, useAsyncKey());
 | 
						|
 | 
						|
      loading.value = false;
 | 
						|
      return units;
 | 
						|
    },
 | 
						|
    async refreshAll() {
 | 
						|
      loading.value = true;
 | 
						|
      const { data } = await api.groupWebhooks.getAll();
 | 
						|
 | 
						|
      if (data) {
 | 
						|
        webhooks.value = data;
 | 
						|
      }
 | 
						|
 | 
						|
      loading.value = false;
 | 
						|
    },
 | 
						|
    async createOne() {
 | 
						|
      loading.value = true;
 | 
						|
 | 
						|
      const payload = {
 | 
						|
        enabled: false,
 | 
						|
        name: "New Webhook",
 | 
						|
        url: "",
 | 
						|
        scheduledTime: "00:00",
 | 
						|
      };
 | 
						|
 | 
						|
      const { data } = await api.groupWebhooks.createOne(payload);
 | 
						|
      if (data) {
 | 
						|
        this.refreshAll();
 | 
						|
      }
 | 
						|
 | 
						|
      loading.value = false;
 | 
						|
    },
 | 
						|
    async updateOne(updateData: ReadWebhook) {
 | 
						|
      if (!updateData.id) {
 | 
						|
        return;
 | 
						|
      }
 | 
						|
 | 
						|
      // Convert to UTC time
 | 
						|
      const [hours, minutes] = updateData.scheduledTime.split(":");
 | 
						|
 | 
						|
      const newDt = new Date();
 | 
						|
      newDt.setHours(Number(hours));
 | 
						|
      newDt.setMinutes(Number(minutes));
 | 
						|
 | 
						|
      updateData.scheduledTime = `${pad(newDt.getUTCHours(), 2)}:${pad(newDt.getUTCMinutes(), 2)}`;
 | 
						|
      console.log(updateData.scheduledTime);
 | 
						|
 | 
						|
      const payload = {
 | 
						|
        ...updateData,
 | 
						|
        scheduledTime: updateData.scheduledTime,
 | 
						|
      };
 | 
						|
 | 
						|
      loading.value = true;
 | 
						|
      const { data } = await api.groupWebhooks.updateOne(updateData.id, payload);
 | 
						|
      if (data) {
 | 
						|
        this.refreshAll();
 | 
						|
      }
 | 
						|
      loading.value = false;
 | 
						|
    },
 | 
						|
 | 
						|
    async deleteOne(id: string | number) {
 | 
						|
      loading.value = true;
 | 
						|
      const { data } = await api.groupWebhooks.deleteOne(id);
 | 
						|
      if (data) {
 | 
						|
        this.refreshAll();
 | 
						|
      }
 | 
						|
    },
 | 
						|
  };
 | 
						|
 | 
						|
  const webhooks = actions.getAll();
 | 
						|
 | 
						|
  return { webhooks, actions, validForm };
 | 
						|
};
 | 
						|
 | 
						|
function pad(num: number, size: number) {
 | 
						|
  let numStr = num.toString();
 | 
						|
  while (numStr.length < size) numStr = "0" + numStr;
 | 
						|
  return numStr;
 | 
						|
}
 | 
						|
 | 
						|
export function timeUTCToLocal(time: string): string {
 | 
						|
  const [hours, minutes] = time.split(":");
 | 
						|
  const dt = new Date();
 | 
						|
  dt.setUTCMinutes(Number(minutes));
 | 
						|
  dt.setUTCHours(Number(hours));
 | 
						|
  return `${pad(dt.getHours(), 2)}:${pad(dt.getMinutes(), 2)}`;
 | 
						|
}
 | 
						|
 | 
						|
export function timeLocalToUTC(time: string) {
 | 
						|
  const [hours, minutes] = time.split(":");
 | 
						|
  const dt = new Date();
 | 
						|
  dt.setHours(Number(hours));
 | 
						|
  dt.setMinutes(Number(minutes));
 | 
						|
  return `${pad(dt.getUTCHours(), 2)}:${pad(dt.getUTCMinutes(), 2)}`;
 | 
						|
}
 |