- added login and loginAdmin;

- fixed bug with saving application data;
This commit is contained in:
Vitalii Kiiko
2024-09-25 16:25:09 +02:00
parent b008fcd37a
commit 25b1b5ae7d
9 changed files with 226 additions and 112 deletions

View File

@@ -1,66 +1,18 @@
import React, { useRef, useState, useEffect } from 'react';
import { __, sprintf } from '@wordpress/i18n';
import { useForm } from 'react-hook-form';
import React, { useRef, useEffect } from 'react';
import { __ } from '@wordpress/i18n';
import { classNames } from 'primereact/utils';
import { isEmpty } from 'ramda';
// tools
import AuthenticationService from '../../service/authentication-service';
// store
import { storeSet, useStore } from '../../store';
// components
import FormField from '../../components/FormField';
import LogoIcon from '../../icons/LogoIcon';
import { Button } from 'primereact/button';
import { Messages } from 'primereact/messages';
const Login = () => {
const token = useStore().main.token();
const [loading, setLoading] = useState(false);
const errorMsgs = useRef(null);
const {
control,
handleSubmit,
formState: { errors },
} = useForm({ mode: 'onChange' });
const onSubmit = (formData) => {
errorMsgs.current.clear();
setLoading(true);
const request = {
...formData,
rememberMe: true
}
AuthenticationService.login(request, loginCallback, loginError);
};
const loginCallback = (data) => {
if (data.status === 'SUCCESS') {
storeSet.main.setAuthData({
token: data.data.token,
userData: data.data.user
});
} else {
errorMsgs.current.show([
{ sticky: true, severity: 'error', summary: '',
detail: data.message,
closable: true }
]);
}
setLoading(false);
}
const loginError = (err) => {
errorMsgs.current.show([
{ sticky: true, severity: 'error', summary: '',
detail: sprintf(__('%s', 'gepafin'), err),
closable: true }
]);
setLoading(false);
}
const loginWithSpid = () => {
console.log('spid')
@@ -68,7 +20,6 @@ const Login = () => {
useEffect(() => {
if (!isEmpty(token)) {
setLoading(true);
window.location.replace('/')
}
}, [token]);
@@ -106,39 +57,6 @@ const Login = () => {
<span>{__('Entra con SPID', 'gepafin')}</span>
</button>
<div className="appPage__spacer"></div>
<div className="appPageSection__hr">
<span>{__('Oppure', 'gepafin')}</span>
</div>
<div className="appPage__spacer"></div>
<form className="appForm" onSubmit={handleSubmit(onSubmit)}>
<FormField
type="textinput"
fieldName="email"
label={__('Email', 'gepafin')}
control={control}
errors={errors}
config={{ required: __('È obbligatorio', 'gepafin') }}
placeholder="sample@example.com"
/>
<FormField
type="textinput"
inputtype="password"
fieldName="password"
label={__('Password', 'gepafin')}
control={control}
errors={errors}
config={{ required: __('È obbligatorio', 'gepafin') }}
/>
<Button
label={__('Accedi', 'gepafin')}
disabled={loading}/>
</form>
</div>
</div>
)