Skip to content

Commit

Permalink
Merged react-router v6 to v7, and replaces flux with react-query
Browse files Browse the repository at this point in the history
Updates the underlying TeamMemberList on the "page", when the button is pressed in a drawer to add/create/delete staff.

Questionnaire and Questions display and editing changed over to react-query.

------------- Pre Jan 8, 2025 comments -----------
First steps toward replacing flux with react-query. react-query looks like it will be much simpler to use and maintain.

This PR is not a full replacement for all the flux stores. This PR compiles, but many existing features do not fully work -- The existing flux based configuration Stores and Actions are not functional components so they have to still use AppObservableStore which is not logically connected to the new replacement ConnectAppContext.jsx Store -- Most of the functional components have been switched to the new useContext() ~ ConnectAppContext.jsx.

The Teams and FAQ pages display, and Login works. You can delete a team member. The Team and Person Store/Actions have been replaced enough for the Teams page to display.

All the code in Stores meant to manage cache is no longer needed -- react-cache does that automatically. I also found that apiCalming() is no longer needed, react-cache 'calms' api queries automatically too. You make the api request as often as you want on a page, and the request get served with cached data. There is a way to force a cache update if needed.

All the breaking changes from react-router 6 and 7 are resolved, this eliminates the need for the 'react-router-dom-v5 compat' library. This router upgrade also nicely simplifies App.jsx.

Some class functions need to be upgraded to functional components to work with useContext(), and more still need upgrading.

As a first step in switching over to useContext -- I replaced AppObservableStore with ConnectAppContext..jsx, which eliminates all the subscriptions that were needed to be created and destroyed for each component. So, many onAppObservableStoreChange() instances were replaced with useEffect(). ConnectAppContext.jsx is just a "modern" global key value store without specific setters/getters.

For all files that I touched, I cleared most of the lint errors.

To simplify this migration, lots of "not yet needed" code is commented out, since I rely heavily on lint errors. Not yet used "useState()" pairs were commented out, partially implemented useState() had the resulting errors "suppressed" with eslint no-unused-vars disables. Unused styled components were commented out.

Removed Storybook (until we actually need it), it had lots of unresolved version dependencies.

Minor note: Boolean props to StyledComponents now need a $ in front of the variable like $largeFont, due to the React upgrade.
  • Loading branch information
SailingSteve committed Jan 17, 2025
1 parent 22d4e14 commit 795f362
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 47 deletions.
21 changes: 4 additions & 17 deletions src/js/components/Person/PersonSummaryRow.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import { withStyles } from '@mui/styles';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import styled from 'styled-components';
import PropTypes from 'prop-types';
import { Delete, Edit } from '@mui/icons-material';
import { withStyles } from '@mui/styles';
import React from 'react';
import styled from 'styled-components';
import DesignTokenColors from '../../common/components/Style/DesignTokenColors';
import { renderLog } from '../../common/utils/logging';
import { useConnectAppContext } from '../../contexts/ConnectAppContext';
import weConnectQueryFn from '../../react-query/WeConnectQuery';
import { DeleteStyled, EditStyled } from '../Style/iconStyles';


const PersonSummaryRow = ({ person, rowNumberForDisplay, teamId }) => {
Expand Down Expand Up @@ -134,19 +134,6 @@ const styles = (theme) => ({
},
});

const DeleteStyled = styled(Delete)`
color: ${DesignTokenColors.neutral200};
width: 20px;
height: 20px;
`;

const EditStyled = styled(Edit)`
color: ${DesignTokenColors.neutral100};
height: 16px;
margin-left: 2px;
width: 16px;
`;

