docbrown/app/javascript/onboarding/components/EmailPreferencesForm.jsx
Rajat Talesra 54131cd94c
Move the digest email opt-in to the follow tags onboarding step (#20133)
* Removed intro slide and updated sequence

* Revert db change

* Initial save

* Design changes

* Mobile designs

* Added functionality

* Added tests

* Gradient effect added

* Added API call test
2023-09-20 17:55:29 +05:30

113 lines
3.2 KiB
JavaScript

import { h, Component } from 'preact';
import PropTypes from 'prop-types';
import { getContentOfToken, updateOnboarding } from '../utilities';
import { Navigation } from './Navigation';
/* eslint-disable camelcase */
export class EmailPreferencesForm extends Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.state = {
email_newsletter: false,
};
}
componentDidMount() {
updateOnboarding('v2: email preferences form');
}
onSubmit() {
const csrfToken = getContentOfToken('csrf-token');
fetch('/onboarding/notifications', {
method: 'PATCH',
headers: {
'X-CSRF-Token': csrfToken,
'Content-Type': 'application/json',
},
body: JSON.stringify({ notifications: this.state }),
credentials: 'same-origin',
}).then((response) => {
if (response.ok) {
localStorage.setItem('shouldRedirectToOnboarding', false);
const { next } = this.props;
next();
}
});
}
handleChange(event) {
const { name } = event.target;
this.setState((currentState) => ({
[name]: !currentState[name],
}));
}
render() {
const { email_newsletter } = this.state;
const { prev, slidesCount, currentSlideIndex } = this.props;
return (
<div
data-testid="onboarding-email-preferences-form"
className="onboarding-main crayons-modal crayons-modal--large"
>
<div
className="crayons-modal__box"
role="dialog"
aria-labelledby="title"
aria-describedby="subtitle"
>
<div className="onboarding-content terms-and-conditions-wrapper">
<header className="onboarding-content-header">
<h1 id="title" className="title">
Almost there!
</h1>
<h2 id="subtitle" className="subtitle">
Review your email preferences before we continue.
</h2>
</header>
<form>
<fieldset>
<legend>Email preferences</legend>
<ul>
<li className="checkbox-item">
<label htmlFor="email_newsletter">
<input
type="checkbox"
id="email_newsletter"
name="email_newsletter"
checked={email_newsletter}
onChange={this.handleChange}
/>
I want to receive weekly newsletter emails.
</label>
</li>
</ul>
</fieldset>
</form>
</div>
<Navigation
prev={prev}
next={this.onSubmit}
slidesCount={slidesCount}
currentSlideIndex={currentSlideIndex}
/>
</div>
</div>
);
}
}
EmailPreferencesForm.propTypes = {
prev: PropTypes.func.isRequired,
next: PropTypes.string.isRequired,
slidesCount: PropTypes.number.isRequired,
currentSlideIndex: PropTypes.func.isRequired,
};
/* eslint-enable camelcase */