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
Any UI changes that I have made were to aid debugging, so feel free to reverse.
Some code seemed "in process" and I tried to get it working the same way using re

------------- 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 20, 2025
1 parent 1e33765 commit fcaa748
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 38 deletions.
11 changes: 6 additions & 5 deletions src/js/components/Drawers/AddTeamDrawer.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import React from 'react';
import { withStyles } from '@mui/styles';
// import styled from 'styled-components';
import PropTypes from 'prop-types';
import { withStyles } from '@mui/styles';
import DrawerTemplateA from './DrawerTemplateA';
import React from 'react';
import { renderLog } from '../../common/utils/logging';
import { useConnectAppContext } from '../../contexts/ConnectAppContext';
import AddTeamDrawerMainContent from '../Team/AddTeamDrawerMainContent';
import DrawerTemplateA from './DrawerTemplateA';


// eslint-disable-next-line no-unused-vars
const AddTeamDrawer = () => {
renderLog('AddTeamDrawer');

const { getAppContextValue } = useConnectAppContext();

return (
<DrawerTemplateA
drawerId="addTeamDrawer"
drawerOpenGlobalVariableName="addTeamDrawerOpen"
mainContentJsx={<AddTeamDrawerMainContent />}
headerTitleJsx={<>Add Team</>}
headerTitleJsx={getAppContextValue('AddTeamDrawerLabel')}
headerFixedJsx={<></>}
/>
);
Expand Down
36 changes: 24 additions & 12 deletions src/js/components/Team/AddTeamForm.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import { Button, FormControl, TextField } from '@mui/material';
import { withStyles } from '@mui/styles';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import PropTypes from 'prop-types';
import React, { useRef } from 'react';
import styled from 'styled-components';
import PropTypes from 'prop-types';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { renderLog } from '../../common/utils/logging';
import { useConnectAppContext } from '../../contexts/ConnectAppContext';
import weConnectQueryFn from '../../react-query/WeConnectQuery';

/* global $ */

const AddTeamForm = ({ classes }) => {
renderLog('AddTeamForm');
const { getAppContextValue } = useConnectAppContext();

const teamNameFldRef = useRef('');
const queryClient = useQueryClient();
const [team] = React.useState(getAppContextValue('teamForAddTeamDrawer'));
const [teamNameCached, setTeamNameCached] = React.useState(team && team.teamName);
const [errorText, setErrorText] = React.useState('');

const saveTeamMutation = useMutation({
mutationFn: (team) => weConnectQueryFn(['team-save'], {
teamName: team,
mutationFn: () => weConnectQueryFn(['team-save'], {
teamName: teamNameCached,
teamNameChanged: true,
teamId: '-1',
teamId: team ? team.id : '-1',
}),
onSuccess: () => {
console.log('--------- saveTeamMutation addTeamForm mutated ---------');
Expand All @@ -30,25 +34,27 @@ const AddTeamForm = ({ classes }) => {
const saveNewTeam = () => {
const teamName = teamNameFldRef.current.value;
if (teamName.length === 0) {
$('#teamErrorLine').css('display', 'block').text('Enter a valid team name');
setErrorText('Enter a valid team name');
return;
}
setErrorText('');
setTeamNameCached(teamName);
console.log('saveNewTeam data:', teamName);
saveTeamMutation.mutate(teamName);
saveTeamMutation.mutate();
};

return (
<AddTeamFormWrapper>
<div id="teamErrorLine" style={{ display: 'none', fontWeight: 800, paddingBottom: '10px' }} />
<ErrorTeamLine>{errorText}</ErrorTeamLine>
<FormControl classes={{ root: classes.formControl }}>
<TextField
autoFocus
// classes={{ root: classes.textField }} // Not working yet
defaultValue={teamNameCached}
id="teamNameToBeSaved"
inputRef={teamNameFldRef}
label="Team Name"
name="teamNameToBeSaved"
margin="dense"
inputRef={teamNameFldRef}
placeholder="Team Name"
variant="outlined"
/>
Expand All @@ -58,7 +64,7 @@ const AddTeamForm = ({ classes }) => {
onClick={saveNewTeam}
variant="contained"
>
Save New Team
{team ? 'Save Team' : 'Save New Team'}
</Button>
</FormControl>
</AddTeamFormWrapper>
Expand All @@ -80,6 +86,12 @@ const styles = (theme) => ({
},
});

const ErrorTeamLine = styled('div')`
fontWeight: 800;
paddingBottom: '10px';
color: coral;
`;

const AddTeamFormWrapper = styled('div')`
`;

Expand Down
37 changes: 22 additions & 15 deletions src/js/components/Team/TeamHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import React from 'react';
import { Link } from 'react-router';
import styled from 'styled-components';
import { renderLog } from '../../common/utils/logging';
import { useConnectAppContext } from '../../contexts/ConnectAppContext';
import weConnectQueryFn from '../../react-query/WeConnectQuery';
import { DeleteStyled, EditStyled } from '../Style/iconStyles';


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

const queryClient = useQueryClient();
const [teamLocal] = React.useState(useQueryClient(team || getAppContextValue('teamForAddTeamDrawer')));

const removeTeamMutation = useMutation({
mutationFn: (teamId) => weConnectQueryFn('team-delete', {
Expand All @@ -25,36 +28,40 @@ const TeamHeader = ({ classes, showHeaderLabels, showIcons, team }) => {
},
});


const removeTeamClick = () => {
removeTeamMutation.mutate(team.id);
console.log('removeTeamMutation team: ', teamLocal.id);
removeTeamMutation.mutate(teamLocal.id);
};

const editTeamClick = () => {
console.log('editTeamClick: ', teamLocal);
setAppContextValue('addTeamDrawerOpen', true);
setAppContextValue('AddTeamDrawerLabel', 'Edit Team Name');
setAppContextValue('teamForAddTeamDrawer', teamLocal);
};

return (
<OneTeamHeader>
{/* Width (below) of this TeamHeaderCell comes from the combined widths of the first x columns in TeamMemberList */}
<TeamHeaderCell $largeFont $titleCell width={15 + 150 + 125}>
{team && (
<Link to={`/team-home/${team.id}`}>
{team.teamName}
{teamLocal && (
<Link to={`/team-home/${teamLocal.id}`}>
{teamLocal.teamName}
</Link>
)}
</TeamHeaderCell>
{showHeaderLabels && (
<TeamHeaderCell width={190}>
Title / Volunteering Love
</TeamHeaderCell>
)}
<TeamHeaderCell width={190}>
{showHeaderLabels ? 'Title / Volunteering Love' : ''}
</TeamHeaderCell>
{/* Edit icon */}
{showHeaderLabels && showIcons && (
<TeamHeaderCell width={20}>
{showIcons && (
<TeamHeaderCell width={20} onClick={editTeamClick}>
<EditStyled />
</TeamHeaderCell>
)}
{/* Delete icon */}
{showHeaderLabels && showIcons && (
<TeamHeaderCell width={20} onClick={() => removeTeamClick(team)}>
{showIcons && (
<TeamHeaderCell width={20} onClick={removeTeamClick}>
<DeleteStyled />
</TeamHeaderCell>
)}
Expand All @@ -64,7 +71,7 @@ const TeamHeader = ({ classes, showHeaderLabels, showIcons, team }) => {
TeamHeader.propTypes = {
classes: PropTypes.object.isRequired,
showHeaderLabels: PropTypes.bool,
team: PropTypes.object.isRequired,
team: PropTypes.object,
showIcons: PropTypes.bool,
};

Expand Down
4 changes: 0 additions & 4 deletions src/js/components/Team/TeamMemberList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ const TeamMemberList = ({ teamId }) => {
renderLog('TeamMemberList');
const { getAppContextValue } = useConnectAppContext();

// const [removeTeamMember, { isLoading, error, data }] = RemoveTeamMemberMutation(personId, teamId);
// console.log('RemoveTeamMemberMutation called: ', isLoading, error, data);


let teamMemberList = [];
const teamListFromContext = getAppContextValue('teamListNested');
if (teamListFromContext) {
Expand Down
4 changes: 2 additions & 2 deletions src/js/pages/TeamHome.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const TeamHome = ({ classes }) => { // classes, params
<link rel="canonical" href={`${webAppConfig.WECONNECT_URL_FOR_SEO}/team-home`} />
</Helmet>
<PageContentContainer>
<h1>{team.teamName}</h1>
<h1>{team ? team.teamName : 'none'}</h1>
<div>
Team Home for
{' '}
Expand All @@ -88,7 +88,7 @@ const TeamHome = ({ classes }) => { // classes, params
>
Add Team Member
</Button>
<TeamHeader team={team} showHeaderLabels={(team.teamMemberList && team.teamMemberList.length > 0)} showIcons={false} />
<TeamHeader team={team} showHeaderLabels={(team && team.teamMemberList && team.teamMemberList.length > 0)} showIcons={false} />
<TeamMemberList teamId={teamId} />
{displayAddDrawer ? <AddPersonDrawer /> : null }
</PageContentContainer>
Expand Down
1 change: 1 addition & 0 deletions src/js/pages/Teams.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const Teams = ({ classes, match }) => {

const addTeamClick = () => {
setAppContextValue('addTeamDrawerOpen', true);
setAppContextValue('AddTeamDrawerLabel', 'Add Team');
};

const personProfile = getAppContextValue('personProfileDrawerOpen');
Expand Down

0 comments on commit fcaa748

Please sign in to comment.