Skip to content

Commit

Permalink
fix: refactor some code
Browse files Browse the repository at this point in the history
  • Loading branch information
Innocent-Akim committed Nov 27, 2024
1 parent 3ed5062 commit dbba956
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
6 changes: 2 additions & 4 deletions apps/web/app/hooks/features/useTimesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,16 @@ export function useTimesheet({
const createTimesheet = useCallback(
async ({ ...timesheetParams }: UpdateTimesheet) => {
if (!user) {
console.error("User not authenticated");
return;
throw new Error("User not authenticated");
}
try {
const response = await queryCreateTimesheet(timesheetParams);
console.log("Timesheet created successfully:", response.data);
setTimesheet((prevTimesheet) => [
response.data,
...(prevTimesheet || [])
]);
} catch (error) {
console.error("Error creating timesheet:", error);
throw error;
}
},
[queryCreateTimesheet, setTimesheet, user]
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/interfaces/timer/ITimerLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export interface TimesheetLog extends BaseEntity {
stoppedAt: string;
editedAt: string | null;
logType: TimeLogType;
source: "WEB_TIMER" | "MOBILE_APP" | "DESKTOP_APP" | TimerSource;
source: TimerSource;
description: string;
reason: string | null;
isBillable: boolean;
Expand Down
9 changes: 8 additions & 1 deletion apps/web/app/services/client/api/timer/timer-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,12 @@ export function updateStatusTimesheetFromApi(data: IUpdateTimesheetStatus) {
export function createTimesheetFromApi(data: UpdateTimesheet) {
const organizationId = getOrganizationIdCookie();
const tenantId = getTenantIdCookie();
return post<TimesheetLog>('/timesheet/time-log', { ...data, organizationId }, { tenantId })
if (!organizationId || !tenantId) {
throw new Error('Required parameters missing: organizationId and tenantId are required');
}
try {
return post<TimesheetLog>('/timesheet/time-log', { ...data, organizationId }, { tenantId })
} catch (error) {
throw new Error('Failed to create timesheet log');
}
}
3 changes: 2 additions & 1 deletion apps/web/app/stores/time-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface IFilterOption {
label: string;
}


export const timerLogsDailyReportState = atom<ITimerLogsDailyReport[]>([]);

export const timesheetRapportState = atom<TimesheetLog[]>([])
Expand All @@ -19,4 +20,4 @@ export const timesheetFilterStatusState = atom<IFilterOption[]>([]);
export const timesheetDeleteState = atom<string[]>([]);
export const timesheetGroupByDayState = atom<TimesheetFilterByDays>('Daily')
export const timesheetUpdateStatus = atom<UpdateTimesheetStatus[]>([])
export const timesheetUpdateState = atom<TimesheetLog>()
export const timesheetUpdateState = atom<TimesheetLog | null>(null)
27 changes: 19 additions & 8 deletions apps/web/lib/features/integrations/calendar/table-time-sheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export const columns: ColumnDef<TimeSheet>[] = [

export function DataTableTimeSheet({ data }: { data?: GroupedTimesheet[] }) {
const { isOpen, openModal, closeModal } = useModal();
const { deleteTaskTimesheet, loadingDeleteTimesheet, getStatusTimesheet } = useTimesheet({});
const { deleteTaskTimesheet, loadingDeleteTimesheet, getStatusTimesheet, updateTimesheetStatus } = useTimesheet({});
const { handleSelectRowTimesheet, selectTimesheet, setSelectTimesheet, timesheetGroupByDays } = useTimelogFilterOptions();
const [isDialogOpen, setIsDialogOpen] = React.useState(false);
const handleConfirm = () => {
Expand Down Expand Up @@ -196,10 +196,15 @@ export function DataTableTimeSheet({ data }: { data?: GroupedTimesheet[] }) {
}
});

const handleButtonClick = (action: StatusAction) => {
const handleButtonClick = async (action: StatusAction) => {
switch (action) {
case 'Approved':
// TODO: Implement approval logic
if (selectTimesheet.length > 0) {
await updateTimesheetStatus({
status: 'APPROVED',
ids: selectTimesheet
})
}
break;
case 'Denied':
openModal();
Expand All @@ -216,7 +221,7 @@ export function DataTableTimeSheet({ data }: { data?: GroupedTimesheet[] }) {
<div className="w-full dark:bg-dark--theme">
<AlertDialogConfirmation
title="Are you sure you want to delete this?"
description={`This action is irreversible. All related data will be lost. (${selectTimesheet.length})`}
description={`This action is irreversible. All related data will be lost.`}
confirmText={t('common.DELETE')}
cancelText={t('common.CANCEL')}
isOpen={isDialogOpen}
Expand Down Expand Up @@ -504,10 +509,16 @@ export const StatusTask = ({ timesheet }: { timesheet: TimesheetLog }) => {
<DropdownMenuPortal>
<DropdownMenuSubContent>
{statusTable?.map((status, index) => (
<DropdownMenuItem onClick={() => updateTimesheetStatus({
status: status.label as TimesheetStatus,
ids: [timesheet.timesheet.id]
})} key={index} textValue={status.label} className="cursor-pointer">
<DropdownMenuItem onClick={async () => {
try {
await updateTimesheetStatus({
status: status.label as TimesheetStatus,
ids: [timesheet.timesheet.id]
});
} catch (error) {
console.error('Failed to update timesheet status:');
}
}} key={index} textValue={status.label} className="cursor-pointer">
<div className="flex items-center gap-3">
<div className={clsxm('h-2 w-2 rounded-full', statusColor(status.label).bg)}></div>
<span>{status.label}</span>
Expand Down

0 comments on commit dbba956

Please sign in to comment.