Skip to content

Commit

Permalink
[Feat] LoginForm layout
Browse files Browse the repository at this point in the history
Created basic login form layout.
References #21.
  • Loading branch information
angel-penchev committed Oct 23, 2021
1 parent 97e8b6b commit ab0c86b
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 0 deletions.
13 changes: 13 additions & 0 deletions client/src/components/LoginForm/LoginForm.lazy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React, { lazy, Suspense } from 'react';

const LazyLoginForm = lazy(() => import('./LoginForm'));

const LoginForm = (
props: JSX.IntrinsicAttributes & { children?: React.ReactNode },
) => (
<Suspense fallback={null}>
<LazyLoginForm {...props} />
</Suspense>
);

export default LoginForm;
5 changes: 5 additions & 0 deletions client/src/components/LoginForm/LoginForm.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.LoginForm {
a {
cursor: pointer;
}
}
13 changes: 13 additions & 0 deletions client/src/components/LoginForm/LoginForm.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable */
import LoginForm from './LoginForm';

export default {
title: "LoginForm",
component: LoginForm
};

export const Default = () => <LoginForm apiLoginEndpoint=''/>;

Default.story = {
name: 'default',
};
14 changes: 14 additions & 0 deletions client/src/components/LoginForm/LoginForm.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import LoginForm from './LoginForm';

describe('<LoginForm />', () => {
test('it should mount', () => {
render(<LoginForm />);

const loginForm = screen.getByTestId('LoginForm');

expect(loginForm).toBeInTheDocument();
});
});
95 changes: 95 additions & 0 deletions client/src/components/LoginForm/LoginForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {
Button,
Container,
Grid,
Link,
TextField,
Typography,
} from '@mui/material';
import axios from 'axios';
import React, { FC, useState } from 'react';

import styles from './LoginForm.module.scss';

interface LoginFormProps {
apiLoginEndpoint: string;
}

const LoginForm: FC<LoginFormProps> = (props: LoginFormProps) => {
const [username, setUsername] = useState<string>('');
const [password, setPassword] = useState<string>('');

const login = async () => {
const response = await axios.post(
props.apiLoginEndpoint,
{
username,
password,
},
{
withCredentials: true,
},
);

if (response.data === 'success') {
return (window.location.href = '/');
}
};

const register = () => {
console.log('aaa');
};

return (
<Container className={styles.LoginForm} data-testid="LoginForm">
<Grid container spacing={2}>
<Grid item xs={12}>
<Typography variant="h4">Login</Typography>
</Grid>
<Grid item xs={12}>
<TextField
label="Username"
variant="filled"
type="text"
required
fullWidth
onChange={(e: {
target: { value: React.SetStateAction<string> };
}) => setUsername(e.target.value)}
/>
</Grid>

<Grid item xs={12}>
<TextField
label="Password"
variant="filled"
type="password"
required
fullWidth
onChange={(e: {
target: { value: React.SetStateAction<string> };
}) => setPassword(e.target.value)}
/>
</Grid>

<Grid item xs={12}>
<Button
type="button"
variant="contained"
size="large"
fullWidth
onClick={() => login()}
>
Log in
</Button>
<Typography align="center">
Don't have an account?{' '}
<Link onClick={() => register()}>Register!</Link>
</Typography>
</Grid>
</Grid>
</Container>
);
};

export default LoginForm;
4 changes: 4 additions & 0 deletions client/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}

a {
cursor: pointer;
}

0 comments on commit ab0c86b

Please sign in to comment.