Skip to content

Commit

Permalink
review (rename state and rework functions)
Browse files Browse the repository at this point in the history
  • Loading branch information
RenauxLeaInsee committed Jan 4, 2024
1 parent 80d8917 commit 15a897a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
5 changes: 2 additions & 3 deletions src/components/CampaignPortal/CampaignPortal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
12 changes: 6 additions & 6 deletions src/components/ModalSelection/ModalSelection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions src/components/View/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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 ?? []});
})
}

Expand Down

0 comments on commit 15a897a

Please sign in to comment.