diff --git a/client/src/components/AccountForm/index.js b/client/src/components/AccountForm/index.js index 79a0ffc..55bb3a6 100644 --- a/client/src/components/AccountForm/index.js +++ b/client/src/components/AccountForm/index.js @@ -1,6 +1,6 @@ import { h, Component, render } from 'preact'; import style from './style.css'; -import { handleSubmit, clearForms, setStateUserOrRedirectToSignIn } from "../../js/utilities"; +import { validateAccountForm, clearForms } from "../../js/validate-account-form"; import { LOGIN_PATH, REGISTER_PATH, RESET_PATH } from '../../../config'; import linkState from "linkstate"; import { route } from 'preact-router'; @@ -8,102 +8,101 @@ import { route } from 'preact-router'; export default class AccountForm extends Component { constructor() { super(); - this.state = { - form_message: "", - successMessageMap: this.createMessageMap(), - matchPasswordsMap: this.createMatchPasswordsMap(), - validatePasswordMap: this.createValidatePasswordMap(), - }; - this.handleSubmit = handleSubmit.bind(this); - this.routeToRegister = this.routeToRegister.bind(this); - } - createMessageMap = () => { - const messageMap = new Map; - messageMap.set(LOGIN_PATH, 'You have signed in.'); - messageMap.set(REGISTER_PATH, 'You have created an account.'); - messageMap.set(RESET_PATH, 'You have changed your password.'); - return messageMap; + this.routeToRegister = this.routeToRegister.bind(this); + this.handleSubmit = this.handleSubmit.bind(this); } - createMatchPasswordsMap = () => { - const matchPasswordsMap = new Map; - matchPasswordsMap.set(REGISTER_PATH, ['password','confirm_password']); - matchPasswordsMap.set(RESET_PATH, ['new_password','confirm_password']); - return matchPasswordsMap; - } + /** + * TODO - refactor: simplify + * + * Pass: event, path + * Return: a promise - request result that can be used to here to set 'successMessage' + * via setState + */ + handleSubmit = (event) => { + event.preventDefault(); - createValidatePasswordMap = () => { - const validatePasswordMap = new Map; - validatePasswordMap.set(LOGIN_PATH, 'password'); - validatePasswordMap.set(REGISTER_PATH, 'password'); - validatePasswordMap.set(RESET_PATH, 'new_password'); - return validatePasswordMap; - } + const formData = { + name: this.state.name, + email: this.state.email, + password: this.state.password, + new_password: this.state.new_password, + confirm_password: this.state.confirm_password, + } - doSubmit = (event) => { const args = { - event: event, path: this.props.path, - message_key: 'form_message', - component: this, - matchPasswordFields: this.state.matchPasswordsMap.get(this.props.path), - passwordToValidate: this.state.validatePasswordMap.get(this.props.path), - successMessage: this.getSuccessMessage(), - }; - this.handleSubmit(args); - } + formData, + } - getSuccessMessage = () => { - return this.state.successMessageMap.get(this.props.path); - } + validateAccountForm(args).then((response) => { + console.log('validateAccountForm(): ', response); + if (response.status) { + if (this.props.path === RESET_PATH) { + // TODO - alert with success + console.log('path: ', this.props.path); + alert(response.message); + } else { // redirect to /profile with success for login/registration + console.log('route to profile'); + route(`/profile`, true); + } + } else { + // TODO - alert failure to process + alert(response.message); + } + }).catch(function (error) { + alert(error); + }); + }; componentWillUnmount = () => { clearForms(); } componentDidMount = () => { - if (this.props.path === RESET_PATH) { - setStateUserOrRedirectToSignIn(this); - } + // TODO - remove + console.log('AccountForm.componentDidMount()'); } routeToRegister() { route("/register", true); } - render({path},{ form_message, user, name, email, password, new_password, confirm_password }) { + render({ path },{ name, email, password, new_password, confirm_password }) { //DEFAULT TO LOGIN_PATH let display =