const GraySpan = styled('span')`
color: ${DesignTokenColors.neutral400};
`;
Expand Down
3 changes: 1 addition & 2 deletions src/js/components/Questionnaire/EditQuestionForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const EditQuestionForm = ({ classes }) => {
const [requireAnswerValue, setRequireAnswerValue] = React.useState(false);
const [statusActiveValue, setStatusActiveValue] = React.useState(true);


// eslint-disable-next-line no-unused-vars
const [fieldMappingRuleCopied, setFieldMappingRuleCopied] = React.useState('');
const [saveButtonActive, setSaveButtonActive] = React.useState(false);
Expand Down Expand Up @@ -79,7 +78,7 @@ const EditQuestionForm = ({ classes }) => {
const questionSaveMutation = useMutation({
mutationFn: (requestParams) => weConnectQueryFn('question-save', requestParams),
onSuccess: () => {
// console.log('--------- questionSaveMutation mutated ---------');
// console.log('--------- questionSaveMutation mutated --------- ');
queryClient.invalidateQueries('question-list-retrieve').then(() => {});
},
});
Expand Down
18 changes: 18 additions & 0 deletions src/js/components/Style/iconStyles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Delete, Edit } from '@mui/icons-material';
import styled from 'styled-components';
import DesignTokenColors from '../../common/components/Style/DesignTokenColors';

const DeleteStyled = styled(Delete)`
color: ${DesignTokenColors.neutral200};
width: 20px;
height: 20px;
`;

const EditStyled = styled(Edit)`
color: ${DesignTokenColors.neutral100};
height: 16px;
margin-left: 2px;
width: 16px;
`;

export { DeleteStyled, EditStyled };
39 changes: 32 additions & 7 deletions src/js/components/Team/TeamHeader.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
import { withStyles } from '@mui/styles';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import PropTypes from 'prop-types';
import React from 'react';
import { Link } from 'react-router';
import styled from 'styled-components';
import PropTypes from 'prop-types';
import { withStyles } from '@mui/styles';
import { renderLog } from '../../common/utils/logging';
import weConnectQueryFn from '../../react-query/WeConnectQuery';
import { DeleteStyled, EditStyled } from '../Style/iconStyles';


// eslint-disable-next-line no-unused-vars
const TeamHeader = ({ classes, showHeaderLabels, team }) => {
const TeamHeader = ({ classes, showHeaderLabels, showIcons, team }) => {
renderLog('TeamHeader');

const queryClient = useQueryClient();

const removeTeamMutation = useMutation({
mutationFn: (teamId) => weConnectQueryFn('team-delete', {
teamId,
}),
onSuccess: () => {
console.log('--------- removeMemberMutation mutated ---------');
queryClient.invalidateQueries('team-list-retrieve').then(() => {});
},
});


const removeTeamClick = () => {
removeTeamMutation.mutate(team.id);
};


return (
<OneTeamHeader>
{/* Width (below) of this TeamHeaderCell comes from the combined widths of the first x columns in TeamMemberList */}
Expand All @@ -26,12 +47,16 @@ const TeamHeader = ({ classes, showHeaderLabels, team }) => {
</TeamHeaderCell>
)}
{/* Edit icon */}
{showHeaderLabels && (
<TeamHeaderCell width={20} />
{showHeaderLabels && showIcons && (
<TeamHeaderCell width={20}>
<EditStyled />
</TeamHeaderCell>
)}
{/* Delete icon */}
{showHeaderLabels && (
<TeamHeaderCell width={20} />
{showHeaderLabels && showIcons && (
<TeamHeaderCell width={20} onClick={() => removeTeamClick(team)}>
<DeleteStyled />
</TeamHeaderCell>
)}
</OneTeamHeader>
);
Expand Down
2 changes: 1 addition & 1 deletion src/js/components/Team/TeamMemberList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const TeamMemberList = ({ teamId }) => {
teamMemberList = oneTeam.teamMemberList;
}
} else {
// console.log('no teamListFromContext yet!');
console.log('no teamListFromContext yet!');
}

return (
Expand Down
17 changes: 6 additions & 11 deletions src/js/pages/SystemSettings/Questionnaire.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ const Questionnaire = ({ classes, match }) => {
renderLog('Questionnaire');
const { setAppContextValue, getAppContextValue } = useConnectAppContext();

const dummy = {
questionnaireName: '---',
questionnaireTitle: '---',
questionnaireInstructions: 'Dev note: There is enough info in the URL to handle the edge case of the bookmarking this page, or hard refreshing while on this page... someday...',
};
const [questionList, setQuestionList] = React.useState([]);
const [questionnaire] = React.useState(getAppContextValue('selectedQuestionnaire'));
const [questionnaire] = React.useState(getAppContextValue('selectedQuestionnaire') || dummy);
// eslint-disable-next-line no-unused-vars
const [questionnaireList, setQuestionnaireList] = React.useState([]);

Expand Down Expand Up @@ -64,16 +69,6 @@ const Questionnaire = ({ classes, match }) => {
setAppContextValue('editQuestionnaireDrawerOpen', true);
};

if (!questionnaire) {
return (
<PageContentContainer>
<div style={{ marginTop: '100px' }}>
No questionnaire, due to starting a session on this page with no chance to set &quot;selectedQuestionnaire&quot;<br />
This can be fixed, but good enough for now.<br /> (We have the questionnaire ID in the URL).
</div>
</PageContentContainer>
);
}
return (
<>
<Helmet>
Expand Down
9 changes: 1 addition & 8 deletions src/js/pages/TeamHome.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const TeamHome = ({ classes }) => { // classes, params

const params = useParams();
const [team, setTeam] = React.useState({});
const [teamFound, setTeamFound] = React.useState(false);
const [teamId] = React.useState(params.teamId);
const displayAddDrawer = getAppContextValue('addPersonDrawerOpen');

Expand All @@ -30,12 +29,6 @@ const TeamHome = ({ classes }) => { // classes, params
setTeam(oneTeam);
};

const teamList = getAppContextValue('teamListNested');
if (teamList && !teamFound) { // If you navigate directly to team-home, in a new session 'teamListNested' will not be set. Fixable but low priority.
setTeamFound(true);
updateTeam(teamList);
}

const isAddPersonDrawerOpen = document.getElementById('addPersonDrawer');
const { data, isSuccess, isFetching, isStale } = useFetchData(['team-list-retrieve'], {});
useEffect(() => {
Expand Down Expand Up @@ -95,7 +88,7 @@ const TeamHome = ({ classes }) => { // classes, params
>
Add Team Member
</Button>
<TeamHeader showHeaderLabels={(team.teamMemberList && team.teamMemberList.length > 0)} />
<TeamHeader team={team} showHeaderLabels={(team.teamMemberList && team.teamMemberList.length > 0)} showIcons={false} />
<TeamMemberList teamId={teamId} />
{displayAddDrawer ? <AddPersonDrawer /> : null }
</PageContentContainer>
Expand Down
2 changes: 1 addition & 1 deletion src/js/pages/Teams.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const Teams = ({ classes, match }) => {
</Button>
{teamList.map((team, index) => (
<OneTeamWrapper key={`team-${team.id}`}>
<TeamHeader team={team} showHeaderLabels={(index === 0) && showAllTeamMembers && (team.teamMemberList && team.teamMemberList.length > 0)} />
<TeamHeader team={team} showHeaderLabels={(index === 0) && showAllTeamMembers && (team.teamMemberList && team.teamMemberList.length > 0)} showIcons />
{showAllTeamMembers && (
<TeamMemberList teamId={team.id} teamList={teamList} />
)}
Expand Down

0 comments on commit 795f362

Please sign in to comment.