Skip to content

Commit 6a0c9af

Browse files
Merge pull request #43 from ut-code/user_data
user管理クラス
2 parents 19bf5da + 57d0c88 commit 6a0c9af

File tree

5 files changed

+132
-35
lines changed

5 files changed

+132
-35
lines changed

readme/developer_readme.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,51 @@
1-
## setup
1+
## セットアップ
22

3-
After cloning the repository, you need to install the required packages.
4-
Please run the following command in the root directory of the repository
3+
リポジトリをクローンした後、必要なパッケージをインストールする必要があります。
4+
リポジトリのルートディレクトリで以下のコマンドを実行してください。
55

66
```bash
77
npm install
88
```
99

10-
## development
10+
## 開発
1111

12-
To run the development mode, run the following command.
12+
開発モードを実行するには、以下のコマンドを実行してください。
1313

1414
```bash
1515
npm run dev
1616
```
1717

18-
Before pushing the code, please run the following command to check the code quality.
18+
コードをプッシュする前に、コード品質をチェックするために以下のコマンドを実行してください。
1919

2020
```bash
2121
npm run lint
2222
```
2323

24-
if there are any prettier errors, please run the following command to fix them.
24+
もし `prettier` のエラーがある場合は、以下のコマンドを実行して修正してください。
2525

2626
```bash
2727
npx prettier . --write
2828
```
2929

30-
## mock mode
30+
## モックモード
3131

32-
To run the mock mode, run the following command.
32+
モックモードを実行するには、以下のコマンドを実行してください。
3333

3434
```bash
3535
npm run dev:mock
3636
```
3737

38-
It will run the application with the mock data.
38+
このコマンドを実行すると、モックデータを使用してアプリケーションが実行されます。
39+
40+
## 関数やクラスの説明
41+
42+
### 1. Userのデータを扱う場合 (src/app/utils/user.ts)
43+
44+
Userのデータは`User`クラスを使用して扱います。Userのデータは以下の場合があります。
45+
46+
- `npm run dev:mock`を実行した場合
47+
- Userはモックのデータが使用されます。
48+
- `npm run dev`を実行した場合
49+
- UserはlocalStorageに保存されたデータが使用されます。
50+
51+
ただ、まだユーザを登録する機能がないので、mockでユーザを作成する必要があります。

src/app/UserContext.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use client";
2+
3+
import { createContext, useContext } from "react";
4+
import { RegisterType } from "@/app/type";
5+
6+
/**
7+
* UserContext で管理するデータの型定義
8+
*/
9+
export type UserContextValue = {
10+
user: RegisterType | undefined;
11+
setUser: (newUser: RegisterType) => void;
12+
};
13+
14+
/**
15+
* ユーザー情報を提供するContext
16+
*/
17+
export const UserContext = createContext<UserContextValue | null>(null);
18+
19+
/**
20+
* カスタムフック: useUser
21+
* @returns UserContextValue - ユーザー情報とその更新関数を提供するオブジェクト
22+
*/
23+
export const useUser = () => {
24+
const context = useContext(UserContext);
25+
if (!context) {
26+
throw new Error("useUser must be used within a UserProvider");
27+
}
28+
return context;
29+
};

src/app/layout.tsx

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,58 @@ import { Inter } from "next/font/google";
44
import { useState } from "react";
55

66
import "./globals.css";
7-
import { ThemeType, ThemeProviderValue } from "@/app/type";
7+
import { ThemeType, RegisterType } from "@/app/type";
88
import { ThemeContext } from "@/app/context";
9+
import { UserContext, UserContextValue } from "@/app/UserContext";
10+
11+
import { User } from "@/app/utils/user";
912

1013
const inter = Inter({ subsets: ["latin"] });
1114

