forked from sombochea/verdaccio-ui
feat: login Dialog Component - Replaced class by func. comp + added react-hook-form (#341)
* refactor: convert class to func * refactor: changed login form logic * refactor: conver to testing-library tests * refactor: moved dependency * refactor: replaced uglifyjs-webpack-plugin by terser-webpack-plugin * fix: fixed e2e errors * fix: fixed e2e test * Delete settings.json * fix: vscode settings rollback * refactor: rollback webpack config * refactor: updated eslint rule * fix: removed --fix * refactor: incresed the bundle size
This commit is contained in:
committed by
Juan Picado @jotadeveloper
parent
501845b5f8
commit
42d3bb8508
56
src/components/LoginDialog/LoginDialog.tsx
Normal file
56
src/components/LoginDialog/LoginDialog.tsx
Normal file
@@ -0,0 +1,56 @@
|
||||
import React, { useState, useContext, useCallback } from 'react';
|
||||
|
||||
import { makeLogin } from '../../utils/login';
|
||||
import storage from '../../utils/storage';
|
||||
import Dialog from '../../muiComponents/Dialog';
|
||||
import DialogContent from '../../muiComponents/DialogContent';
|
||||
import AppContext from '../../App/AppContext';
|
||||
|
||||
import LoginDialogCloseButton from './LoginDialogCloseButton';
|
||||
import LoginDialogForm, { FormValues } from './LoginDialogForm';
|
||||
import LoginDialogHeader from './LoginDialogHeader';
|
||||
|
||||
interface Props {
|
||||
open?: boolean;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
const LoginDialog: React.FC<Props> = ({ onClose, open = false }) => {
|
||||
const appContext = useContext(AppContext);
|
||||
|
||||
if (!appContext) {
|
||||
throw Error('The app Context was not correct used');
|
||||
}
|
||||
|
||||
const [error, setError] = useState();
|
||||
|
||||
const handleDoLogin = useCallback(
|
||||
async (data: FormValues) => {
|
||||
const { username, token, error } = await makeLogin(data.username, data.password);
|
||||
|
||||
if (error) {
|
||||
setError(error);
|
||||
}
|
||||
|
||||
if (username && token) {
|
||||
storage.setItem('username', username);
|
||||
storage.setItem('token', token);
|
||||
appContext.setUser({ username });
|
||||
onClose();
|
||||
}
|
||||
},
|
||||
[appContext, onClose]
|
||||
);
|
||||
|
||||
return (
|
||||
<Dialog fullWidth={true} id="login--dialog" maxWidth="sm" onClose={onClose} open={open}>
|
||||
<LoginDialogCloseButton onClose={onClose} />
|
||||
<DialogContent>
|
||||
<LoginDialogHeader />
|
||||
<LoginDialogForm error={error} onSubmit={handleDoLogin} />
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default LoginDialog;
|
||||
Reference in New Issue
Block a user