Skip to content

Commit 0b10f71

Browse files
committed
add: mentorsData context #101
1 parent 8054f03 commit 0b10f71

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/app/page.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import styles from './page.module.scss';
22
import MentorIndex from '@/components/Home/MentorIndex';
3+
import MentorsDataProvider from '@/middleware/MentorsDataProvider';
34
import { MentorType } from '@/types/mentorType';
45

56
async function getMentorsData() {
@@ -14,7 +15,9 @@ export default async function Home() {
1415

1516
return (
1617
<div className={styles['container']}>
17-
<MentorIndex mentorsData={mentorsData} />
18+
<MentorsDataProvider mentors={mentorsData}>
19+
<MentorIndex mentorsData={mentorsData} />
20+
</MentorsDataProvider>
1821
</div>
1922
)
2023
}

src/components/UI/Modal/MemberFormModal.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { SetStateAction, useRef, useState } from "react";
3+
import { SetStateAction, useContext, useRef, useState } from "react";
44
import styles from "./modal.module.scss";
55
import Image from "next/image";
66
import ImageFormModal from "./ImageFormModal";
@@ -9,6 +9,7 @@ import { CardDesignType } from "@/types/cardDesignType";
99
import { toast } from "react-toastify";
1010
import { RxCross2 } from "react-icons/rx";
1111
import { CgAdd, CgSelectO } from "react-icons/cg";
12+
import { MentorsDataContext } from "@/middleware/MentorsDataProvider";
1213

1314
const postMessage = async(memberName: string, body: string, cardDesign: number, mentorId: number) => {
1415
try {
@@ -38,6 +39,9 @@ export default function MemberFormModal({
3839
const [isImgModalOpen, setIsImgModalOpen] = useState(false);
3940
const textareaRef = useRef<any>(null);
4041

42+
const mentorsData = useContext(MentorsDataContext);
43+
const mentorData = mentorsData.find((item) => item.id === id);
44+
4145
const handleMessageChange = (e: { target: { value: SetStateAction<string>; }; }) => {
4246
setMessage(e.target.value);
4347
const element = textareaRef.current;
@@ -78,7 +82,7 @@ export default function MemberFormModal({
7882
<div className={styles["close-button"]} onClick={onClose}>
7983
<RxCross2 />
8084
</div>
81-
<p className={styles['modal-title']}><span>メンター</span></p>
85+
<p className={styles['modal-title']}><span>{mentorData?.name}</span></p>
8286
<div className={styles['index-container']}>
8387
<div className={styles['input-container']}>
8488
<div className={styles['input-top-box']}>
@@ -153,9 +157,6 @@ export default function MemberFormModal({
153157
<div className={styles["modal-bg-black"]} onClick={onClose} />
154158
{isImgModalOpen && (
155159
<ImageFormModal
156-
id={id}
157-
memberName={memberName}
158-
message={message}
159160
designNumber={cardDesign ? cardDesign.id : 0}
160161
onClose={handleImgModal}
161162
setCardDesign={handleCardDesign}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use client'
2+
3+
import { createContext, useEffect, useState } from "react";
4+
import { MentorType } from "@/types/mentorType";
5+
6+
export const MentorsDataContext = createContext<MentorType[]>([]);
7+
8+
export default function MentorsDataProvider({
9+
children, mentors,
10+
}: {
11+
children: React.ReactNode, mentors: MentorType[],
12+
}) {
13+
const [mentorsData, setMentorsData] = useState<MentorType[]>(mentors);
14+
15+
useEffect(() => {
16+
setMentorsData(mentors);
17+
}, [mentors]);
18+
19+
return (
20+
<MentorsDataContext.Provider value={mentorsData}>
21+
{children}
22+
</MentorsDataContext.Provider>
23+
);
24+
};

0 commit comments

Comments
 (0)