import { useAuthStore } from "src/stores/auth-store"; import { useTimesheetStore } from "src/stores/timesheet-store" export const useTimesheetApi = () => { const timesheet_store = useTimesheetStore(); const auth_store = useAuthStore(); const getTimesheetsByDate = async (date_string: string) => { const success = await timesheet_store.getPayPeriodByDate(date_string); if (success) { await timesheet_store.getTimesheetsByPayPeriodAndEmail(auth_store.user.email) } } const fetchPayPeriod = async (direction: number) => { const current_pay_period = timesheet_store.current_pay_period; let new_pay_period_no = current_pay_period.pay_period_no + direction; let new_pay_year = current_pay_period.pay_year; if (new_pay_period_no > 26) { new_pay_period_no = 1; new_pay_year += 1; } if (new_pay_period_no < 1) { new_pay_period_no = 26; new_pay_year -= 1; } const success = await timesheet_store.getPayPeriodByYearAndPeriodNumber(new_pay_year, new_pay_period_no); if (success) { await timesheet_store.getTimesheetsByPayPeriodAndEmail(auth_store.user.email); } }; const getCurrentPayPeriod = async () => fetchPayPeriod(0); const getNextPayPeriod = async () => fetchPayPeriod(1); const getPreviousPayPeriod = async () => fetchPayPeriod(-1); return { getTimesheetsByDate, fetchPayPeriod, getCurrentPayPeriod, getNextPayPeriod, getPreviousPayPeriod, }; };