Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add default login #4

Merged
merged 4 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
{ allowConstantExport: true },
],
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'import/order': [
'error',
{
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"scripts": {
"dev": "yarn && vite",
"build": "tsc && vite build",
"fix:eslint": "eslint src --fix --ext ts,tsx --report-unused-disable-directives",
"fix:prettier": "prettier --write --list-different \"src/**/*.{ts,tsx}\"",
"fix": "npm-run-all -p --print-label \"fix:**\"",
"lint:eslint": "eslint src --ext ts,tsx --report-unused-disable-directives",
"lint:prettier": "prettier --list-different \"src/**/*.{ts,tsx}\"",
"lint": "npm-run-all -p --print-label \"lint:**\"",
Expand Down
6 changes: 3 additions & 3 deletions src/features/app/Container.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { FunctionComponent } from 'react';

import Layout from '../shared/components/Layout';
import LayoutContainer from '../shared/layout/Container';
import HomeContainer from '../home/Container';

const queryClient = new QueryClient();

const AppContainer: FunctionComponent = () => {
return (
<QueryClientProvider client={queryClient}>
<Layout>
<LayoutContainer>
<HomeContainer />
</Layout>
</LayoutContainer>
</QueryClientProvider>
);
};
Expand Down
36 changes: 36 additions & 0 deletions src/features/login/Container.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { FunctionComponent } from 'react';

import useLogin from './hooks/useLogin';

const LoginContainer: FunctionComponent = () => {
const {
email,
handleEmailChange,
password,
handlePasswordChange,
error,
handleSubmitClick,
} = useLogin();

return (
<div>
<div>Login</div>
<input
type="text"
value={email}
placeholder="Insert your email"
onChange={handleEmailChange}
/>
<input
type="password"
value={password}
placeholder="Insert your password"
onChange={handlePasswordChange}
/>
{error && <span>Please check your email or password</span>}
<button onClick={handleSubmitClick}>submit</button>
</div>
);
};

export default LoginContainer;
5 changes: 5 additions & 0 deletions src/features/login/atom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { atomWithReset } from 'jotai/utils';

import { User } from './type';

export const userAtom = atomWithReset<User>({});
8 changes: 8 additions & 0 deletions src/features/login/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DummyUser } from './type';

export const DUMMY_USER: DummyUser = {
email: '[email protected]',
companyNo: 'JS1234',
password: 'just',
department: 'dev1',
};
45 changes: 45 additions & 0 deletions src/features/login/hooks/useLogin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ChangeEventHandler, ReactEventHandler, useState } from 'react';
import { useAtom } from 'jotai';

import { setItem } from '../../shared/utils/storage';
import { DummyUser } from '../type';
import { DUMMY_USER } from '../constants';
import { userAtom } from '../atom';

const useLogin = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState(false);
const [, setUser] = useAtom(userAtom);

const handleEmailChange: ChangeEventHandler<HTMLInputElement> = (e) => {
setEmail(e.target.value);
setError(false);
};
const handlePasswordChange: ChangeEventHandler<HTMLInputElement> = (e) => {
setPassword(e.target.value);
setError(false);
};
const handleSubmitClick: ReactEventHandler<HTMLButtonElement> = () => {
if (DUMMY_USER.email !== email || DUMMY_USER.password !== password) {
setError(true);
return;
}
const curUser: DummyUser = { ...DUMMY_USER };
delete curUser.password;
setUser({ ...curUser });
setItem('user', curUser);
// TODO: Redirect Home;
};

return {
email,
handleEmailChange,
password,
handlePasswordChange,
error,
handleSubmitClick,
};
};

export default useLogin;
6 changes: 6 additions & 0 deletions src/features/login/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type User = {
email?: string;
companyNo?: string;
department?: string;
};
export type DummyUser = User & { password?: string };
8 changes: 0 additions & 8 deletions src/features/shared/components/Layout.tsx

This file was deleted.

13 changes: 13 additions & 0 deletions src/features/shared/layout/Container.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { FunctionComponent, PropsWithChildren } from 'react';

import './layout.scss';
import useLayout from './hooks/useLayout';

const LayoutContainer: FunctionComponent<PropsWithChildren> = ({
children,
}) => {
useLayout();
return <div className="layout-container">{children}</div>;
};

export default LayoutContainer;
18 changes: 18 additions & 0 deletions src/features/shared/layout/hooks/useLayout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useAtom } from 'jotai';
import { useLayoutEffect } from 'react';

import { userAtom } from '../../../login/atom';
import { getItem } from '../../utils/storage';

const useLayout = () => {
const [user] = useAtom(userAtom);

useLayoutEffect(() => {
const user = getItem('user');
if (!user) {
// TODO: Redirect Login
}
}, []);
};

export default useLayout;
4 changes: 4 additions & 0 deletions src/features/shared/layout/layout.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.layout-container {
min-width: 320px;
max-width: 480px;
}
5 changes: 0 additions & 5 deletions src/features/shared/style/global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,3 @@ body {
place-content: center;
min-height: 100vh;
}

.layout-container {
min-width: 320px;
max-width: 480px;
}