diff --git a/src/modules/timesheets/composables/use-timesheet-api.ts b/src/modules/timesheets/composables/use-timesheet-api.ts index e69de29..f04ecf4 100644 --- a/src/modules/timesheets/composables/use-timesheet-api.ts +++ b/src/modules/timesheets/composables/use-timesheet-api.ts @@ -0,0 +1,33 @@ +import { useAuthStore } from "src/stores/auth-store"; +import { useTimesheetStore } from "src/stores/timesheet-store" +import { ref } from "vue"; +import { timesheetTempService } from "../services/timesheet-services"; + + +export const useTimesheetApi = () => { + const timesheet_store = useTimesheetStore(); + const auth_store = useAuthStore(); + const week_offset = ref(0); + + const fetch_week = async (offset = week_offset.value) => { + const email = auth_store.user?.email; + if(!email) return; + try{ + timesheet_store.is_loading = true; + const timesheet = await timesheetTempService.getTimesheetsByEmail(email, offset); + timesheet_store.current_timesheet = timesheet; + week_offset.value = offset; + }catch (err) { + console.error('fetch week error', err); + timesheet_store.current_timesheet = { ...timesheet_store.current_timesheet, shifts: [], expenses: [] }; + } finally { + timesheet_store.is_loading = false; + } + }; + + const this_week = async () => fetch_week(0); + const next_week = async () => fetch_week(week_offset.value + 1); + const previous_week = async () => fetch_week(week_offset.value - 1); + + return { week_offset, this_week, next_week, previous_week, fetch_week}; +} \ No newline at end of file diff --git a/src/modules/timesheets/pages/timesheet-temp-page.vue b/src/modules/timesheets/pages/timesheet-temp-page.vue index d030193..1b5d365 100644 --- a/src/modules/timesheets/pages/timesheet-temp-page.vue +++ b/src/modules/timesheets/pages/timesheet-temp-page.vue @@ -1,20 +1,28 @@