import { h, Component } from 'preact';
import PropTypes from 'prop-types';
import { userData, updateOnboarding } from '../utilities';
import { ProfileImage } from './ProfileForm/ProfileImage';
import { Navigation } from './Navigation';
import { TextArea } from './ProfileForm/TextArea';
import { TextInput } from './ProfileForm/TextInput';
import { CheckBox } from './ProfileForm/CheckBox';
import { request } from '@utilities/http';
/* eslint-disable camelcase */
export 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: {
username: this.user.username,
profile_image_90: this.user.profile_image_90,
},
canSkip: false,
last_onboarding_page: 'v2: personal info form',
profile_image_90: this.user.profile_image_90,
};
}
componentDidMount() {
this.getProfileFieldGroups();
updateOnboarding('v2: personal info form');
}
async getProfileFieldGroups() {
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() });
}
}
async onSubmit() {
const { formValues, last_onboarding_page } = this.state;
const { username, profile_image_90, ...newFormValues } = formValues;
try {
const response = await request('/onboarding', {
method: 'PATCH',
body: {
user: { last_onboarding_page, profile_image_90, username },
profile: { ...newFormValues },
},
});
if (!response.ok) {
throw response;
}
const { next } = this.props;
next();
} catch (error) {
Honeybadger.notify(error);
let errorMessage = 'Unable to continue, please try again.';
if (error.status === 422) {
// parse validation error messages from UsersController#onboarding
const errorData = await error.json();
errorMessage = errorData.errors;
this.setState({ error: true, errorMessage });
} else {
this.setState({ error: true, errorMessage });
}
}
}
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;
const 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 (
Remaining characters: {SUMMARY_MAXLENGTH - summaryCharacters} {summaryCharacters}/{SUMMARY_MAXLENGTH}