Skip to content

Commit

Permalink
SW-5331 Show loading page when determining module projects (#2607)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommylau523 committed May 13, 2024
1 parent 754d895 commit 9434e4a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/providers/Participant/ParticipantContext.tsx
Expand Up @@ -11,8 +11,8 @@ export type ParticipantData = {
moduleProjects: Project[];
modules?: Module[];
participantProjects: Project[];
orgHasModules: boolean;
orgHasParticipants: boolean;
orgHasModules: boolean | undefined; // undefined for unknown, useful for showing loading spinner
orgHasParticipants: boolean | undefined; // undefined for unknown, useful for showing loading spinner
setCurrentParticipantProject: (projectId: string | number) => void;
};

Expand Down
28 changes: 15 additions & 13 deletions src/providers/Participant/ParticipantProvider.tsx
Expand Up @@ -22,22 +22,22 @@ const ParticipantProvider = ({ children }: Props) => {
const { selectedOrganization } = useOrganization();
const { activeLocale } = useLocalization();

const [activeModules, setActiveModules] = useState<Module[]>([]);
const [currentParticipantProject, setCurrentParticipantProject] = useState<Project>();
const [participantProjects, setParticipantProjects] = useState<Project[]>([]);
const [moduleProjects, setModuleProjects] = useState<Project[]>([]);
const [modules, setModules] = useState<Module[]>([]);
const [activeModules, setActiveModules] = useState<Module[]>([]);
const [orgHasModules, setOrgHasModules] = useState<boolean | undefined>(undefined);
const [orgHasParticipants, setOrgHasParticipants] = useState<boolean | undefined>(undefined);

const [listModulesRequest, setListModulesRequest] = useState<string>('');
const [listModuleProjectsRequest, setListModuleProjectsRequest] = useState<string>('');

const moduleProjectsList = useAppSelector(selectModuleProjects(listModuleProjectsRequest));
const participant = useAppSelector(selectParticipant(currentParticipantProject?.participantId || -1));

const projectModuleList = useAppSelector(selectProjectModuleList(listModulesRequest));
const projects = useAppSelector(selectProjects);

const moduleProjectsList = useAppSelector(selectModuleProjects(listModuleProjectsRequest));

const _setCurrentParticipantProject = useCallback(
(projectId: string | number) => {
setCurrentParticipantProject(participantProjects.find((project) => project.id === Number(projectId)));
Expand All @@ -49,16 +49,18 @@ const ParticipantProvider = ({ children }: Props) => {
activeModules,
moduleProjects,
modules,
orgHasModules,
orgHasParticipants,
participantProjects,
orgHasParticipants: false,
orgHasModules: false,
setCurrentParticipantProject: _setCurrentParticipantProject,
});

useEffect(() => {
if (selectedOrganization && activeLocale) {
setCurrentParticipantProject(undefined);
setModuleProjects([]);
setOrgHasModules(undefined);
setOrgHasParticipants(undefined);
setParticipantProjects([]);
dispatch(requestProjects(selectedOrganization.id, activeLocale));
}
Expand All @@ -67,6 +69,7 @@ const ParticipantProvider = ({ children }: Props) => {
useEffect(() => {
const nextParticipantProjects = (projects || []).filter((project) => !!project.participantId);
setParticipantProjects(nextParticipantProjects);
setOrgHasParticipants(nextParticipantProjects.length > 0);
}, [projects]);

useEffect(() => {
Expand All @@ -77,11 +80,7 @@ const ParticipantProvider = ({ children }: Props) => {
}, [selectedOrganization, dispatch]);

useEffect(() => {
if (!projectModuleList) {
return;
}

if (projectModuleList.status === 'success') {
if (projectModuleList && projectModuleList.status === 'success') {
const nextModules = projectModuleList.data ?? [];
setModules(nextModules);
setActiveModules(nextModules.filter((module) => module.isActive === true));
Expand All @@ -95,6 +94,7 @@ const ParticipantProvider = ({ children }: Props) => {
.filter((project): project is Project => !!project);

setModuleProjects(nextModuleProjects);
setOrgHasModules(nextModuleProjects.length > 0);

// Assign the first project with modules as the current participant project
if (nextModuleProjects.length > 0 && !currentParticipantProject) {
Expand All @@ -119,15 +119,17 @@ const ParticipantProvider = ({ children }: Props) => {
moduleProjects,
modules,
participantProjects,
orgHasModules: moduleProjects.length > 0,
orgHasParticipants: participantProjects.length > 0,
orgHasModules: orgHasModules,
orgHasParticipants: orgHasParticipants,
setCurrentParticipantProject: _setCurrentParticipantProject,
});
}, [
activeModules,
currentParticipantProject,
moduleProjects,
modules,
orgHasModules,
orgHasParticipants,
participant,
participantProjects,
_setCurrentParticipantProject,
Expand Down
11 changes: 7 additions & 4 deletions src/scenes/Home/index.tsx
Expand Up @@ -3,6 +3,7 @@ import React, { useMemo } from 'react';
import { Theme } from '@mui/material';
import { makeStyles } from '@mui/styles';

import Page from 'src/components/Page';
import { useParticipantData } from 'src/providers/Participant/ParticipantContext';
import useDeviceInfo from 'src/utils/useDeviceInfo';

Expand Down Expand Up @@ -40,10 +41,12 @@ export default function Home(): JSX.Element {
const { orgHasModules } = useParticipantData();
const classes = useStyles({ isMobile });

const homeScreen = useMemo(
(): JSX.Element => (orgHasModules ? <ParticipantHomeView /> : <TerrawareHomeView />),
[orgHasModules]
);
const homeScreen = useMemo((): JSX.Element => {
if (orgHasModules === undefined) {
return <Page isLoading={true} />;
}
return orgHasModules ? <ParticipantHomeView /> : <TerrawareHomeView />;
}, [orgHasModules]);

return <main className={classes.main}>{homeScreen}</main>;
}

0 comments on commit 9434e4a

Please sign in to comment.