Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

クラスidを通じたユーザー情報関数の追加及びログイン状態管理の修正 #118

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/api/apiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ api.interceptors.response.use(
}
} catch (error) {
console.error('トークンの有効期限が切れました。');
window.location.href = '/intro';
}
}
return Promise.reject(error);
Expand Down
10 changes: 10 additions & 0 deletions src/api/classUser/getClassInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import req from '../apiUtils';

const getClassInfo = async (uId: number, cId: number) => {
const response = await req(`/cu/${uId}/${cId}/info`, 'get', 'gin');
console.log(response);

return response.data;
};

export default getClassInfo;
7 changes: 4 additions & 3 deletions src/api/prompts/postPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ const postPrompt = async (
message: string,
chat: (reader: ReadableStreamDefaultReader) => void
) => {
const token = localStorage.getItem('access_token');
console.log('token:', token);
const body = {
message: message,
};
console.log('body:', body);
try {
const response = await fetch(
`http://3.38.86.236:3000/api/nest/class/${cId}/prompts/${id}`,
{
method: 'POST',
headers: {
Authorization:
'Bearer eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MiwiZXhwIjoxOTY5OTAxMDIyfQ.U0k1q2oTrp3JwsIpem16o2W77tpVGiwylwc5cTFaZgU',
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(body),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const PromptChat = () => {
const [reload, setReload] = useState<boolean>(false);

useEffect(() => {
getPrompt(1, 1, 1, 6).then(res => {
getPrompt(4, 126, 1, 6).then(res => {
res.messages.reverse();
setMsg(res.messages);
});
}, [reload]);

const handleClickIcon = (mId: number) => {
patchMessage(1, 1, mId, true).then(res => {
patchMessage(4, 126, mId, true).then(res => {
console.log(res);
});
};
Expand Down Expand Up @@ -56,8 +56,7 @@ const PromptChat = () => {

useEffect(() => {
if (inputMsg === '') return;
postPrompt(1, 1, inputMsg, chat).then(res => {
console.log(res);
postPrompt(4, 126, inputMsg, chat).then(() => {
setInputMsg('');
});
}, [inputMsg]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Storage = () => {

useEffect(() => {
// コメントを取得する処理
getMessage(1, 1, 1, 5).then(res => {
getMessage(4, 126, 1, 5).then(res => {
console.log(res);
setMsg(res);
setIsOpen(new Array(res.length).fill(false)); // コメントの開閉状態を初期化
Expand Down
1 change: 0 additions & 1 deletion src/app/intro/googleLogin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const Page = () => {
const setRefreshToken = useSetRecoilState(refreshTokenState);
const router = useRouter();
useEffect(() => {
console.log(code);
if (code) {
postGoogleLogin(code).then(res => {
if (res.user) {
Expand Down
15 changes: 6 additions & 9 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {Metadata} from 'next';
import {Inter} from 'next/font/google';
import {Navbar} from '../components/navbar';
import {Footer} from '../components/footer';
import Protect from '../components/protect';
import RecoilRootContainer from '../components/RecoilRootContainer';
import './globals.css';
import '@/src/styles/variable.css';
Expand All @@ -19,15 +18,13 @@ const RootLayout = ({children}: {children: React.ReactNode}) => {
<html lang="en">
<body className={inter.className}>
<RecoilRootContainer>
<Protect>
<div className="flex h-full relative">
<Navbar />
<div className="grow overflow-x-auto overflow-y-auto">
<div className="mainContainer">{children}</div>
<Footer />
</div>
<div className="flex h-full relative">
<Navbar />
<div className="grow overflow-x-auto overflow-y-auto">
<div className="mainContainer">{children}</div>
<Footer />
</div>
</Protect>
</div>
</RecoilRootContainer>
</body>
</html>
Expand Down
12 changes: 5 additions & 7 deletions src/components/navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
import {useState} from 'react';
import Image from 'next/image';
import Link from 'next/link';
import {useParams, usePathname} from 'next/navigation';
import {useRecoilValue} from 'recoil';
import userState from '@/src/recoil/atoms/userState';
import {useParams, usePathname, useSearchParams} from 'next/navigation';
import {MaterialContainer, MaterialForm} from './material';
import Profile from './profile';
import {User} from '@/src/interfaces/user';
import icons from '@/public/svgs/navbar';
import '@/src/styles/variable.css';

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

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

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

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

Expand Down Expand Up @@ -82,7 +80,7 @@ const Navbar = () => {
</div>
{isOpen ? <MaterialForm setIsOpen={setIsOpen} /> : null}
</div>
<MaterialContainer params={params} />
<MaterialContainer params={params} cId={search} />
</div>
<div className="h-16"></div>

Expand Down
18 changes: 14 additions & 4 deletions src/components/navbar/material/MaterialContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import searchMaterial from '@/src/api/material/searchMaterial';
import {Material, ParamsProps} from '@/src/interfaces/navbar';
import icons from '@/public/svgs/navbar';

const MaterialContainer = ({params}: {params: ParamsProps}) => {
const MaterialContainer = ({
params,
cId,
}: {
params: ParamsProps;
cId: string | null;
}) => {
const [materials, setMaterials] = useState<Material[]>([]);
const [searchMaterials, setSearchMaterials] = useState<Material[]>([]);
const [keyWord, setKeyWord] = useState<string>('');
Expand All @@ -16,7 +22,7 @@ const MaterialContainer = ({params}: {params: ParamsProps}) => {

const onLoadMore = () => {
setHasMore(false);
getMaterial(1, boardPage, 8).then(res => {
getMaterial(4, boardPage, 8).then(res => {
if (res.length === 0) {
setHasMore(false);
} else {
Expand Down Expand Up @@ -85,9 +91,13 @@ const MaterialContainer = ({params}: {params: ParamsProps}) => {
>
{materials ? (
keyWord ? (
<MaterialList materials={searchMaterials} params={params} />
<MaterialList
materials={searchMaterials}
params={params}
cId={cId}
/>
) : (
<MaterialList materials={materials} params={params} />
<MaterialList materials={materials} params={params} cId={cId} />
)
) : null}
</InfiniteScroll>
Expand Down
12 changes: 9 additions & 3 deletions src/components/navbar/material/MaterialForm.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import {ChangeEvent, useRef, useState} from 'react';
import {ChangeEvent, useEffect, useRef, useState} from 'react';
import Image from 'next/image';
import postMaterial from '@/src/api/material/postMaterial';
import {FormProps} from '@/src/interfaces/navbar';
import icons from '@/public/svgs/navbar/prompt';

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

useEffect(() => {
if (editData) {
console.log(editData);
}
}, []);

const handleEnterName = (e: ChangeEvent<HTMLInputElement>) => {
setMaterialName(e.target.value);
};
Expand All @@ -28,7 +34,7 @@ const MaterialForm = ({setIsOpen}: FormProps) => {
const handleClickButton = () => {
console.log(material, materialName);
if (material && materialName) {
postMaterial(1, materialName, material);
postMaterial(4, materialName, material);
}
};

Expand Down
12 changes: 9 additions & 3 deletions src/components/navbar/material/MaterialList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import icons from '@/public/svgs/navbar';
const MaterialList = ({
materials,
params,
cId,
}: {
materials: Material[];
params: ParamsProps;
cId: string | null;
}) => {
const [isToggleOpen, setIsToggleOpen] = useState<boolean[]>([]);
const [isOpen, setIsOpen] = useState<boolean>(false);
const [editData, setEditData] = useState<Material>();

const handleClickSubject = (mId: number) => {
if (materials[mId] && materials[mId].prompts.length === 0) {
postPromptAccess(1, mId);
if (materials[mId] && materials[mId].prompts.length === 0 && cId) {
postPromptAccess(parseInt(cId), mId);
}
};

Expand Down Expand Up @@ -70,6 +73,7 @@ const MaterialList = ({
<div
className="p-2 hover:bg-gray-200"
onClick={() => {
setEditData(material);
setIsOpen(true);
setIsToggleOpen(prev => prev.map(() => false));
}}
Expand All @@ -89,7 +93,9 @@ const MaterialList = ({
</li>
);
})}
{isOpen ? <MaterialForm setIsOpen={setIsOpen} /> : null}
{isOpen ? (
<MaterialForm setIsOpen={setIsOpen} editData={editData} />
) : null}
</ul>
</div>
);
Expand Down
Loading
Loading