Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reports): update logic to count inactive publishers #2471

Merged
merged 3 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/components/progress_bar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const ProgressBar = ({ value, maxValue }: ProgressBarProps) => {
)}

{maxValue - value > 0 && (
<StyledProgressBarToFill>{maxValue - value}</StyledProgressBarToFill>
<StyledProgressBarToFill>
{value === 0 ? 0 : maxValue - value}
</StyledProgressBarToFill>
)}
</StyledProgressBar>
</StyledProgressBarBox>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -28,6 +28,7 @@ const ListByGroups = (props: ListByGroupsProps) => {
key={person.person_uid}
person={person}
month={month}
type={type}
/>
))}
</Stack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<UserCard onClick={handleOpenPublisher}>
<PersonDetails person={person} month={month} />
<PersonDetails person={person} month={month} badgesOverride={badges} />

<IconArrowLink color="var(--black)" />
</UserCard>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { PersonType } from '@definition/person';

export type PersonItemProps = {
person: PersonType;
month: string
month: string;
type: 'active' | 'inactive';
};
Original file line number Diff line number Diff line change
@@ -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('/');
rhahao marked this conversation as resolved.
Show resolved Hide resolved
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;
39 changes: 20 additions & 19 deletions src/features/persons/hooks/usePersons.tsx
Original file line number Diff line number Diff line change
@@ -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 = () => {
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/features/persons/person_details/index.types.ts
Original file line number Diff line number Diff line change
@@ -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 }[];
};
10 changes: 8 additions & 2 deletions src/features/persons/person_details/usePersonDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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 };
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ const useReportDetails = () => {
handleAssignAP,
handleVerifyReport,
isInactive,
handleMarkAsActive,currentMonth
handleMarkAsActive,
currentMonth,
};
};

Expand Down
3 changes: 2 additions & 1 deletion src/locales/en/congregation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
}
Loading