1215
/**
1316
* RootLayout コンポーネントは、アプリケーション全体のレイアウトを定義します。
14-
* @param props - コンポーネントのプロパティ。
15-
* @param props.children - レイアウト内に表示する子要素。
16-
* @returns HTMLドキュメントのルート要素としてのJSX要素。
17+
*
18+
* - テーマ(light/dark)の切り替え機能を提供します。
19+
* - ユーザー情報を管理し、アプリ全体で利用可能にします。
20+
* @param props コンポーネントのプロパティ
21+
* @param props.children レイアウト内に表示する子要素
22+
* @returns HTMLドキュメントのルート要素としてのJSX要素
1723
*/
1824
export default function RootLayout({
1925
children,
2026
}: Readonly<{
2127
children: React.ReactNode;
2228
}>) {
29+
/**
30+
* テーマ管理
31+
*/
2332
const [theme, setTheme] = useState<ThemeType>("light");
33+
34+
const userInstance = new User();
35+
const [user, setUserState] = useState<RegisterType | undefined>(
36+
userInstance.getUser(),
37+
);
38+
39+
const setUser = (newUser: RegisterType) => {
40+
userInstance.setUser(newUser);
41+
setUserState(newUser);
42+
};
43+
44+
const userContextValue: UserContextValue = {
45+
user,
46+
setUser,
47+
};
48+
2449
return (
25-
// ライト/ダークモードで背景色と文字色を変更
26-
<ThemeContext.Provider value={{ theme: theme, setTheme: setTheme }}>
27-
<html
28-
lang="ja"
29-
className={`bg-surface-container text-text-default ${theme}`}
30-
>
31-
<body className={inter.className}>{children}</body>
32-
</html>
50+
<ThemeContext.Provider value={{ theme, setTheme }}>
51+
<UserContext.Provider value={userContextValue}>
52+
<html
53+
lang="ja"
54+
className={`bg-surface-container text-text-default ${theme}`}
55+
>
56+
<body className={inter.className}>{children}</body>
57+
</html>
58+
</UserContext.Provider>
3359
</ThemeContext.Provider>
3460
);
3561
}

src/app/page.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1+
"use client";
2+
13
import Header from "@/app/components/Header";
24
import Footer from "@/app/components/Footer";
35
import Logo from "@/../../public/syllabus_icon.svg";
46
import Image from "next/image";
7+
import { useUser } from "@/app/UserContext";
8+
import { RegisterType } from "@/app/type";
9+
10+
import { useEffect } from "react";
511

612
/**
713
* Home コンポーネントは、ホームページの内容を表示します。
814
* @returns HTMLを生成するReactコンポーネント。
915
*/
1016
export default function Home(): JSX.Element {
17+
const { user, setUser } = useUser();
1118
return (
1219
<div>
1320
<Header />
14-
1521
{/* バックグラウンド画像 */}
1622
<div className={`absolute right-0 bottom-0 w-[800px] h-full`}>
1723
<Image

src/app/utils/user.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,41 @@ import { ClassDataType, RegisterType } from "@/app/type";
55

66
const MODE = process.env.NEXT_PUBLIC_MODE;
77

8-
export const fetchClasses = (): ClassDataType[] | undefined => {
9-
if (MODE === "mock") {
10-
return SampleClasses;
11-
} else {
12-
localStorage.getItem("classes");
8+
export class User {
9+
private user: RegisterType | undefined;
10+
11+
constructor() {
12+
if (typeof window !== "undefined") {
13+
if (MODE === "mock") {
14+
this.user = SampleUser;
15+
} else {
16+
const storedUser = localStorage.getItem("user");
17+
this.user = storedUser ? JSON.parse(storedUser) : undefined;
18+
}
19+
} else {
20+
this.user = undefined;
21+
}
22+
}
23+
24+
/**
25+
* ユーザー情報を取得します。
26+
* undefinedの場合は、未登録とみなします。
27+
* @returns 現在のユーザー情報
28+
*/
29+
getUser(): RegisterType | undefined {
30+
return this.user;
1331
}
14-
};
1532

16-
export const fetchUser = (): RegisterType | undefined => {
17-
if (MODE == "mock") {
18-
return SampleUser;
19-
} else {
20-
localStorage.getItem("user");
33+
/**
34+
* ユーザー情報を更新します。
35+
* クライアントサイドのみで localStorage を更新します。
36+
* @param newUser 新しいユーザー情報
37+
*/
38+
setUser(newUser: RegisterType): void {
39+
this.user = newUser;
40+
41+
if (typeof window !== "undefined" && MODE !== "mock") {
42+
localStorage.setItem("user", JSON.stringify(newUser));
43+
}
2144
}
22-
};
45+
}

0 commit comments

Comments
 (0)