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

feat(persons): include shortcut for creating new User for specific person #3175

Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions src/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export { default as AppUpdater } from './app_updater';
/* -------------------------- Color scheme selector ------------------------- */
export { default as ColorSchemeSelector } from './color_scheme_selector';

/* -------------------------------- Contact ------------------------------- */
/* --------------------------------- Contact -------------------------------- */
export { default as Contact } from './contact';

/* -------------------------------- Dashboard ------------------------------- */
export { default as DashboardCard } from './dashboard/card';
export { default as DashboardMenu } from './dashboard/menu';
export { default as DashboardSkeletonLoader } from './dashboard/skeleton_loader';

/* -------------------------------- Demo ------------------------------- */
/* ---------------------------------- Demo ---------------------------------- */
export { default as DemoBanner } from './demo/banner';
export { default as DemoNotice } from './demo/notice';
export { default as DemoStartup } from './demo/start';
Expand Down Expand Up @@ -63,6 +63,7 @@ export { default as PersonsList } from './persons/list';
export { default as PersonsSearch } from './persons/search';
export { default as PersonTimeAway } from './persons/time_away';
export { default as PersonAssignmentsHistory } from './persons/assignments_history';
export { default as PersonAppUserProfile } from './persons/app_user_profile';
FussuChalice marked this conversation as resolved.
Show resolved Hide resolved

/* -------------------------------- Ministry -------------------------------- */
export { default as MinistryTimer } from './ministry/report/ministry_timer';
60 changes: 60 additions & 0 deletions src/features/persons/app_user_profile/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Box } from '@mui/material';
import usePersonAppPersonProfile from './useAppUserProfile';
import Typography from '@components/typography';
import { useAppTranslation } from '@hooks/index';
import Button from '@components/button';
import { IconAddPerson, IconArrowLink } from '@components/icons';

const PersonAppUserProfile = () => {
const { t } = useAppTranslation();
const {
userIsRegistered,
getTextForAppPersonProfileDesc,
navigateToManageAccess,
} = usePersonAppPersonProfile();

return (
<Box
sx={{
padding: '16px',
gap: '16px',
display: 'flex',
flexDirection: 'column',
borderRadius: 'var(--radius-xl)',
backgroundColor: userIsRegistered
? 'var(--white)'
: 'var(--accent-150)',
border: `1px ${userIsRegistered ? 'solid' : 'dashed'} var(--accent-300)`,
}}
>
<Box
sx={{
gap: '12px',
display: 'flex',
flexDirection: 'column',
}}
>
<Typography className="h2" color="var(--black)">
{t('tr_appUserProfile')}
</Typography>
<Typography
className="body-regular"
color="var(--black)"
dangerouslySetInnerHTML={{ __html: getTextForAppPersonProfileDesc() }}
></Typography>
FussuChalice marked this conversation as resolved.
Show resolved Hide resolved
</Box>
<Button
startIcon={userIsRegistered ? <IconArrowLink /> : <IconAddPerson />}
variant="small"
sx={{ width: 'fit-content' }}
onClick={() => navigateToManageAccess()}
>
{userIsRegistered
? t('tr_manageUserProfileSettings')
: t('tr_createUserProfile')}
</Button>
</Box>
);
};

export default PersonAppUserProfile;
62 changes: 62 additions & 0 deletions src/features/persons/app_user_profile/useAppUserProfile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { CongregationUserType } from '@definition/api';
import { useAppTranslation } from '@hooks/index';
import { formatDate } from '@services/dateformat';
import { congregationsPersonsState } from '@states/app';
import { personCurrentDetailsState } from '@states/persons';
import { shortDateFormatState } from '@states/settings';
import { useNavigate } from 'react-router-dom';
import { useRecoilValue } from 'recoil';

