Skip to content

Commit c1695d5

Browse files
committed
♻️ refactor : Modify API requests based on classId
- Modify API requests based on the specified class user on prompt and material pages. - Fix to get locally stored token in postPrompt function Related issue: #104
1 parent 76c2295 commit c1695d5

File tree

6 files changed

+40
-18
lines changed

6 files changed

+40
-18
lines changed

src/api/prompts/postPrompt.ts

Lines changed: 4 additions & 3 deletions
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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/components/navbar/material/MaterialContainer.tsx

Lines changed: 14 additions & 4 deletions
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

Lines changed: 9 additions & 3 deletions
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

Lines changed: 9 additions & 3 deletions
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)