import React, { useState } from 'react'; import { useDispatch } from 'react-redux'; /* !- Redux Actions */ import { setUser } from './actions'; /* !- React Elements */ import Form from '../form/intl/form'; import Input from '../form/intl/input'; import Submit from '../form/intl/submit'; /* !- Constants */ import { LOGIN_SCHEME, LOGIN_FIELDS } from './constants'; /* !- Types */ type PropTypes = { fields: { email: {}, password: {}, }, scheme: { email: {}, password: {}, }, onSuccess?: (response) => boolean, onError?: (response) => boolean, } /** * Login: classic login form component: email, password input + submit. * Use Contex Api (see: submit component) * + if Api reponse success, then set user via Redux. * * @example * // api * * const API = ({ method }) => * { * if (method === 'login') * { * return request * .post(`${API_URL}user/login`) * .type('form') * .send(payload) * .then(response => response.body) * .catch(parser); * } * } * * */ const Login = ({ scheme = LOGIN_SCHEME, fields = LOGIN_FIELDS, onSuccess, onError, }: PropTypes) => { const dispatch = useDispatch(); return (
{ if (typeof onSuccess === 'function') { if (onSuccess(response) === true) { return; } } dispatch(setUser(response.records)); }} onFailed={(response) => { if (typeof onError === 'function') { onError(response); } }} > ); } export default Login;