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 ceab724 commit 22d4e14
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 497 deletions.
13 changes: 7 additions & 6 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { StyledEngineProvider, ThemeProvider } from '@mui/material/styles';
import React, { useEffect, useState } from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { Routes, Route, BrowserRouter } from 'react-router';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import React, { useEffect, useState } from 'react';
import { BrowserRouter, Route, Routes } from 'react-router';
import styled from 'styled-components';
import { PrivateRoute } from './js/auth';
import muiTheme from './js/common/components/Style/muiTheme';
// import LoadingWheelComp from './js/common/components/Widgets/LoadingWheelComp';
import { normalizedHref } from './js/common/utils/hrefUtils';
import initializejQuery from './js/common/utils/initializejQuery';
import { renderLog } from './js/common/utils/logging';
import Login from './js/pages/Login';
import { PrivateRoute } from './js/auth';
import ConnectAppContext from './js/contexts/ConnectAppContext';
import Drawers from './js/components/Drawers/Drawers';
import ConnectAppContext from './js/contexts/ConnectAppContext';
import Login from './js/pages/Login';


// Root URL pages
Expand Down Expand Up @@ -88,6 +88,7 @@ function App () {
<Route path="*" element={<PageNotFound />} />
</Routes>
{/* Hack 1/14/25 <Footer /> */}
<ReactQueryDevtools />
</WeVoteBody>
</BrowserRouter>
</ThemeProvider>
Expand Down
21 changes: 12 additions & 9 deletions src/js/common/utils/requestParamsUtils.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const makeRequestParams = (fixedPreamble = '', data) => {
let requestParams = '';
if (fixedPreamble.length) {
requestParams += `${fixedPreamble}&`;
}
const makeRequestParams = (plainParams, data) => {
const expandedParams = {};
Object.keys(plainParams).forEach((key) => {
expandedParams[`${key}`] = plainParams[key];
});
Object.keys(data).forEach((key) => {
requestParams += `${key}ToBeSaved=${data[key]}&`;
requestParams += `${key}Changed=${true}&`;
expandedParams[`${key}ToBeSaved`] = data[key];
expandedParams[`${key}Changed`] = 'true';
});
requestParams = requestParams.slice(0, -1);
return encodeURI(requestParams);
const queryString = Object.entries(expandedParams)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&');

return queryString;
};

export default makeRequestParams;
4 changes: 2 additions & 2 deletions src/js/components/Drawers/EditQuestionDrawer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const EditQuestionDrawer = () => {

useEffect(() => { // Replaces onAppObservableStoreChange and will be called whenever the context value changes
console.log('EditQuestionDrawer: Context value changed:', true);
const questionIdTemp = getAppContextValue('editQuestionDrawerQuestionId');
if (questionIdTemp >= 0) {
const question = getAppContextValue('selectedQuestion');
if (question && question.id >= 0) {
setHeaderTitleJsx(<>Edit Question</>);
} else {
setHeaderTitleJsx(<>Add Question</>);
Expand Down
4 changes: 2 additions & 2 deletions src/js/components/Drawers/EditQuestionnaireDrawer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const EditQuestionnaireDrawer = () => {
useEffect(() => { // Replaces onAppObservableStoreChange and will be called whenever the context value changes
if (getAppContextValue('editQuestionnaireDrawerOpen')) {
console.log('EditQuestionnaireDrawer: Context value changed:', true);
const questionnaireIdTemp = getAppContextValue('editQuestionnaireDrawerQuestionnaireId');
if (questionnaireIdTemp >= 0) {
const questionnaire = getAppContextValue('selectedQuestionnaire');
if (questionnaire) {
setHeaderTitleJsx(<>Edit Questionnaire</>);
} else {
setHeaderTitleJsx(<>Add Questionnaire</>);
Expand Down
6 changes: 3 additions & 3 deletions src/js/components/Person/PersonProfile.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { withStyles } from '@mui/styles';
import PropTypes from 'prop-types';
import React, { useEffect } from 'react';
import styled from 'styled-components';
import PropTypes from 'prop-types';
import { withStyles } from '@mui/styles';
import { useConnectAppContext } from '../../contexts/ConnectAppContext';
import { renderLog } from '../../common/utils/logging';
import { useConnectAppContext } from '../../contexts/ConnectAppContext';
import CopyQuestionnaireLink from '../Questionnaire/CopyQuestionnaireLink';


Expand Down
6 changes: 3 additions & 3 deletions src/js/components/Person/PersonProfileDrawerMainContent.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { withStyles } from '@mui/styles';
import PropTypes from 'prop-types';
import React from 'react';
import styled from 'styled-components';
import PropTypes from 'prop-types';
import { withStyles } from '@mui/styles';
import { renderLog } from '../../common/utils/logging';
import PersonProfile from './PersonProfile';
import QuestionnaireResponsesList from '../Questionnaire/QuestionnaireResponsesList';
import PersonProfile from './PersonProfile';


// eslint-disable-next-line no-unused-vars
Expand Down
Loading

0 comments on commit 22d4e14

Please sign in to comment.