docbrown/app/javascript/onboarding/components/EmailPreferencesForm.jsx
Suzanne Aitchison 5bed8f56d4
improve keyboard accessibility of modals 12427 10610 (#12511)
* adds focusTrap wrapper to preact Modal component

* add view specific code for focus trap in admin add nav link

* add script to return a focustrap toggle, use in add nav link modal partial

* add trap to edit nav link modal

* add handlers for sign up modal

* update modal controller for admin section, update nav link modals to use

* update other admin modals with new data values for trap

* remove unneeded erb script file

* remove unneeded target

* refactor to remove extra unneeded param

* remove duplicate code, store getFocusTrapToggle in window

* trap focus in comment and bookmark showModal instances for not logged in user

* remove need for activator id

* clean up id refs no longer needed

* remove custom code and re-use focsu-trap lib

* update storybook docs

* update default export in focusTrap

* prevent close button click triggering a modal toggle twice

* ensure if user navigates from a modal the trap is deactivated

* add jsdoc comments and add dynamic import

* ensure admin controller modal traps are cleaned up on disconnect

* update sign up modal to use crayons

* update modal controller and admin nav links modals to use preact modal

* update profile fields modals for new controller

* tweak styling of sign up and admin modals to match previous

* update listings modal to use crayons modal, adapt focus trap to work with click outside

* memoize deactivate callback to ensure modal can be presented on first page load

* add missed focustrap changes

* fix focus trap issues in onboarding flow

* refactor onboarding focus trap, remove getFocusTrapToggle

* tweaks for styling and article modal toggle

* add click outside tests to modal

* add cypress tests for the login modal

* update liquid tag tests affected by change

* refactors to address review comments

* fix issue with login modal presented twice on comment add

* change ids to selectors in admin modals

* small pr comment refactors

* add listings e2e tests

* add nav link modal tests

* fix issue with help modal

* tweak to fix ui bug from merge

* remove context from showLoginModal

* rename toggleModal

* rename state property for clarity

Co-authored-by: Nick Taylor <nick@dev.to>
2021-02-24 16:01:10 +00:00

127 lines
3.8 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,
email_digest_periodic: false,
};
}
componentDidMount() {
updateOnboarding('v2: email preferences form');
}
onSubmit() {
const csrfToken = getContentOfToken('csrf-token');
fetch('/onboarding_checkbox_update', {
method: 'PATCH',
headers: {
'X-CSRF-Token': csrfToken,
'Content-Type': 'application/json',
},
body: JSON.stringify({ user: 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, email_digest_periodic } = this.state;
const { prev, slidesCount, currentSlideIndex } = this.props;
return (
<div
data-testid="onboarding-email-preferences-form"
className="onboarding-main crayons-modal"
>
<div
className="crayons-modal__box"
role="dialog"
aria-labelledby="title"
aria-describedby="subtitle"
>
<Navigation
prev={prev}
next={this.onSubmit}
slidesCount={slidesCount}
currentSlideIndex={currentSlideIndex}
/>
<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>
<li className="checkbox-item">
<label htmlFor="email_digest_periodic">
<input
type="checkbox"
id="email_digest_periodic"
name="email_digest_periodic"
checked={email_digest_periodic}
onChange={this.handleChange}
/>
I want to receive a periodic digest of top posts from my
tags.
</label>
</li>
</ul>
</fieldset>
</form>
</div>
</div>
</div>
);
}
}
EmailPreferencesForm.propTypes = {
prev: PropTypes.func.isRequired,
next: PropTypes.string.isRequired,
slidesCount: PropTypes.number.isRequired,
currentSlideIndex: PropTypes.func.isRequired,
};
/* eslint-enable camelcase */