Skip to content

Commit

Permalink
fix(records): add unsaved weekly timesheet ref
Browse files Browse the repository at this point in the history
  • Loading branch information
italoteix committed Jun 7, 2021
1 parent a404e81 commit ff35c77
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions composables/useTimesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { buildEmailData } from "~/helpers/email";
export default (employeeId: string, startTimestamp?: number) => {
const store = useStore<RootStoreState>();
const hasUnsavedChanges = ref<Boolean>(false);
const unsavedWeeklyTimesheet = ref<WeeklyTimesheet>();
const recordsState = computed(() => store.state.records);
const timesheetState = computed(() => store.state.timesheets);

Expand Down Expand Up @@ -67,6 +68,7 @@ export default (employeeId: string, startTimestamp?: number) => {
if (!confirmation) return;
}

unsavedWeeklyTimesheet.value = undefined;
store.dispatch("records/goToWeek", { employeeId, to });
};

Expand All @@ -83,13 +85,18 @@ export default (employeeId: string, startTimestamp?: number) => {
};

const deleteProject = (project: TimesheetProject) => {
hasUnsavedChanges.value = false;

store.dispatch("records/deleteProjectRecords", {
week: recordsState.value.selectedWeek,
project,
employeeId,
});

unsavedWeeklyTimesheet.value = {
projects: timesheet.value.projects.filter(
(proj) => proj.customer.id !== project.customer.id
),
travelProject: timesheet.value.travelProject,
};
};

const copyPreviousWeek = () => {
Expand Down Expand Up @@ -129,19 +136,27 @@ export default (employeeId: string, startTimestamp?: number) => {
recordsState.value.travelRecords,
],
() => {
hasUnsavedChanges.value = false;
if (!timesheet.value) {
hasUnsavedChanges.value = false;
}

store.dispatch("timesheets/getTimesheets", {
date: new Date(recordsState.value.selectedWeek[0].date).getTime(),
employeeId,
});

timesheet.value = createWeeklyTimesheet({
const newTimesheet = createWeeklyTimesheet({
week: recordsState.value.selectedWeek,
timeRecords: recordsState.value.timeRecords,
travelRecords: recordsState.value.travelRecords,
workScheme: recordsState.value.workScheme,
});

timesheet.value = unsavedWeeklyTimesheet.value
? unsavedWeeklyTimesheet.value
: newTimesheet;

unsavedWeeklyTimesheet.value = undefined;
},
{ deep: true }
);
Expand All @@ -150,6 +165,8 @@ export default (employeeId: string, startTimestamp?: number) => {
newTimesheetStatus: TimesheetStatus,
denialMessage?: string
) => {
unsavedWeeklyTimesheet.value = undefined;

store.dispatch("records/saveTimesheet", {
employeeId,
week: recordsState.value.selectedWeek,
Expand Down Expand Up @@ -187,6 +204,7 @@ export default (employeeId: string, startTimestamp?: number) => {
};

const denyTimesheet = (employee: Employee, denialMessage: string) => {
unsavedWeeklyTimesheet.value = undefined;
const selectedTimesheet = timesheetState.value.timesheets[0];

if (!selectedTimesheet || selectedTimesheet.status !== recordStatus.PENDING)
Expand Down

0 comments on commit ff35c77

Please sign in to comment.