Skip to content

Commit b96bb1d

Browse files
committed
✨ feat : Add material state management
- Add material state management - Modify components to retrieve values via material state management Related issue: YJU-OKURA#93
1 parent c2568f3 commit b96bb1d

File tree

7 files changed

+41
-13
lines changed

7 files changed

+41
-13
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ import {PromptMessagesProps} from '@/src/interfaces/prompt';
99
import icons from '@/public/svgs/prompt';
1010
import '@/src/styles/variable.css';
1111

12-
const PromptChat = () => {
12+
const PromptChat = ({pId}: {pId: number}) => {
1313
const [messages, setMsg] = useState<PromptMessagesProps[]>();
1414
const [inputMsg, setInputMsg] = useState<string>('');
1515
const [promptRes, setPromptRes] = useState<string>('');
1616
const [reload, setReload] = useState<boolean>(false);
1717

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

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

5757
useEffect(() => {
5858
if (inputMsg === '') return;
59-
postPrompt(4, 126, inputMsg, chat).then(() => {
59+
postPrompt(4, pId, inputMsg, chat).then(() => {
6060
setInputMsg('');
6161
});
6262
}, [inputMsg]);

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import patchMessage from '@/src/api/prompts/patchMessage';
77
import {StorageMessage} from '@/src/interfaces/prompt';
88
import icons from '@/public/svgs/prompt';
99

10-
const Storage = () => {
10+
const Storage = ({pId}: {pId: number}) => {
1111
const [isOpen, setIsOpen] = useState<boolean[]>([]);
1212
const [messages, setMsg] = useState<StorageMessage[]>();
1313
const [reload, setReload] = useState<boolean>(false);
1414

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

2424
const handleClickDelete = (messageId: number) => {
2525
// コメントを削除する処理
26-
patchMessage(1, 1, messageId, false).then(res => {
26+
patchMessage(4, pId, messageId, false).then(res => {
2727
console.log(res);
2828
setReload(!reload);
2929
});

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
'use client';
22
import {useState} from 'react';
3+
import {useRecoilValue} from 'recoil';
34
import {Dashboard, TabsMapping} from '@/src/components/dashboard';
45
import PromptChat from './PromptChat';
56
import Storage from './Storage';
7+
import materialState from '@/src/recoil/atoms/materialState';
68
import '@/src/styles/variable.css';
79

810
const SubContainer = () => {
911
const TABS = ['PromptChat', 'Storage'];
1012
const [activeTab, setActiveTab] = useState(TABS[0]);
13+
const material = useRecoilValue(materialState);
1114
const tabMapping = {
12-
PromptChat: <PromptChat />,
13-
Storage: <Storage />,
15+
PromptChat: <PromptChat pId={material ? material?.prompts[0].id : 0} />,
16+
Storage: <Storage pId={material ? material?.prompts[0].id : 0} />,
1417
};
1518

1619
return (

src/app/[className]/[materialName]/page.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
'use client';
2+
import {useRecoilValue} from 'recoil';
13
import {ManageContainer, UserContainer} from './components';
24
import {SubContainer} from './components/subComponents';
3-
import User from '@/src/model/User';
5+
import classUserState from '@/src/recoil/atoms/classUserState';
6+
import ROLES from '@/src/constants/roles';
47

58
const Page = () => {
9+
const classUser = useRecoilValue(classUserState);
610
return (
711
<div className="w-full box-border ">
8-
{User.managerRoll === 'manager' ? (
12+
{classUser && ROLES[classUser?.role_id] === 'ADMIN' ? (
913
<div className="p-4">
1014
<div className="text-5xl py-4 border-b-2">SubjectName</div>
1115
<ManageContainer />

src/components/navbar/material/MaterialList.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import deleteMaterial from '@/src/api/material/deleteMaterial';
66
import postPromptAccess from '@/src/api/prompts/postPromptAccess';
77
import {Material, ParamsProps} from '@/src/interfaces/navbar';
88
import icons from '@/public/svgs/navbar';
9+
import {useSetRecoilState} from 'recoil';
10+
import materialState from '@/src/recoil/atoms/materialState';
911

1012
const MaterialList = ({
1113
materials,
@@ -19,6 +21,7 @@ const MaterialList = ({
1921
const [isToggleOpen, setIsToggleOpen] = useState<boolean[]>([]);
2022
const [isOpen, setIsOpen] = useState<boolean>(false);
2123
const [editData, setEditData] = useState<Material>();
24+
const setMaterialState = useSetRecoilState(materialState);
2225

2326
const handleClickSubject = (mId: number) => {
2427
if (materials[mId] && materials[mId].prompts.length === 0 && cId) {
@@ -57,7 +60,10 @@ const MaterialList = ({
5760
className="flex w-full items-center justify-between"
5861
onClick={() => handleClickSubject(parseInt(material.id))}
5962
>
60-
<Link href={`/${params.className}/${material.name}`}>
63+
<Link
64+
href={`/${params.className}/${material.name}`}
65+
onClick={() => setMaterialState(material)}
66+
>
6167
{material.name}
6268
</Link>
6369
<Image

src/interfaces/navbar/material.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ interface Material {
44
file: {
55
m_path: string;
66
};
7-
prompts: [];
7+
prompts: {
8+
id: number;
9+
}[];
810
}
911

1012
export default Material;

src/recoil/atoms/materialState.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {atom} from 'recoil';
2+
import {Material} from '@/src/interfaces/navbar';
3+
import {recoilPersist} from 'recoil-persist';
4+
5+
const {persistAtom} = recoilPersist();
6+
7+
const materialState = atom<Material | null>({
8+
key: 'materialState',
9+
default: null,
10+
effects_UNSTABLE: [persistAtom],
11+
});
12+
13+
export default materialState;

0 commit comments

Comments
 (0)