From dd70a69f86acb738b7cbcb7a8b1eaf1b36195d3b Mon Sep 17 00:00:00 2001 From: Lea Renaux Date: Fri, 24 May 2024 12:04:51 +0200 Subject: [PATCH 1/3] call isSurveyOnline in Main and use response in menus/responseButton --- src/components/main.js | 43 +++++++++++++++++++--- src/components/template/footer-menu.js | 5 +-- src/components/template/menu.js | 31 +++++++++------- src/components/template/response-button.js | 34 +++-------------- 4 files changed, 62 insertions(+), 51 deletions(-) diff --git a/src/components/main.js b/src/components/main.js index 2496796..04d229e 100644 --- a/src/components/main.js +++ b/src/components/main.js @@ -18,7 +18,7 @@ import AssistanceForm from 'components/forms/assistance-form'; import AssistanceRouter from 'components/forms/assistance-router'; import ControlEmailForm from 'components/forms/mail-modif'; import DonneesPersonnelles from './content/donnees-personnelles'; - +import { isSurveyOnLine } from 'utils/api'; import { idExists, getSurveyTitleById, @@ -40,6 +40,9 @@ class Main extends React.Component { showResponseButton: true, showMinimalFooter: false, id: match.params.id, + isSurveyOnLine: undefined, + messageSurveyOffline: "", + messageInfoSurveyOffline: "" }; } @@ -70,10 +73,31 @@ class Main extends React.Component { } }; + fetchData = async () => { + try { + const response = await isSurveyOnLine(this.props.urlBackEnd)(this.state.id); + if (response.data && response.data.opened === false) { + this.setState({isSurveyOnLine: false}) + this.setState({messageSurveyOffline: response.data.messageSurveyOffline}) + this.setState({messageInfoSurveyOffline: response.data.messageInfoSurveyOffline}) + } else { + this.setState({isSurveyOnLine: true}) + } + } catch (error) { + console.error('Error checking survey online:', error); + this.setState({isSurveyOnLine: false}) + } + }; + + componentDidMount = () => { + this.fetchData(); + } + + /* return */ render() { const { urlBackEnd, match, urlMySurveys, keycloakAuth } = this.props; - const { id, showMenu, showResponseButton, showMinimalFooter } = this.state; + const { id, showMenu, showResponseButton, showMinimalFooter , isSurveyOnLine, messageSurveyOffline, messageInfoSurveyOffline} = this.state; return ( <> @@ -105,7 +129,7 @@ class Main extends React.Component { )} /> - {showMenu && } + {showMenu && }
{/* } /> {!showMinimalFooter ? ( - + ) : ( - + )}
- {showResponseButton && } + {showResponseButton && + + }
diff --git a/src/components/template/footer-menu.js b/src/components/template/footer-menu.js index 8c26d5d..6004e02 100644 --- a/src/components/template/footer-menu.js +++ b/src/components/template/footer-menu.js @@ -4,9 +4,8 @@ import { Navbar, Nav, NavItem } from 'react-bootstrap'; import { LinkContainer } from 'react-router-bootstrap'; import logoTwitter from 'img/common/logo-twitter.png'; import { urlInseeFr } from 'utils/properties'; -import { isSurveyOnLine } from 'utils/api'; -function FooterMenu({ path, home, id }) { +function FooterMenu({ path, home, id, isSurveyOnLine }) { const [isDesktop, setIsDesktop] = useState(window.innerWidth > 767); const updateFooterMenu = () => { if (window.innerWidth <= 767) { @@ -44,7 +43,7 @@ function FooterMenu({ path, home, id }) { {`Questions/réponses`} - {isSurveyOnLine(id) && ( + {isSurveyOnLine && ( {`Contacter l'assistance`} diff --git a/src/components/template/menu.js b/src/components/template/menu.js index b0e2fe1..bdb8b5f 100644 --- a/src/components/template/menu.js +++ b/src/components/template/menu.js @@ -6,9 +6,9 @@ import { Link } from 'react-router-dom'; import { urlInseeFr } from 'utils/properties'; import logoTwitter from 'img/common/logo-twitter.png'; import { getResultsMenuTitle, getSurveyDetailLink } from 'utils/read-content'; -import { isSurveyOnLine } from 'utils/api'; +import ReactLoading from 'react-loading'; -function Menu({ id }) { +function Menu({ id, isSurveyOnLine }) { const [isDesktop, setIsDesktop] = useState(window.innerWidth > 767); const updateFooterMenu = () => { if (window.innerWidth <= 767) { @@ -35,16 +35,21 @@ function Menu({ id }) {