Onboarding JS: refactor function out of repetitive blocks (#5564) [deploy]

This commit is contained in:
Frederico Hasegawa 2020-02-06 18:00:57 -03:00 committed by GitHub
parent 4872422373
commit 7436e70158
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 46 deletions

View file

@ -2,7 +2,7 @@ import { h, Component } from 'preact';
import PropTypes from 'prop-types';
import Navigation from './Navigation';
import { getContentOfToken } from '../utilities';
import { getContentOfToken, updateOnboarding } from '../utilities';
class BioForm extends Component {
constructor(props) {
@ -17,16 +17,7 @@ class BioForm extends Component {
}
componentDidMount() {
const csrfToken = getContentOfToken('csrf-token');
fetch('/onboarding_update', {
method: 'PATCH',
headers: {
'X-CSRF-Token': csrfToken,
'Content-Type': 'application/json',
},
body: JSON.stringify({ user: { last_onboarding_page: 'bio form' } }),
credentials: 'same-origin',
});
updateOnboarding('bio form');
}
onSubmit() {

View file

@ -1,20 +1,11 @@
import { h, Component } from 'preact';
import PropTypes from 'prop-types';
import { getContentOfToken } from '../utilities';
import { updateOnboarding } from '../utilities';
class ClosingSlide extends Component {
componentDidMount() {
const csrfToken = getContentOfToken('csrf-token');
fetch('/onboarding_update', {
method: 'PATCH',
headers: {
'X-CSRF-Token': csrfToken,
'Content-Type': 'application/json',
},
body: JSON.stringify({ user: { last_onboarding_page: 'closing slide' } }),
credentials: 'same-origin',
});
updateOnboarding('closing slide');
}
render() {

View file

@ -4,7 +4,7 @@ import { h, Component } from 'preact';
import PropTypes from 'prop-types';
import Navigation from './Navigation';
import { getContentOfToken } from '../utilities';
import { getContentOfToken, updateOnboarding } from '../utilities';
/* eslint-disable camelcase */
@ -27,18 +27,7 @@ class EmailTermsConditionsForm extends Component {
}
componentDidMount() {
const csrfToken = getContentOfToken('csrf-token');
fetch('/onboarding_update', {
method: 'PATCH',
headers: {
'X-CSRF-Token': csrfToken,
'Content-Type': 'application/json',
},
body: JSON.stringify({
user: { last_onboarding_page: 'emails, COC and T&C form' },
}),
credentials: 'same-origin',
});
updateOnboarding('emails, COC and T&C form');
}
onSubmit() {

View file

@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import Navigation from './Navigation';
import SlideContent from './SlideContent';
import { getContentOfToken } from '../utilities';
import { updateOnboarding } from '../utilities';
class IntroSlide extends Component {
constructor(props) {
@ -13,16 +13,7 @@ class IntroSlide extends Component {
}
componentDidMount() {
const csrfToken = getContentOfToken('csrf-token');
fetch('/onboarding_update', {
method: 'PATCH',
headers: {
'X-CSRF-Token': csrfToken,
'Content-Type': 'application/json',
},
body: JSON.stringify({ user: { last_onboarding_page: 'intro slide' } }),
credentials: 'same-origin',
});
updateOnboarding('intro slide');
}
onSubmit() {

View file

@ -6,3 +6,16 @@ export const jsonToForm = data => {
export const getContentOfToken = token =>
document.querySelector(`meta[name='${token}']`).content;
export const updateOnboarding = lastPage => {
const csrfToken = getContentOfToken('csrf-token');
fetch('/onboarding_update', {
method: 'PATCH',
headers: {
'X-CSRF-Token': csrfToken,
'Content-Type': 'application/json',
},
body: JSON.stringify({ user: { last_onboarding_page: lastPage } }),
credentials: 'same-origin',
});
};