Skip to content

Commit acddc5e

Browse files
authored
fix(ministry): show previous month if report not yet submitted
1 parent 6722175 commit acddc5e

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

src/features/ministry/report/publisher_report/monthly_report/useMonthlyReport.tsx

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ import { congFieldServiceReportsState } from '@states/field_service_reports';
1414
import { userLocalUIDState } from '@states/settings';
1515
import usePerson from '@features/persons/hooks/usePerson';
1616

17+
const getPreviousMonth = (value: string) => {
18+
const [year, month] = value.split('/').map(Number);
19+
20+
if (month === 1) {
21+
return `${year - 1}/12`;
22+
}
23+
24+
const newMonth = month - 1;
25+
return `${year}/${newMonth.toString().padStart(2, '0')}`;
26+
};
27+
1728
const useMonthlyReport = () => {
1829
const { person, first_report } = useCurrentUser();
1930

@@ -95,9 +106,33 @@ const useMonthlyReport = () => {
95106
});
96107
}, [monthsList, reports, congReports, userUID]);
97108

98-
const initialMonthReport = useMemo(() => {
99-
return currentMonthServiceYear();
100-
}, []);
109+
const getInitialMonth = () => {
110+
const currentMonth = currentMonthServiceYear();
111+
const previousMonth = getPreviousMonth(currentMonth);
112+
113+
const prevCongregationReport = congReports.find(
114+
(report) =>
115+
report.report_data.report_date === previousMonth &&
116+
report.report_data.person_uid === userUID
117+
);
118+
119+
const prevUserReport = reports.find(
120+
(report) => report.report_date === previousMonth
121+
);
122+
123+
const isPrevSubmitted =
124+
(prevCongregationReport &&
125+
prevCongregationReport.report_data.shared_ministry) ||
126+
(prevUserReport && prevUserReport.report_data.status !== 'pending');
127+
128+
if (!isPrevSubmitted) {
129+
return previousMonth;
130+
}
131+
132+
return currentMonth;
133+
};
134+
135+
const initialMonthReport = getInitialMonth();
101136

102137
const handleMonthChange = (value: number) => {
103138
setSelectedMonth(monthsList.at(value).value);

0 commit comments

Comments
 (0)