From 15a897ab291cb82a97d4850a0eea5b01cb546dce Mon Sep 17 00:00:00 2001 From: Lea Renaux Date: Thu, 4 Jan 2024 15:32:41 +0100 Subject: [PATCH] review (rename state and rework functions) --- src/components/CampaignPortal/CampaignPortal.jsx | 5 ++--- src/components/ModalSelection/ModalSelection.jsx | 12 ++++++------ src/components/View/View.jsx | 6 ++++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/components/CampaignPortal/CampaignPortal.jsx b/src/components/CampaignPortal/CampaignPortal.jsx index d91c7ab..09d3b89 100644 --- a/src/components/CampaignPortal/CampaignPortal.jsx +++ b/src/components/CampaignPortal/CampaignPortal.jsx @@ -41,9 +41,8 @@ function CampaignPortal({ if (!survey && location.survey) { 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); } diff --git a/src/components/ModalSelection/ModalSelection.jsx b/src/components/ModalSelection/ModalSelection.jsx index 6088b97..3ff4f4f 100644 --- a/src/components/ModalSelection/ModalSelection.jsx +++ b/src/components/ModalSelection/ModalSelection.jsx @@ -6,12 +6,12 @@ import SurveySelector from '../SurveySelector/SurveySelector'; import InterviewerSelector from '../InterviewerSelector/InterviewerSelector'; function ModalSelection({ - linkTo, title, show, setShow, dataRetreiver, interviewerMode, campaigns, preferences + linkTo, title, show, setShow, dataRetreiver, interviewerMode, campaigns = [], preferences }) { const [redirect, setRedirect] = useState(null); const [surveys, setSurveys] = useState(null); const [interviewers, setInterviewers] = useState(null); - const [newCampaigns, setNewCampaigns] = useState(campaigns ?? []); + const [campaignsWithUpdatedPreferences, setCampaignsWithUpdatedPreferences] = useState(campaigns); const updateCampaignsWithPreferences = () => { return campaigns.map((campaign) => { @@ -32,18 +32,18 @@ function ModalSelection({ (survey) => survey.preference, ), }); - }, newCampaigns); - }, [dataRetreiver, newCampaigns]); + }, campaignsWithUpdatedPreferences); + }, [dataRetreiver, campaignsWithUpdatedPreferences]); useEffect(() => { - setNewCampaigns(updateCampaignsWithPreferences()) + setCampaignsWithUpdatedPreferences(updateCampaignsWithPreferences()) }, [preferences]); const updateInterviewers = useCallback(() => { dataRetreiver.getInterviewers((data) => { setInterviewers({ allInterviewers: data }); }); - }, [dataRetreiver, newCampaigns]); + }, [dataRetreiver, campaignsWithUpdatedPreferences]); useEffect(() => { if (interviewerMode) { updateInterviewers(); diff --git a/src/components/View/View.jsx b/src/components/View/View.jsx index cd0ba2c..13b6313 100644 --- a/src/components/View/View.jsx +++ b/src/components/View/View.jsx @@ -38,7 +38,9 @@ class View extends React.Component { } componentDidUpdate(prevProps, prevState) { - if(this.state.campaigns.length !== prevState.campaigns.length ){ + if( !this.state.campaigns.every((element) => prevState.campaigns.includes(element)) + || !prevState.campaigns.every((element) => this.state.campaigns.includes(element)) + ){ this.loadPreferences(); } } @@ -71,7 +73,7 @@ class View extends React.Component { getAllCampaignsData() { this.dataRetreiver.getAllCampaigns((data) => { - data ? this.setState({campaigns: data}) : this.setState({campaigns: []}) + this.setState({campaigns: data ?? []}); }) }