const usePersonAppPersonProfile = () => {
const { t } = useAppTranslation();
const congregationsPersons = useRecoilValue(congregationsPersonsState);
const currentPersonDetails = useRecoilValue(personCurrentDetailsState);
const shortDateFormat = useRecoilValue(shortDateFormatState);
const navigate = useNavigate();

const userIsRegistered: boolean = congregationsPersons.some(
(person) =>
person.profile.user_local_uid === currentPersonDetails.person_uid
);

const currentPersonInCongragation: CongregationUserType =
congregationsPersons.find(
(person) =>
person.profile.user_local_uid === currentPersonDetails.person_uid
);
Comment on lines +22 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix typo in variable name and optimize lookups

  1. The variable name has a typo: "Congragation" should be "Congregation"
  2. This lookup could be combined with the registration check to avoid multiple array iterations
- const currentPersonInCongragation: CongregationUserType =
+ const currentPersonInCongregation: CongregationUserType =
    congregationsPersons.find(
      (person) =>
        person.profile.user_local_uid === currentPersonDetails.person_uid
    );

Consider combining the lookups:

const personLookupResult = useMemo(() => {
  const person = congregationsPersons.find(
    (p) => p.profile.user_local_uid === currentPersonDetails.person_uid
  );
  return {
    userIsRegistered: !!person,
    currentPersonInCongregation: person
  };
}, [congregationsPersons, currentPersonDetails.person_uid]);


const getTextForAppPersonProfileDesc = () => {
if (userIsRegistered) {
const lastTimeOnline = currentPersonInCongragation.sessions[0]?.last_seen;

const formattedLastTimeOnline = lastTimeOnline
? formatDate(new Date(lastTimeOnline), shortDateFormat)
: t('tr_notYet');

return t('tr_appUserProfileRegisteredDesc', {
lastTimeOnline: formattedLastTimeOnline,
});
}

return t('tr_appUserProfileNotRegisteredDesc');
};

const navigateToManageAccess = () => {
if (userIsRegistered) {
navigate(`/manage-access/${currentPersonInCongragation.id}`);
return;
}

navigate(`/manage-access/`);
return;
};

return {
userIsRegistered,
currentPersonInCongragation,
getTextForAppPersonProfileDesc,
navigateToManageAccess,
};
};
Comment on lines +54 to +60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Update return value to match renamed variable

After fixing the typo in the variable name, ensure the return value is updated accordingly.

  return {
    userIsRegistered,
-   currentPersonInCongragation,
+   currentPersonInCongregation,
    getTextForAppPersonProfileDesc,
    navigateToManageAccess,
  };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return {
userIsRegistered,
currentPersonInCongragation,
getTextForAppPersonProfileDesc,
navigateToManageAccess,
};
};
return {
userIsRegistered,
currentPersonInCongregation,
getTextForAppPersonProfileDesc,
navigateToManageAccess,
};
};


export default usePersonAppPersonProfile;
6 changes: 6 additions & 0 deletions src/locales/en/general.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@
"tr_circuit": "Circuit: {{ circuitNumber }}",
"tr_ageInYearsAndMonths": "{{ years }} years, {{months}} months",
"tr_personHasNoAssignmentHistory": "This person has no assignment history yet",
"tr_appUserProfile": "App user profile",
"tr_appUserProfileNotRegisteredDesc": "This person isn't using the Organized app yet. You can create a user profile for this person on the Manage Access page.",
"tr_appUserProfileRegisteredDesc": "This person is already using the Organized app. Last time online: <span class='h4'>{{ lastTimeOnline }}</span>. Edit other user profile settings on the Manage Access page.",
"tr_notYet": "Not yet",
"tr_manageUserProfileSettings": "Manage user profile settings",
"tr_createUserProfile": "Create user profile",
"tr_download": "Download",
"tr_import": "Import"
}
2 changes: 2 additions & 0 deletions src/pages/persons/person_details/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useCurrentUser,
} from '@hooks/index';
import {
PersonAppUserProfile,
PersonAssignment,
PersonAssignmentsHistory,
PersonBasicInfo,
Expand Down Expand Up @@ -53,6 +54,7 @@ const PersonDetails = () => {
}}
>
<PersonBasicInfo />
{!isNewPerson && <PersonAppUserProfile />}
<PersonSpiritualStatus />

{isBaptized && (
Expand Down
Loading