From dcd790ee74743af5755715c000236fc27d16a6bf Mon Sep 17 00:00:00 2001 From: rhahao <26148770+rhahao@users.noreply.github.com> Date: Sun, 8 Sep 2024 10:07:52 +0300 Subject: [PATCH 1/3] fix(reports): update logic to count inactive publishers --- src/components/progress_bar/index.tsx | 6 ++- src/features/persons/hooks/usePersons.tsx | 39 ++++++++++--------- .../submit_report/useSubmitReport.tsx | 2 +- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/components/progress_bar/index.tsx b/src/components/progress_bar/index.tsx index ab147c9079..043760c815 100644 --- a/src/components/progress_bar/index.tsx +++ b/src/components/progress_bar/index.tsx @@ -18,6 +18,8 @@ import { const ProgressBar = ({ value, maxValue }: ProgressBarProps) => { const progressValue = Math.round((value * 100) / maxValue); + console.log(value, maxValue); + return ( @@ -28,7 +30,9 @@ const ProgressBar = ({ value, maxValue }: ProgressBarProps) => { )} {maxValue - value > 0 && ( - {maxValue - value} + + {value === 0 ? 0 : maxValue - value} + )} diff --git a/src/features/persons/hooks/usePersons.tsx b/src/features/persons/hooks/usePersons.tsx index 452c7c0953..43c71211a5 100644 --- a/src/features/persons/hooks/usePersons.tsx +++ b/src/features/persons/hooks/usePersons.tsx @@ -1,6 +1,7 @@ import { useRecoilValue } from 'recoil'; import { personsActiveState } from '@states/persons'; import { formatDate } from '@services/dateformat'; +import { addMonths } from '@utils/date'; import usePerson from './usePerson'; const usePersons = () => { @@ -94,35 +95,35 @@ const usePersons = () => { const startMonth = `${+year - 1}/09`; const endMonth = `${year}/08`; - const result = persons.filter((record) => { - const isMidweek = personIsMidweekStudent(record); + const result = persons.filter((person) => { + const isMidweek = personIsMidweekStudent(person); if (isMidweek) return false; - const firstReport = personGetFirstReport(record); - - if (firstReport > endMonth) return false; - + // get all histories with end date const history = [ - ...record.person_data.publisher_baptized.history, - ...record.person_data.publisher_unbaptized.history, - ].filter((record) => !record._deleted); + ...person.person_data.publisher_baptized.history, + ...person.person_data.publisher_unbaptized.history, + ].filter((record) => !record._deleted && record.start_date?.length > 0); + + const historyWithEndDate = history.filter( + (record) => record.end_date?.length > 0 + ); - if (history.length === 1 && history.at(0).end_date === null) { - return false; - } + const isInactive = historyWithEndDate.some((record) => { + const monthNext = formatDate(addMonths(record.end_date, 1), 'yyyy/MM'); - const inactive = history.some((data) => { - if (data.start_date === null) return false; - if (data.end_date === null) return false; + const isActive = history.some((active) => { + const date = formatDate(new Date(active.start_date), 'yyyy/MM'); - const endTmp = new Date(data.end_date); + return date === monthNext; + }); - const endTmpDate = formatDate(endTmp, 'yyyy/MM'); + if (isActive) return false; - return endTmpDate >= startMonth && endTmpDate <= endMonth; + return monthNext >= startMonth && monthNext <= endMonth; }); - return inactive; + return isInactive; }); return result; diff --git a/src/features/reports/branch_office/submit_report/useSubmitReport.tsx b/src/features/reports/branch_office/submit_report/useSubmitReport.tsx index daca91e142..022c1c2714 100644 --- a/src/features/reports/branch_office/submit_report/useSubmitReport.tsx +++ b/src/features/reports/branch_office/submit_report/useSubmitReport.tsx @@ -43,7 +43,7 @@ const useSubmitReport = ({ onClose }: SubmitReportProps) => { if (!isInactive) continue; const [year, varMonth] = month.split('/'); - const endDate = new Date(+year, +varMonth, 0).toISOString(); + const endDate = new Date(+year, +varMonth - 1, 0).toISOString(); const newPerson = structuredClone(person); From 1225457290891fedec57571951e6e7ca8c845a7c Mon Sep 17 00:00:00 2001 From: rhahao <26148770+rhahao@users.noreply.github.com> Date: Sun, 8 Sep 2024 10:26:09 +0300 Subject: [PATCH 2/3] fix(Publisher records): show last report of inactive publishers --- src/components/progress_bar/index.tsx | 2 - .../publisher_tabs/list_by_groups/index.tsx | 3 +- .../list_by_groups/useListByGroups.tsx | 2 +- .../publisher_tabs/person_item/index.tsx | 6 +-- .../publisher_tabs/person_item/index.types.ts | 3 +- .../person_item/usePersonItem.tsx | 44 +++++++++++++++++-- .../persons/person_details/index.types.ts | 3 +- .../person_details/usePersonDetails.tsx | 10 ++++- .../report_details/useReportDetails.tsx | 3 +- src/locales/en/congregation.json | 3 +- 10 files changed, 63 insertions(+), 16 deletions(-) diff --git a/src/components/progress_bar/index.tsx b/src/components/progress_bar/index.tsx index 043760c815..40954dfcc4 100644 --- a/src/components/progress_bar/index.tsx +++ b/src/components/progress_bar/index.tsx @@ -18,8 +18,6 @@ import { const ProgressBar = ({ value, maxValue }: ProgressBarProps) => { const progressValue = Math.round((value * 100) / maxValue); - console.log(value, maxValue); - return ( diff --git a/src/features/congregation/publisher_records/publisher_tabs/list_by_groups/index.tsx b/src/features/congregation/publisher_records/publisher_tabs/list_by_groups/index.tsx index 93f6c062fb..79b9b591e5 100644 --- a/src/features/congregation/publisher_records/publisher_tabs/list_by_groups/index.tsx +++ b/src/features/congregation/publisher_records/publisher_tabs/list_by_groups/index.tsx @@ -6,7 +6,7 @@ import Divider from '@components/divider'; import PersonItem from '../person_item'; const ListByGroups = (props: ListByGroupsProps) => { - const { groups, month, expanded, handleExpandedChange } = + const { groups, month, expanded, handleExpandedChange, type } = useListByGroups(props); return ( @@ -28,6 +28,7 @@ const ListByGroups = (props: ListByGroupsProps) => { key={person.person_uid} person={person} month={month} + type={type} /> ))} diff --git a/src/features/congregation/publisher_records/publisher_tabs/list_by_groups/useListByGroups.tsx b/src/features/congregation/publisher_records/publisher_tabs/list_by_groups/useListByGroups.tsx index 67f97b32df..356f2cf561 100644 --- a/src/features/congregation/publisher_records/publisher_tabs/list_by_groups/useListByGroups.tsx +++ b/src/features/congregation/publisher_records/publisher_tabs/list_by_groups/useListByGroups.tsx @@ -83,7 +83,7 @@ const useListByGroups = ({ type }: ListByGroupsProps) => { const handleExpandedChange = (value: string | false) => setExpanded(value); - return { groups, month, expanded, handleExpandedChange }; + return { groups, month, expanded, handleExpandedChange, type }; }; export default useListByGroups; diff --git a/src/features/congregation/publisher_records/publisher_tabs/person_item/index.tsx b/src/features/congregation/publisher_records/publisher_tabs/person_item/index.tsx index 39b4feef10..9699d929e2 100644 --- a/src/features/congregation/publisher_records/publisher_tabs/person_item/index.tsx +++ b/src/features/congregation/publisher_records/publisher_tabs/person_item/index.tsx @@ -4,12 +4,12 @@ import { PersonItemProps } from './index.types'; import usePersonItem from './usePersonItem'; import PersonDetails from '@features/persons/person_details'; -const PersonItem = ({ person, month }: PersonItemProps) => { - const { handleOpenPublisher } = usePersonItem(person); +const PersonItem = (props: PersonItemProps) => { + const { handleOpenPublisher, month, person, badges } = usePersonItem(props); return ( - + diff --git a/src/features/congregation/publisher_records/publisher_tabs/person_item/index.types.ts b/src/features/congregation/publisher_records/publisher_tabs/person_item/index.types.ts index 945c8cccef..ebcc365f32 100644 --- a/src/features/congregation/publisher_records/publisher_tabs/person_item/index.types.ts +++ b/src/features/congregation/publisher_records/publisher_tabs/person_item/index.types.ts @@ -2,5 +2,6 @@ import { PersonType } from '@definition/person'; export type PersonItemProps = { person: PersonType; - month: string + month: string; + type: 'active' | 'inactive'; }; diff --git a/src/features/congregation/publisher_records/publisher_tabs/person_item/usePersonItem.tsx b/src/features/congregation/publisher_records/publisher_tabs/person_item/usePersonItem.tsx index 148c292f5d..cd07c742ad 100644 --- a/src/features/congregation/publisher_records/publisher_tabs/person_item/usePersonItem.tsx +++ b/src/features/congregation/publisher_records/publisher_tabs/person_item/usePersonItem.tsx @@ -1,14 +1,52 @@ +import { useMemo } from 'react'; import { useNavigate } from 'react-router-dom'; -import { PersonType } from '@definition/person'; +import { useRecoilValue } from 'recoil'; +import { useAppTranslation } from '@hooks/index'; +import { BadgeColor } from '@definition/app'; +import { congFieldServiceReportsState } from '@states/field_service_reports'; +import { monthNamesState } from '@states/app'; +import { PersonItemProps } from './index.types'; -const usePersonItem = (person: PersonType) => { +const usePersonItem = ({ month, person, type }: PersonItemProps) => { const navigate = useNavigate(); + const { t } = useAppTranslation(); + + const reports = useRecoilValue(congFieldServiceReportsState); + const monthNames = useRecoilValue(monthNamesState); + + const badges = useMemo(() => { + if (type === 'active') return; + + const userReports = reports.filter( + (record) => record.report_data.person_uid === person.person_uid + ); + if (userReports.length === 0) return; + + // get last report + userReports.sort((a, b) => + b.report_data.report_date.localeCompare(a.report_data.report_date) + ); + const lastReport = userReports.at(0).report_data.report_date; + + const [year, month] = lastReport.split('/'); + const monthname = monthNames[+month - 1]; + + const date = t('tr_monthYear', { month: monthname, year }); + + return [ + { + name: t('tr_publisherLastReport', { month: date }), + color: 'grey' as BadgeColor, + }, + ]; + }, [type, reports, person, monthNames, t]); + const handleOpenPublisher = () => { navigate(`/publisher-records/${person.person_uid}`); }; - return { handleOpenPublisher }; + return { handleOpenPublisher, month, person, badges }; }; export default usePersonItem; diff --git a/src/features/persons/person_details/index.types.ts b/src/features/persons/person_details/index.types.ts index 41613e601c..f1f972068b 100644 --- a/src/features/persons/person_details/index.types.ts +++ b/src/features/persons/person_details/index.types.ts @@ -1,8 +1,9 @@ -import { CustomClassName } from '@definition/app'; +import { BadgeColor, CustomClassName } from '@definition/app'; import { PersonType } from '@definition/person'; export type PersonDetailsProps = { person: PersonType; month: string; className?: CustomClassName; + badgesOverride?: { name: string; color: BadgeColor }[]; }; diff --git a/src/features/persons/person_details/usePersonDetails.tsx b/src/features/persons/person_details/usePersonDetails.tsx index 876c55c5cb..93734802b8 100644 --- a/src/features/persons/person_details/usePersonDetails.tsx +++ b/src/features/persons/person_details/usePersonDetails.tsx @@ -2,7 +2,11 @@ import { useMemo } from 'react'; import { PersonDetailsProps } from './index.types'; import usePerson from '@features/persons/hooks/usePerson'; -const usePersonDetails = ({ month, person }: PersonDetailsProps) => { +const usePersonDetails = ({ + month, + person, + badgesOverride, +}: PersonDetailsProps) => { const { getName, getBadges } = usePerson(); const name = useMemo(() => { @@ -20,8 +24,10 @@ const usePersonDetails = ({ month, person }: PersonDetailsProps) => { const badges = useMemo(() => { if (!person) return []; + if (badgesOverride) return badgesOverride; + return getBadges(person, month); - }, [person, getBadges, month]); + }, [person, getBadges, month, badgesOverride]); return { name, female, badges }; }; diff --git a/src/features/reports/field_service/report_details/useReportDetails.tsx b/src/features/reports/field_service/report_details/useReportDetails.tsx index 69e0ef3c0a..9e9281c331 100644 --- a/src/features/reports/field_service/report_details/useReportDetails.tsx +++ b/src/features/reports/field_service/report_details/useReportDetails.tsx @@ -253,7 +253,8 @@ const useReportDetails = () => { handleAssignAP, handleVerifyReport, isInactive, - handleMarkAsActive,currentMonth + handleMarkAsActive, + currentMonth, }; }; diff --git a/src/locales/en/congregation.json b/src/locales/en/congregation.json index bcc4a5155b..5fd9961559 100644 --- a/src/locales/en/congregation.json +++ b/src/locales/en/congregation.json @@ -434,5 +434,6 @@ "tr_eventType": "Event type", "tr_averageAttendanceMM": "Average midweek meeting attendance", "tr_S10ReportWithYear": "Congregation analysis report: {{ year }}", - "tr_groupNotAssigned": "Group not assigned" + "tr_groupNotAssigned": "Group not assigned", + "tr_publisherLastReport": "Last report on {{ month }}" } From 79a6b45dc611612b3c3902fc4818124fd13e4156 Mon Sep 17 00:00:00 2001 From: rhahao <26148770+rhahao@users.noreply.github.com> Date: Sun, 8 Sep 2024 10:32:54 +0300 Subject: [PATCH 3/3] chore(app): update variable name --- .../publisher_tabs/person_item/usePersonItem.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/features/congregation/publisher_records/publisher_tabs/person_item/usePersonItem.tsx b/src/features/congregation/publisher_records/publisher_tabs/person_item/usePersonItem.tsx index cd07c742ad..765cfa5229 100644 --- a/src/features/congregation/publisher_records/publisher_tabs/person_item/usePersonItem.tsx +++ b/src/features/congregation/publisher_records/publisher_tabs/person_item/usePersonItem.tsx @@ -29,8 +29,8 @@ const usePersonItem = ({ month, person, type }: PersonItemProps) => { ); const lastReport = userReports.at(0).report_data.report_date; - const [year, month] = lastReport.split('/'); - const monthname = monthNames[+month - 1]; + const [year, reportMonth] = lastReport.split('/'); + const monthname = monthNames[+reportMonth - 1]; const date = t('tr_monthYear', { month: monthname, year });