diff --git a/src/features/persons/spiritual_status/first_report/useFirstReport.tsx b/src/features/persons/spiritual_status/first_report/useFirstReport.tsx index 2cbc2e1c0f..c747b7f704 100644 --- a/src/features/persons/spiritual_status/first_report/useFirstReport.tsx +++ b/src/features/persons/spiritual_status/first_report/useFirstReport.tsx @@ -118,7 +118,7 @@ const useFirstReport = () => { const minDateFormatted = formatDate(new Date(minDate), 'yyyy/MM/dd'); - const firstReport = newPerson.person_data.first_report.value; + const firstReport = newPerson.person_data.first_report?.value; const currentFirstReport = firstReport ? formatDate(new Date(firstReport), 'yyyy/MM/dd') diff --git a/src/features/reports/publisher_records_details/month_item/useMonthItem.tsx b/src/features/reports/publisher_records_details/month_item/useMonthItem.tsx index 1dbd89f440..8dc24a4379 100644 --- a/src/features/reports/publisher_records_details/month_item/useMonthItem.tsx +++ b/src/features/reports/publisher_records_details/month_item/useMonthItem.tsx @@ -5,12 +5,10 @@ import { monthNamesState } from '@states/app'; import { currentMonthServiceYear } from '@utils/date'; import { congFieldServiceReportsState } from '@states/field_service_reports'; import { branchFieldReportsState } from '@states/branch_field_service_reports'; -import useCurrentUser from '@hooks/useCurrentUser'; +import { formatDate } from '@services/dateformat'; import usePerson from '@features/persons/hooks/usePerson'; const useMonthItem = ({ month, person }: MonthItemProps) => { - const { first_report } = useCurrentUser(); - const { personIsEnrollmentActive, personIsBaptizedPublisher, @@ -25,6 +23,35 @@ const useMonthItem = ({ month, person }: MonthItemProps) => { const [showEdit, setShowEdit] = useState(false); const [editorOpen, setEditorOpen] = useState(false); + const first_report = useMemo(() => { + if (!person) return; + + if (person.person_data.first_report?.value) { + return formatDate( + new Date(person.person_data.first_report.value), + 'yyyy/MM' + ); + } + + // get all status history + let history = [ + ...person.person_data.publisher_unbaptized.history, + ...person.person_data.publisher_baptized.history, + ]; + + history = history.filter( + (record) => !record._deleted && record.start_date?.length > 0 + ); + + history.sort((a, b) => a.start_date.localeCompare(b.start_date)); + + if (history.length === 0) return; + + const firstDate = new Date(history.at(0).start_date); + + return formatDate(firstDate, 'yyyy/MM'); + }, [person]); + const branchReport = useMemo(() => { return branchReports.find((record) => record.report_date === month); }, [branchReports, month]); @@ -63,6 +90,7 @@ const useMonthItem = ({ month, person }: MonthItemProps) => { }, [month]); const not_publisher = useMemo(() => { + console.log(month, first_report); if (!first_report || first_report?.length === 0) return true; if (month < first_report) return true;