docbrown/app/javascript/onboarding/components/EmailPreferencesForm.jsx
Joshua Wehner 6a48b486ca
Onboarding: custom newsletter opt-in settings (#20114)
* Settings can process markdown into html

* Update Settings with new onboarding settings

* Async render onboarding newsletter step

* Tweak onboarding design

* Fix broken spec

* Better rendered component test

* Tweaks to match design

* Try to tweak design

* Try having a default state

* Tweak placeholder content

* Better await componentDidMount

* Continue to tweak the design

* ContentRenderer#process should always return Result

* Try more clarity in the partial

* Rubocop
2023-09-26 06:34:19 -04:00

85 lines
2.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.onSubmit = this.onSubmit.bind(this);
this.state = {
content: '<p>Loading...</p>'
};
}
componentDidMount() {
fetch('/onboarding/newsletter')
.then((response) => response.json())
.then((json) => {
this.setState({ content: json['content'] });
});
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();
}
});
}
render() {
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 email-preferences-wrapper"
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{ __html: this.state.content }}
/>
<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 */