Skip to content

Commit

Permalink
feat(ministry): auto start timer after adding time
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Dec 12, 2024
1 parent 7ec2c12 commit dec47b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/features/ministry/report/ministry_timer/useMinistryTimer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEffect, useMemo, useRef, useState } from 'react';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { TimerState } from './index.types';
import { formatDate } from '@services/dateformat';
import {
reportUserSelectedMonthState,
Expand Down Expand Up @@ -57,17 +56,18 @@ const useMinistryTimer = () => {
}
}, [timer, todayReport]);

const timerState = useMemo(() => {
return timer.state;
}, [timer.state]);

const [time, setTime] = useState(initialTime);
const [duration, setDuration] = useState('00:00');
const [timerState, setTimerState] = useState<TimerState>(timer.state);
const [editorOpen, setEditorOpen] = useState(false);
const [sliderOpen, setSliderOpen] = useState(false);
const [hours, setHours] = useState(0);
const [minutes, setMinutes] = useState(0);

const handleStart = async () => {
setTimerState('started');

let report: UserFieldServiceDailyReportType;

if (!todayReport) {
Expand All @@ -86,8 +86,6 @@ const useMinistryTimer = () => {
};

const handlePause = async () => {
setTimerState('paused');

const report = structuredClone(todayReport);
report.report_data.timer.state = 'paused';
report.report_data.timer.value = time;
Expand All @@ -101,8 +99,6 @@ const useMinistryTimer = () => {
};

const handleStop = async () => {
setTimerState('not_started');

const report = structuredClone(todayReport);
report.report_data.timer.state = 'not_started';
report.report_data.timer.value = 0;
Expand All @@ -113,7 +109,7 @@ const useMinistryTimer = () => {
if (hours > 0 || minutes > 0) {
const draftReport = structuredClone(report);

draftReport.report_data.hours.field_service = `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}`;
draftReport.report_data.hours.field_service = `${hours}:${String(minutes).padStart(2, '0')}`;

await handleSaveDailyFieldServiceReport(draftReport);

Expand Down Expand Up @@ -175,6 +171,8 @@ const useMinistryTimer = () => {

report.report_data._deleted = false;
report.report_data.timer.value = value;
report.report_data.timer.state = 'started';
report.report_data.timer.start = Date.now();
report.report_data.hours.field_service = `${hours}:${String(minutes).padStart(2, '0')}`;
report.report_data.updatedAt = new Date().toISOString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export type ReportFormDialogProps = {
open: boolean;
onClose: VoidFunction;
date?: string;
autoStart?: boolean;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ServiceTimeProps } from './index.types';
import { personIsEnrollmentActive } from '@services/app/persons';
import { handleSaveDailyFieldServiceReport } from '@services/app/user_field_service_reports';
import { hoursCreditsEnabledState } from '@states/settings';
import { formatDate } from '@services/dateformat';
import useMinistryDailyRecord from '@features/ministry/hooks/useMinistryDailyRecord';

const useServiceTime = ({ onClose }: ServiceTimeProps) => {
Expand All @@ -27,6 +28,10 @@ const useServiceTime = ({ onClose }: ServiceTimeProps) => {
const { hours, bibleStudies, hoursCredit } =
useMinistryDailyRecord(currentReport);

const today = useMemo(() => {
return formatDate(new Date(), 'yyyy/MM/dd');
}, []);

const monthReport = useMemo(() => {
if (!currentReport) return;

Expand Down Expand Up @@ -117,6 +122,17 @@ const useServiceTime = ({ onClose }: ServiceTimeProps) => {
const report = structuredClone(currentReport);
report.report_data._deleted = false;

if (currentReport.report_date === today) {
const hoursMinutes = currentReport.report_data.hours.field_service;
const [hours, minutes] = hoursMinutes.split(':').map(Number);

let seconds = hours * 3600;

if (minutes) seconds += minutes * 60;

report.report_data.timer.value = seconds;
}

await handleSaveDailyFieldServiceReport(report);

onClose();
Expand Down

0 comments on commit dec47b5

Please sign in to comment.