/* global gettext */ import React from 'react'; import Cookies from 'js-cookie'; import { DemographicsCollectionModal } from './DemographicsCollectionModal'; export class DemographicsCollectionBanner extends React.Component { constructor(props) { super(props); this.state = { modalOpen: false, hideBanner: false } this.dismissBanner = this.dismissBanner.bind(this); } /** * Utility function that controls hiding the CTA from the Course Dashboard where appropriate. * This can be called one of two ways - when a user clicks the "dismiss" button on the CTA * itself, or when the learner completes all of the questions within the modal. * * The dismiss button itself is nested inside of an , so we need to call stopPropagation() * here to prevent the Modal from _also_ opening when the Dismiss button is clicked. */ async dismissBanner(e) { // Since this function also doubles as a callback in the Modal, we check if e is null/undefined // before calling stopPropagation() if (e) { e.stopPropagation(); } const requestOptions = { method: 'PATCH', credentials: 'include', headers: { 'Content-Type': 'application/json', 'X-CSRFTOKEN': Cookies.get('csrftoken'), }, body: JSON.stringify({ show_call_to_action: false, }) }; await fetch(`${this.props.lmsRootUrl}/api/demographics/v1/demographics/status/`, requestOptions); // No matter what the response is from the API call we always allow the learner to dismiss the // banner when clicking the dismiss button this.setState({ hideBanner: true }); } render() { if (!(this.state.hideBanner)) { return (
this.setState({ modalOpen: true })}>
{gettext('Want to make edX better for everyone?')}
{this.state.modalOpen && this.setState({ modalOpen: false })} dismissBanner={this.dismissBanner} /> }
) } else { return null; } } }