-
+
>
)
diff --git a/src/pages/Home/style.module.css b/src/pages/Home/style.module.css
index f1e9a90..6462990 100644
--- a/src/pages/Home/style.module.css
+++ b/src/pages/Home/style.module.css
@@ -119,3 +119,10 @@
}
}
+.logoutBtn {
+ width: 100%;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ margin-top: 40px;
+}
\ No newline at end of file
diff --git a/src/pages/Login/action.ts b/src/pages/Login/action.ts
new file mode 100644
index 0000000..56e9e9d
--- /dev/null
+++ b/src/pages/Login/action.ts
@@ -0,0 +1,85 @@
+import { ActionType } from './reducer'
+import { LoginApi } from '@/models/ApiType/Login/type'
+import { UserCookieFmt0001VO } from '@/models/entity/client/fmt/UserCookieFmt0001VO'
+import { baseURL } from '@/utils/baseURL'
+import type { NavigateFunction } from 'react-router-dom'
+
+export namespace Action {
+ export async function editForm(
+ dispatch: React.Dispatch,
+ targetName: string,
+ value: any,
+ ) {
+ dispatch({
+ type: 'EDIT_FORM',
+ payload: {
+ targetName,
+ value,
+ },
+ })
+ }
+
+ export async function logIn(
+ dispatch: React.Dispatch,
+ email: string,
+ password: string,
+ navigate: NavigateFunction,
+ ) {
+ dispatch({ type: 'LOGIN_REQUEST' })
+
+ if (!email || !password) {
+ throw new Error('未入力項目があります')
+ }
+
+ try {
+ const json: LoginApi.POST.Request = {
+ email,
+ password,
+ }
+
+ const res = await fetch(`${baseURL}auth/login`, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(json),
+ })
+
+ const result: LoginApi.POST.Response = await res.json()
+
+ dispatch({
+ type: 'LOGIN_SUCCESS',
+ payload: {
+ token: result.token,
+ id: result.id,
+ email: result.email,
+ name: result.name,
+ role: result.role,
+ class_name: result.class_name,
+ },
+ })
+
+ const maxAge = 60 * 60 * 24 * 7
+
+ document.cookie = `W2CToken=${result.token}; path=/; max-age=${maxAge}`
+
+ const userData: UserCookieFmt0001VO.Type = {
+ id: result.id,
+ name: result.name,
+ email: result.email,
+ role: result.role,
+ class_name: result.class_name,
+ }
+ document.cookie = `user=${encodeURIComponent(JSON.stringify(userData))}; path=/; max-age=${maxAge}`
+
+ navigate('/')
+ } catch (e) {
+ dispatch({ type: 'LOGIN_FAILURE' })
+ throw e
+ }
+ }
+
+ export async function ShowPass(dispatch: React.Dispatch) {
+ dispatch({ type: 'SHOW_PASS' })
+ }
+}
diff --git a/src/pages/Login/page.tsx b/src/pages/Login/index.tsx
similarity index 55%
rename from src/pages/Login/page.tsx
rename to src/pages/Login/index.tsx
index e0185ae..4df7954 100644
--- a/src/pages/Login/page.tsx
+++ b/src/pages/Login/index.tsx
@@ -3,15 +3,20 @@ import logo from '../../assets/w2cLogo.svg'
import eyeIcon from '../../assets/eye.svg'
import eyeOffIcon from '../../assets/eyeOff.svg'
import { Button } from '@/stories/Button'
-import { useState } from 'react'
-import { Link } from 'react-router-dom'
+import { useEffect, useReducer } from 'react'
+import { Link, useLocation, useNavigate } from 'react-router-dom'
+import { defaultState, reducer } from './reducer'
+import { Action } from './action'
export default function Login() {
- const [showPassword, setShowPassword] = useState(false)
+ const [state, dispatch] = useReducer(reducer, undefined, defaultState)
+ const navigate = useNavigate()
+ const location = useLocation()
- const togglePassword = () => {
- setShowPassword((prev) => !prev)
- }
+ useEffect(() => {
+ const { email } = location.state || {}
+ Action.editForm(dispatch, 'login.email', email)
+ }, [])
return (
<>
@@ -35,6 +40,13 @@ export default function Login() {
placeholder="学籍番号@ecc.ac.jp"
autoComplete="email"
required
+ onChange={(e) => {
+ Action.editForm(
+ dispatch,
+ 'login.email',
+ e.target.value,
+ )
+ }}
/>
平田晃大
+{userName}
+
+
@@ -45,26 +57,49 @@ export default function Login() {
{
+ Action.editForm(
+ dispatch,
+ 'login.password',
+ e.target.value,
+ )
+ }}
/>
+ Action.ShowPass(dispatch)
+ }
className={styles.eyeIcon}
/>
-
+
@@ -45,19 +53,32 @@ export default function Signup() {
{
+ Action.editForm(
+ dispatch,
+ 'signup.password',
+ e.target.value,
+ )
+ }}
/>
+ Action.ShowPass(dispatch)
+ }
className={styles.eyeIcon}
/>
@@ -72,19 +93,32 @@ export default function Signup() {
{
+ Action.editForm(
+ dispatch,
+ 'signup.conPassword',
+ e.target.value,
+ )
+ }}
/>
+ Action.ShowPass(dispatch)
+ }
className={styles.eyeIcon}
/>
@@ -98,6 +132,13 @@ export default function Signup() {
placeholder="例)ウェブ 二郎"
autoComplete="email"
required
+ onChange={(e) => {
+ Action.editForm(
+ dispatch,
+ 'signup.name',
+ e.target.value,
+ )
+ }}
/>
@@ -108,11 +149,30 @@ export default function Signup() {
id="text"
placeholder="例)WD1A"
required
+ onChange={(e) => {
+ Action.editForm(
+ dispatch,
+ 'signup.class_name',
+ e.target.value,
+ )
+ }}
/>
-
+