import { h, Component } from 'preact';
import PropTypes from 'prop-types';
import { userData, getContentOfToken, updateOnboarding } from '../utilities';
import Navigation from './Navigation';
import ColorPicker from './ProfileForm/ColorPicker';
import TextArea from './ProfileForm/TextArea';
import TextInput from './ProfileForm/TextInput';
import CheckBox from './ProfileForm/CheckBox';
import { request } from '@utilities/http';
/* eslint-disable camelcase */
class ProfileForm extends Component {
constructor(props) {
super(props);
this.handleFieldChange = this.handleFieldChange.bind(this);
this.handleColorPickerChange = this.handleColorPickerChange.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.user = userData();
this.state = {
groups: [],
formValues: {},
canSkip: true,
last_onboarding_page: 'v2: personal info form',
};
}
componentDidMount() {
this.getProfielFieldGroups();
updateOnboarding('v2: personal info form');
}
async getProfielFieldGroups() {
try {
const response = await request(`/profile_field_groups?onboarding=true`);
if (response.ok) {
const data = await response.json();
this.setState({ groups: data.profile_field_groups });
} else {
throw new Error(response.statusText);
}
} catch (error) {
this.setState({ error: true, errorMessage: error.toString() });
}
}
onSubmit() {
const csrfToken = getContentOfToken('csrf-token');
const { formValues, last_onboarding_page } = this.state;
fetch('/onboarding_update', {
method: 'PATCH',
headers: {
'X-CSRF-Token': csrfToken,
'Content-Type': 'application/json',
},
body: JSON.stringify({
user: { last_onboarding_page },
profile: { ...formValues },
}),
credentials: 'same-origin',
}).then((response) => {
if (response.ok) {
const { next } = this.props;
next();
}
});
}
handleFieldChange(e) {
const { formValues } = { ...this.state };
const currentFormState = formValues;
const { name, value } = e.target;
currentFormState[name] = value;
this.setState({
formValues: currentFormState,
canSkip: this.formIsEmpty(currentFormState),
});
}
handleColorPickerChange(e) {
const { formValues } = { ...this.state };
const currentFormState = formValues;
const field = e.target;
const { name, value } = field;
let sibling = field.nextElementSibling
? field.nextElementSibling
: field.previousElementSibling;
sibling.value = value;
currentFormState[name] = value;
this.setState({
formValues: currentFormState,
canSkip: this.formIsEmpty(currentFormState),
});
}
formIsEmpty(currentFormState) {
// Once we've derived the new form values, check if the form is empty
// and use that value to set the `canSkip` property on the state.
Object.values(currentFormState).filter((v) => v.length > 0).length === 0;
}
renderAppropriateFieldType(field) {
switch (field.input_type) {
case 'check_box':
return (
{username}