Skip to content

Commit

Permalink
feat(reports): add delete button to delete daily report
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Jun 12, 2023
1 parent 36ab8d9 commit af9bba3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
5 changes: 2 additions & 3 deletions src/features/userFieldServiceReports/S4DailyRecord.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ import S4BibleStudiesField from './S4BibleStudiesField';
import { UserS4Records } from '../../classes/UserS4Records';
import { shortDatePickerFormatState } from '../../states/main';

const S4DailyRecord = ({ isOpen, month }) => {
const S4DailyRecord = ({ isOpen, month, date, setDate }) => {
const { t } = useTranslation('ui');

const lastDate = new Date(new Date(month).getFullYear(), new Date(month).getMonth() + 1, 0);

const shortDatePickerFormat = useRecoilValue(shortDatePickerFormatState);

const [date, setDate] = useState(null);
const [placements, setPlacements] = useState(0);
const [videos, setVideos] = useState(0);
const [returnVisits, setReturnVisits] = useState(0);
Expand Down Expand Up @@ -48,7 +47,7 @@ const S4DailyRecord = ({ isOpen, month }) => {

useEffect(() => {
setDate(null);
}, [month]);
}, [month, setDate]);

return (
<Collapse
Expand Down
43 changes: 27 additions & 16 deletions src/features/userFieldServiceReports/S4DailyRecordItem.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useTranslation } from 'react-i18next';
import Box from '@mui/material/Box';
import DeleteIcon from '@mui/icons-material/Delete';
import IconButton from '@mui/material/IconButton';
import Typography from '@mui/material/Typography';
import { BibleStudies } from '../../classes/BibleStudies';

Expand All @@ -14,7 +16,7 @@ const styles = {
},
};

const S4DailyRecord = ({ report }) => {
const S4DailyRecord = ({ report, handleDeleteDailyRecord }) => {
const { t } = useTranslation('ui');

const date = new Date(report.month_date).getDate();
Expand All @@ -24,23 +26,32 @@ const S4DailyRecord = ({ report }) => {
return bs.person_name;
});

const handleDeleteRecord = async () => {
await handleDeleteDailyRecord(report.month_date);
};

return (
<Box sx={{ width: '300px', display: 'flex', gap: '8px' }}>
<Typography
sx={{
backgroundColor: '#3f51b5',
color: 'white',
width: '50px',
height: '50px',
padding: '10px',
textAlign: 'center',
borderRadius: '5px',
fontSize: '18px',
fontWeight: 'bold',
}}
>
{date}
</Typography>
<Box sx={{ display: 'flex', gap: '8px', flexDirection: 'column', alignItems: 'center' }}>
<Typography
sx={{
backgroundColor: '#3f51b5',
color: 'white',
width: '50px',
height: '50px',
padding: '10px',
textAlign: 'center',
borderRadius: '5px',
fontSize: '18px',
fontWeight: 'bold',
}}
>
{date}
</Typography>
<IconButton aria-label="delete" color="error" onClick={handleDeleteRecord}>
<DeleteIcon sx={{ fontSize: '35px' }} />
</IconButton>
</Box>

<Box sx={{ display: 'flex', flexDirection: 'column', gap: '15px', marginBottom: '25px' }}>
<Box>
Expand Down
16 changes: 14 additions & 2 deletions src/pages/UserFieldServiceReport.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const UserFieldServiceReport = () => {
const [isSubmitted, setIsSubmitted] = useState(false);
const [isPending, setIsPending] = useState(true);
const [isLocked, setIsLocked] = useState(false);
const [date, setDate] = useState(null);

const secretaryRole = Setting.cong_role.includes('secretary');

Expand Down Expand Up @@ -69,6 +70,15 @@ const UserFieldServiceReport = () => {
setIsSubmitted(currentS4.isSubmitted);
};

const handleDeleteDailyRecord = async (report_date) => {
const currentReport = await UserS4Records.get(report_date);
await UserS4Records.delete(currentReport.report_uid);
setDate(null);

const currentS4 = await UserS4MonthlyReport.get(currentMonth);
setDailyRecords(currentS4.reports);
};

useEffect(() => {
if (currentServiceYear !== '') {
const options = ServiceYear.getMonths(currentServiceYear);
Expand Down Expand Up @@ -209,7 +219,7 @@ const UserFieldServiceReport = () => {
)}
</Box>

<S4DailyRecord isOpen={openDailyRecord} month={currentMonth} />
<S4DailyRecord isOpen={openDailyRecord} month={currentMonth} date={date} setDate={setDate} />
</Box>

<UserS4 isOpen={showS4 || isLocked || isSubmitted} isSubmitted={isSubmitted} month={currentMonth} />
Expand All @@ -223,7 +233,9 @@ const UserFieldServiceReport = () => {
report.videos > 0 ||
report.duration !== 0 ||
report.returnVisits > 0 ||
report.bibleStudies.length > 0) && <S4DailyRecordItem report={report} />}
report.bibleStudies.length > 0) && (
<S4DailyRecordItem report={report} handleDeleteDailyRecord={handleDeleteDailyRecord} />
)}
</Box>
);
})}
Expand Down

0 comments on commit af9bba3

Please sign in to comment.