Skip to content

Commit

Permalink
components refactoring (#93)
Browse files Browse the repository at this point in the history
* components refactoring

* remove useless rerender in CampaignPortal

* rework interviewer condition

* review (rename state and rework functions)

* bump version
  • Loading branch information
RenauxLeaInsee authored Jan 5, 2024
1 parent ae314b3 commit e46e441
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 732 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sonor",
"version": "0.5.24",
"version": "0.5.25",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
Expand Down
3 changes: 2 additions & 1 deletion src/components/App/App.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ const updatePreferences = jest.fn((newPrefs, cb) => {

DataFormatter.mockImplementation(() => ({
getPreferences: (c) => (c(preferences)),
getAllCampaigns: (c) => (c(mainScreenData)),
getQuestionnaireId: (id, c) => (c({ questionnaireId: 'QXT55' })),
getDataForMainScreen: (a, c) => {
getFormattedCampaignsForMainScreen: (a, c) => {
if (c) { c(mainScreenData); }
return Promise.resolve(mainScreenData);
},
Expand Down
15 changes: 6 additions & 9 deletions src/components/CampaignPortal/CampaignPortal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import D from '../../i18n';
import './CampaignPortal.css';

function CampaignPortal({
location, dataRetreiver,
location, dataRetreiver, campaigns
}) {
const initialData = {};
initialData.interviewers = [];
Expand All @@ -39,17 +39,14 @@ function CampaignPortal({

useEffect(() => {
if (!survey && location.survey) {
dataRetreiver.getDataForMainScreen(null, (campaignsData) => {
dataRetreiver.getFormattedCampaignsForMainScreen(null, (campaignsData) => {
const newSurvey = campaignsData.find((s) => s.id === location.survey.id);
newSurvey.allSurveys = campaignsData;
setSurvey(newSurvey);
setSurveyInfo(campaignsData.find((s) => s.id === location.survey.id));
setSurvey({...newSurvey, allSurveys : campaignsData});
setSurveyInfo(newSurvey);
setRedirect(null);
});
}, campaigns);
}
}, [redirect, dataRetreiver, location, survey]);

useEffect(() => {
if (survey) {
setIsLoading(true);
dataRetreiver.getDataForCampaignPortal(!survey || survey.id, (res) => {
Expand All @@ -58,7 +55,7 @@ function CampaignPortal({
setIsLoading(false);
});
}
}, [redirect, dataRetreiver, location, survey]);
}, [survey]);

function handleSort(property, asc) {
const [sortedData, newSort] = Utils.handleSort(property, data, sort, 'campaignPortal', asc);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Close/CloseSUTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function makeTableForExport(data) {
return header.concat(data.map((line) => ([
line.campaign,
line.id,
`${line.interviewer.interviewerLastName} ${line.interviewer.interviewerFirstName}`,
`${line.interviewer?.interviewerLastName ?? ""} ${line.interviewer?.interviewerFirstName ?? ""}`,
line.ssech,
line.location ? line.location.substring(0, 2) : null,
line.city,
Expand Down
4 changes: 2 additions & 2 deletions src/components/CollectionTable/CollectionTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CollectionTable extends React.Component {

async refreshData() {
this.setState({ loading: true });
const { dataRetreiver, location } = this.props;
const { dataRetreiver, location, campaigns } = this.props;
const { survey, interviewer } = location;
const { date } = this.state;
const dateToUse = date || new Date().toISOString().slice(0, 10);
Expand All @@ -64,7 +64,7 @@ class CollectionTable extends React.Component {
if (interviewer) {
surveyToUse = interviewer;
} else if (modeToUse !== C.BY_INTERVIEWER_ONE_SURVEY && modeToUse !== C.BY_SITE) {
surveyToUse = await dataRetreiver.getDataForMainScreen();
surveyToUse = await dataRetreiver.getFormattedCampaignsForMainScreen(null, null, campaigns);
} else {
surveyToUse = survey;
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/CollectionTable/CollectionTable.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const TestingRouter = ({ ComponentWithRedirection }) => (
</Router>
);

const mockGetDataForMainScreen = jest.fn(() => (Promise.resolve(mainScreenData)));
const mockGetFormattedCampaignsForMainScreen = jest.fn(() => (mainScreenData));

const mockGetDataForCollectionTable = jest.fn(
(survey, date, pagination, mode, cb) => {
Expand All @@ -82,7 +82,7 @@ const mockGetDataForCollectionTable = jest.fn(

const mockDataFormatter = DataFormatter.mockImplementation(() => ({
getDataForCollectionTable: mockGetDataForCollectionTable,
getDataForMainScreen: mockGetDataForMainScreen,
getFormattedCampaignsForMainScreen: mockGetFormattedCampaignsForMainScreen
}));

const mockDataRetreiver = new DataFormatter();
Expand Down Expand Up @@ -314,7 +314,7 @@ it('Reloading the page with no survey set (F5) by survey (Site > Progress)', asy
await waitForElement(() => screen.getByTestId('pagination-nav'));

// Should call getDataForMainScreen and display the page anyway
expect(mockGetDataForMainScreen).toHaveBeenCalled();
expect(mockGetFormattedCampaignsForMainScreen).toHaveBeenCalled();
expect(component).toMatchSnapshot();
});

Expand Down
4 changes: 3 additions & 1 deletion src/components/Header/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { version } from '../../../package.json';
import './Header.css';

function Header({
user, showPreferences, dataRetreiver,
user, showPreferences, dataRetreiver, campaigns, preferences
}) {
const [showFollowDropdown, setShowFollowDropdown] = useState(false);
const [showReviewDropdown, setShowReviewDropdown] = useState(false);
Expand Down Expand Up @@ -160,6 +160,8 @@ function Header({
title={modalTitle}
linkTo={modalLink}
interviewerMode={modalInterviewerMode}
campaigns={campaigns}
preferences={preferences}
/>
</header>
);
Expand Down
4 changes: 2 additions & 2 deletions src/components/Header/Header.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ DataFormatter.mockImplementation(() => ({
getPreferences: (c) => (c(preferences)),
getInterviewers: (c) => (c(interviewers)),
getQuestionnaireId: (id, c) => (c({ questionnaireId: 'QXT55' })),
getDataForMainScreen: (a, c) => {
getFormattedCampaignsForMainScreen: (a, c, mainScreenData) => {
if (c) { c(mainScreenData); }
return Promise.resolve(mainScreenData);
},
Expand Down Expand Up @@ -97,7 +97,7 @@ jest.mock('react-router-dom', () => ({

const TestingRouter = () => (
<Router history={history}>
<Header user={userData} showPreferences={() => { mockShowPreferences(); }} dataRetreiver={dataRetreiver} />
<Header user={userData} showPreferences={() => { mockShowPreferences(); }} dataRetreiver={dataRetreiver} campaigns={mainScreenData} preferences={preferences}/>
<Switch>
<Route
path="*"
Expand Down
6 changes: 3 additions & 3 deletions src/components/MainScreen/MainScreen.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ class MainScreen extends React.Component {
}

fetchData() {
const { preferences, dataRetreiver } = this.props;
const { preferences, dataRetreiver, campaigns } = this.props;
this.setState({ isLoading: true });
dataRetreiver.getDataForMainScreen(null, (data) => {
dataRetreiver.getFormattedCampaignsForMainScreen(null, (data) => {
this.setState({
isLoading: false,
data: data.filter(
(survey) => (preferences[survey.id] && preferences[survey.id].preference),
),
}, () => { this.handleSort('label', true); });
});
}, campaigns );
}

handlePageChange(pagination) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/MainScreen/MainScreen.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const TestingRouter = ({ ComponentWithRedirection }) => (
);

DataFormatter.mockImplementation(() => ({
getDataForMainScreen: (a, c) => (c(data)),
getFormattedCampaignsForMainScreen: (a, c) => (c(data)),
}));

const mockDataRetreiver = new DataFormatter();
Expand Down
28 changes: 22 additions & 6 deletions src/components/ModalSelection/ModalSelection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,44 @@ import SurveySelector from '../SurveySelector/SurveySelector';
import InterviewerSelector from '../InterviewerSelector/InterviewerSelector';

function ModalSelection({
linkTo, title, show, setShow, dataRetreiver, interviewerMode,
linkTo, title, show, setShow, dataRetreiver, interviewerMode, campaigns = [], preferences
}) {
const [redirect, setRedirect] = useState(null);
const [surveys, setSurveys] = useState(null);
const [interviewers, setInterviewers] = useState(null);
const [campaignsWithUpdatedPreferences, setCampaignsWithUpdatedPreferences] = useState(campaigns);

const updateCampaignsWithPreferences = () => {
return campaigns.map((campaign) => {
const associatedPreference = Object.entries(preferences).find((preference) => preference[0] === campaign.id)

if(associatedPreference){
campaign.preference = associatedPreference[1].preference
}

return campaign;
})
}

const updateSurveys = useCallback(() => {
dataRetreiver.getDataForMainScreen(null, (data) => {
dataRetreiver.getFormattedCampaignsForMainScreen(null, (data) => {
setSurveys({
allSurveys: data.filter(
(survey) => survey.preference,
),
});
});
}, [dataRetreiver]);
}, campaignsWithUpdatedPreferences);
}, [dataRetreiver, campaignsWithUpdatedPreferences]);

useEffect(() => {
setCampaignsWithUpdatedPreferences(updateCampaignsWithPreferences())
}, [preferences]);

const updateInterviewers = useCallback(() => {
dataRetreiver.getInterviewers((data) => {
setInterviewers({ allInterviewers: data });
});
}, [dataRetreiver]);

}, [dataRetreiver, campaignsWithUpdatedPreferences]);
useEffect(() => {
if (interviewerMode) {
updateInterviewers();
Expand Down
6 changes: 3 additions & 3 deletions src/components/MonitoringTable/MonitoringTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MonitoringTable extends React.Component {

async refreshData() {
this.setState({ loading: true });
const { dataRetreiver, location } = this.props;
const { dataRetreiver, location, campaigns } = this.props;
const { survey, interviewer } = location;
const { date } = this.state;
const dateToUse = date || new Date().toISOString().slice(0, 10);
Expand All @@ -63,7 +63,7 @@ class MonitoringTable extends React.Component {
if (interviewer) {
surveyToUse = interviewer;
} else if (modeToUse !== C.BY_INTERVIEWER_ONE_SURVEY && modeToUse !== C.BY_SITE) {
surveyToUse = await dataRetreiver.getDataForMainScreen();
surveyToUse = await dataRetreiver.getFormattedCampaignsForMainScreen(null,null, campaigns);
} else {
surveyToUse = survey;
}
Expand Down Expand Up @@ -99,7 +99,7 @@ class MonitoringTable extends React.Component {
this.handleSort(firstColumnSortAttribute, true);
});
}
},
}, campaigns
);
} else {
this.setState({ redirect: '/' });
Expand Down
10 changes: 6 additions & 4 deletions src/components/MonitoringTable/MonitoringTable.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const mockGetDataForMonitoringTable = jest.fn(

const mockDataFormatter = DataFormatter.mockImplementation(() => ({
getDataForMonitoringTable: mockGetDataForMonitoringTable,
getDataForMainScreen: mockGetDataForMainScreen,
getFormattedCampaignsForMainScreen: mockGetDataForMainScreen,
}));

const mockDataRetreiver = new DataFormatter();
Expand Down Expand Up @@ -203,7 +203,7 @@ it('Select another date', async () => {

const component = render(
<Router history={history}>
<MonitoringTable location={{ survey, pathname }} dataRetreiver={mockDataRetreiver} />
<MonitoringTable location={{ survey, pathname }} dataRetreiver={mockDataRetreiver} campaigns={mainScreenData} />
</Router>,
);

Expand All @@ -216,6 +216,7 @@ it('Select another date', async () => {
expect.anything(),
expect.anything(),
expect.anything(),
mainScreenData
);

// And the page should render correctly
Expand All @@ -228,13 +229,13 @@ it('Component did update', () => {

const { container } = render(
<Router history={history}>
<MonitoringTable location={{ survey, pathname }} dataRetreiver={mockDataRetreiver} />
<MonitoringTable location={{ survey, pathname }} dataRetreiver={mockDataRetreiver} campaigns={mainScreenData} />
</Router>,
);

render(
<Router history={history}>
<MonitoringTable location={{ survey, pathname: '/follow/sites/simpsons2020x00' }} dataRetreiver={mockDataRetreiver} />
<MonitoringTable location={{ survey, pathname: '/follow/sites/simpsons2020x00' }} dataRetreiver={mockDataRetreiver} campaigns={mainScreenData}/>
</Router>,
{ container },
);
Expand All @@ -246,6 +247,7 @@ it('Component did update', () => {
expect.anything(),
C.BY_SITE,
expect.anything(),
mainScreenData
);
})

Expand Down
4 changes: 2 additions & 2 deletions src/components/Review/Review.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Utils from "../../utils/Utils";
import D from "../../i18n";
import "./Review.css";

function Review({ location, dataRetreiver, match }) {
function Review({ location, dataRetreiver, match, campaigns }) {
const { survey } = location;
const { id } = match.params;
const [data, setData] = useState([]);
Expand All @@ -29,7 +29,7 @@ function Review({ location, dataRetreiver, match }) {
setData(res);
setRedirect(null);
setIsLoading(false);
});
}, campaigns);
}, [dataRetreiver, survey]);

useEffect(() => {
Expand Down
33 changes: 25 additions & 8 deletions src/components/View/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,29 @@ class View extends React.Component {
preferences: {},
redirect: null,
loadingPreferences: true,
campaigns: []
};
this.dataRetreiver = new DataFormatter(props.keycloak);
}

componentDidMount() {
this.componentIsMounted = true;
this.loadPreferences();
this.getAllCampaignsData();
}

componentDidUpdate(prevProps, prevState) {
if( !this.state.campaigns.every((element) => prevState.campaigns.includes(element))
|| !prevState.campaigns.every((element) => this.state.campaigns.includes(element))
){
this.loadPreferences();
}
}

loadPreferences() {
this.setState({ loadingPreferences: true });
this.dataRetreiver.getPreferences((preferences) => {
this.setState({ preferences, loadingPreferences: false, redirect: <Redirect to="/" /> });
});
}, this.state.campaigns);
}

updatePreferences(newPreferences) {
Expand All @@ -62,9 +71,15 @@ class View extends React.Component {
this.setState({ showPreferences: false });
}

getAllCampaignsData() {
this.dataRetreiver.getAllCampaigns((data) => {
this.setState({campaigns: data ?? []});
})
}

render() {
const {
showPreferences, preferences, redirect, loadingPreferences,
showPreferences, preferences, redirect, loadingPreferences, campaigns
} = this.state;
const { userData } = this.props;
return (
Expand All @@ -78,19 +93,21 @@ class View extends React.Component {
showPreferences={() => {
this.showPreferences();
}}
campaigns={campaigns}
preferences={preferences}
/>
<Switch>
<Route path="/review/:id?" component={(routeProps) => <Review dataRetreiver={this.dataRetreiver} {...routeProps} />} />
<Route path="/review/:id?" component={(routeProps) => <Review dataRetreiver={this.dataRetreiver} {...routeProps} campaigns={campaigns} />} />
<Route path="/followUp" component={(routeProps) => <Remind dataRetreiver={this.dataRetreiver} {...routeProps} />} />
<Route path="/follow" component={(routeProps) => <MonitoringTable dataRetreiver={this.dataRetreiver} {...routeProps} />} />
<Route path="/collection" component={(routeProps) => <CollectionTable dataRetreiver={this.dataRetreiver} {...routeProps} />} />
<Route path="/follow" component={(routeProps) => <MonitoringTable dataRetreiver={this.dataRetreiver} {...routeProps} campaigns={campaigns} />} />
<Route path="/collection" component={(routeProps) => <CollectionTable dataRetreiver={this.dataRetreiver} {...routeProps} campaigns={campaigns} />} />
<Route path="/provisionalstatus" component={(routeProps) => <ProvisionalStatusTable dataRetreiver={this.dataRetreiver} {...routeProps} />} />
<Route path="/terminated/:id" component={(routeProps) => <Terminated dataRetreiver={this.dataRetreiver} {...routeProps} />} />
<Route path="/listSU/:id" component={(routeProps) => <ListSU dataRetreiver={this.dataRetreiver} {...routeProps} />} />
<Route path="/portal/:id" component={(routeProps) => <CampaignPortal dataRetreiver={this.dataRetreiver} {...routeProps} />} />
<Route path="/portal/:id" component={(routeProps) => <CampaignPortal dataRetreiver={this.dataRetreiver} {...routeProps} campaigns={campaigns}/>} />
<Route path="/notifications" component={(routeProps) => <Notifications dataRetreiver={this.dataRetreiver} {...routeProps} user={userData} />} />
<Route path="/close" component={(routeProps) => <Close dataRetreiver={this.dataRetreiver} {...routeProps} />} />
<Route path="/" component={() => <MainScreen preferences={preferences} loadingPreferences={loadingPreferences} dataRetreiver={this.dataRetreiver} />} />
<Route path="/" component={() => <MainScreen preferences={preferences} loadingPreferences={loadingPreferences} dataRetreiver={this.dataRetreiver} campaigns={campaigns} />} />
</Switch>
</div>
</Router>
Expand Down
Loading

0 comments on commit e46e441

Please sign in to comment.