Skip to content

Commit c2568f3

Browse files
authored
Merge pull request #118 from dorimu0/refact/api-login
クラスidを通じたユーザー情報関数の追加及びログイン状態管理の修正
2 parents 47e6676 + c1695d5 commit c2568f3

File tree

19 files changed

+161
-98
lines changed

19 files changed

+161
-98
lines changed

src/api/apiUtils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ api.interceptors.response.use(
4040
}
4141
} catch (error) {
4242
console.error('トークンの有効期限が切れました。');
43+
window.location.href = '/intro';
4344
}
4445
}
4546
return Promise.reject(error);

src/api/classUser/getClassInfo.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import req from '../apiUtils';
2+
3+
const getClassInfo = async (uId: number, cId: number) => {
4+
const response = await req(`/cu/${uId}/${cId}/info`, 'get', 'gin');
5+
console.log(response);
6+
7+
return response.data;
8+
};
9+
10+
export default getClassInfo;

src/api/prompts/postPrompt.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ const postPrompt = async (
44
message: string,
55
chat: (reader: ReadableStreamDefaultReader) => void
66
) => {
7+
const token = localStorage.getItem('access_token');
8+
console.log('token:', token);
79
const body = {
810
message: message,
911
};
12+
console.log('body:', body);
1013
try {
1114
const response = await fetch(
1215
`http://3.38.86.236:3000/api/nest/class/${cId}/prompts/${id}`,
1316
{
1417
method: 'POST',
1518
headers: {
16-
Authorization:
17-
'Bearer eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MiwiZXhwIjoxOTY5OTAxMDIyfQ.U0k1q2oTrp3JwsIpem16o2W77tpVGiwylwc5cTFaZgU',
18-
'Content-Type': 'application/json',
19+
Authorization: `Bearer ${token}`,
1920
},
2021
body: JSON.stringify(body),
2122
}

src/app/[className]/[materialName]/components/subComponents/PromptChat.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ const PromptChat = () => {
1616
const [reload, setReload] = useState<boolean>(false);
1717

1818
useEffect(() => {
19-
getPrompt(1, 1, 1, 6).then(res => {
19+
getPrompt(4, 126, 1, 6).then(res => {
2020
res.messages.reverse();
2121
setMsg(res.messages);
2222
});
2323
}, [reload]);
2424

2525
const handleClickIcon = (mId: number) => {
26-
patchMessage(1, 1, mId, true).then(res => {
26+
patchMessage(4, 126, mId, true).then(res => {
2727
console.log(res);
2828
});
2929
};
@@ -56,8 +56,7 @@ const PromptChat = () => {
5656

5757
useEffect(() => {
5858
if (inputMsg === '') return;
59-
postPrompt(1, 1, inputMsg, chat).then(res => {
60-
console.log(res);
59+
postPrompt(4, 126, inputMsg, chat).then(() => {
6160
setInputMsg('');
6261
});
6362
}, [inputMsg]);

src/app/[className]/[materialName]/components/subComponents/Storage.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const Storage = () => {
1414

1515
useEffect(() => {
1616
// コメントを取得する処理
17-
getMessage(1, 1, 1, 5).then(res => {
17+
getMessage(4, 126, 1, 5).then(res => {
1818
console.log(res);
1919
setMsg(res);
2020
setIsOpen(new Array(res.length).fill(false)); // コメントの開閉状態を初期化

src/app/intro/googleLogin/page.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const Page = () => {
1717
const setRefreshToken = useSetRecoilState(refreshTokenState);
1818
const router = useRouter();
1919
useEffect(() => {
20-
console.log(code);
2120
if (code) {
2221
postGoogleLogin(code).then(res => {
2322
if (res.user) {

src/app/layout.tsx

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type {Metadata} from 'next';
22
import {Inter} from 'next/font/google';
33
import {Navbar} from '../components/navbar';
44
import {Footer} from '../components/footer';
5-
import Protect from '../components/protect';
65
import RecoilRootContainer from '../components/RecoilRootContainer';
76
import './globals.css';
87
import '@/src/styles/variable.css';
@@ -19,15 +18,13 @@ const RootLayout = ({children}: {children: React.ReactNode}) => {
1918
<html lang="en">
2019
<body className={inter.className}>
2120
<RecoilRootContainer>
22-
<Protect>
23-
<div className="flex h-full relative">
24-
<Navbar />
25-
<div className="grow overflow-x-auto overflow-y-auto">
26-
<div className="mainContainer">{children}</div>
27-
<Footer />
28-
</div>
21+
<div className="flex h-full relative">
22+
<Navbar />
23+
<div className="grow overflow-x-auto overflow-y-auto">
24+
<div className="mainContainer">{children}</div>
25+
<Footer />
2926
</div>
30-
</Protect>
27+
</div>
3128
</RecoilRootContainer>
3229
</body>
3330
</html>

src/components/navbar/Navbar.tsx

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22
import {useState} from 'react';
33
import Image from 'next/image';
44
import Link from 'next/link';
5-
import {useParams, usePathname} from 'next/navigation';
6-
import {useRecoilValue} from 'recoil';
7-
import userState from '@/src/recoil/atoms/userState';
5+
import {useParams, usePathname, useSearchParams} from 'next/navigation';
86
import {MaterialContainer, MaterialForm} from './material';
97
import Profile from './profile';
10-
import {User} from '@/src/interfaces/user';
118
import icons from '@/public/svgs/navbar';
129
import '@/src/styles/variable.css';
1310

1411
const Navbar = () => {
15-
const user = useRecoilValue(userState) as User;
1612
const [isOpen, setIsOpen] = useState<boolean>(false);
1713

1814
const pages = [
@@ -24,6 +20,8 @@ const Navbar = () => {
2420

2521
const router = usePathname();
2622
const params = useParams<{className: string; materialName: string}>();
23+
const searchParams = useSearchParams();
24+
const search = searchParams.get('id');
2725

2826
if (router === '/intro' || router === '/intro/googleLogin') {
2927
return null;
@@ -33,7 +31,7 @@ const Navbar = () => {
3331
<div className="w-72 h-full bg-gray-50">
3432
<div className="relative w-72 px-6 pt-5 navbar flex flex-col">
3533
{/* Profile */}
36-
<Profile user={user} params={params} />
34+
<Profile params={params} cId={search} />
3735
<div className="h-px bg-zinc-300"></div>
3836
<div className="h-8"></div>
3937

@@ -82,7 +80,7 @@ const Navbar = () => {
8280
</div>
8381
{isOpen ? <MaterialForm setIsOpen={setIsOpen} /> : null}
8482
</div>
85-
<MaterialContainer params={params} />
83+
<MaterialContainer params={params} cId={search} />
8684
</div>
8785
<div className="h-16"></div>
8886

src/components/navbar/material/MaterialContainer.tsx

+14-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import searchMaterial from '@/src/api/material/searchMaterial';
77
import {Material, ParamsProps} from '@/src/interfaces/navbar';
88
import icons from '@/public/svgs/navbar';
99

10-
const MaterialContainer = ({params}: {params: ParamsProps}) => {
10+
const MaterialContainer = ({
11+
params,
12+
cId,
13+
}: {
14+
params: ParamsProps;
15+
cId: string | null;
16+
}) => {
1117
const [materials, setMaterials] = useState<Material[]>([]);
1218
const [searchMaterials, setSearchMaterials] = useState<Material[]>([]);
1319
const [keyWord, setKeyWord] = useState<string>('');
@@ -16,7 +22,7 @@ const MaterialContainer = ({params}: {params: ParamsProps}) => {
1622

1723
const onLoadMore = () => {
1824
setHasMore(false);
19-
getMaterial(1, boardPage, 8).then(res => {
25+
getMaterial(4, boardPage, 8).then(res => {
2026
if (res.length === 0) {
2127
setHasMore(false);
2228
} else {
@@ -85,9 +91,13 @@ const MaterialContainer = ({params}: {params: ParamsProps}) => {
8591
>
8692
{materials ? (
8793
keyWord ? (
88-
<MaterialList materials={searchMaterials} params={params} />
94+
<MaterialList
95+
materials={searchMaterials}
96+
params={params}
97+
cId={cId}
98+
/>
8999
) : (
90-
<MaterialList materials={materials} params={params} />
100+
<MaterialList materials={materials} params={params} cId={cId} />
91101
)
92102
) : null}
93103
</InfiniteScroll>

src/components/navbar/material/MaterialForm.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
import {ChangeEvent, useRef, useState} from 'react';
1+
import {ChangeEvent, useEffect, useRef, useState} from 'react';
22
import Image from 'next/image';
33
import postMaterial from '@/src/api/material/postMaterial';
44
import {FormProps} from '@/src/interfaces/navbar';
55
import icons from '@/public/svgs/navbar/prompt';
66

7-
const MaterialForm = ({setIsOpen}: FormProps) => {
7+
const MaterialForm = ({setIsOpen, editData}: FormProps) => {
88
const inputRef = useRef<HTMLInputElement>(null);
99
const [materialName, setMaterialName] = useState<string>('');
1010
const [material, setMaterial] = useState<File>();
1111

12+
useEffect(() => {
13+
if (editData) {
14+
console.log(editData);
15+
}
16+
}, []);
17+
1218
const handleEnterName = (e: ChangeEvent<HTMLInputElement>) => {
1319
setMaterialName(e.target.value);
1420
};
@@ -28,7 +34,7 @@ const MaterialForm = ({setIsOpen}: FormProps) => {
2834
const handleClickButton = () => {
2935
console.log(material, materialName);
3036
if (material && materialName) {
31-
postMaterial(1, materialName, material);
37+
postMaterial(4, materialName, material);
3238
}
3339
};
3440

src/components/navbar/material/MaterialList.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ import icons from '@/public/svgs/navbar';
1010
const MaterialList = ({
1111
materials,
1212
params,
13+
cId,
1314
}: {
1415
materials: Material[];
1516
params: ParamsProps;
17+
cId: string | null;
1618
}) => {
1719
const [isToggleOpen, setIsToggleOpen] = useState<boolean[]>([]);
1820
const [isOpen, setIsOpen] = useState<boolean>(false);
21+
const [editData, setEditData] = useState<Material>();
1922

2023
const handleClickSubject = (mId: number) => {
21-
if (materials[mId] && materials[mId].prompts.length === 0) {
22-
postPromptAccess(1, mId);
24+
if (materials[mId] && materials[mId].prompts.length === 0 && cId) {
25+
postPromptAccess(parseInt(cId), mId);
2326
}
2427
};
2528

@@ -70,6 +73,7 @@ const MaterialList = ({
7073
<div
7174
className="p-2 hover:bg-gray-200"
7275
onClick={() => {
76+
setEditData(material);
7377
setIsOpen(true);
7478
setIsToggleOpen(prev => prev.map(() => false));
7579
}}
@@ -89,7 +93,9 @@ const MaterialList = ({
8993
</li>
9094
);
9195
})}
92-
{isOpen ? <MaterialForm setIsOpen={setIsOpen} /> : null}
96+
{isOpen ? (
97+
<MaterialForm setIsOpen={setIsOpen} editData={editData} />
98+
) : null}
9399
</ul>
94100
</div>
95101
);

0 commit comments

Comments
 (0)