Skip to content

Commit

Permalink
(#82) ๐Ÿš€ feat: ์Šคํƒ€๋””์›€ ๊ด€๋ จ ๋ฐ์ดํ„ฐ๋Š” ๋ชจ๋‘ ์ „์—ญ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋„๋ก StadiumContext์— ์ถ”๊ฐ€
Browse files Browse the repository at this point in the history
  • Loading branch information
inaemon committed Nov 30, 2024
1 parent 458e384 commit afb9e4a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
30 changes: 29 additions & 1 deletion src/context/StadiumContext.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
import React, { createContext, useState, useContext, ReactNode } from "react";
import { StadiumType } from "@/src/constants/ZoneData";
import { ZoneGetResponseType } from "@/src/api/StadiumApiType";

// 1. Context ํƒ€์ž… ์ •์˜
interface StadiumContextType {
selectedStadium: StadiumType;
setSelectedStadium: (stadium: StadiumType) => void;
selectedSection: string;
setSelectedSection: (section: string) => void;
selectedSectionColor: string;
setSelectedSectionColor: (color: string) => void;
zoneNameList: string[];
setZoneNameList: (zones: string[]) => void;
stadiumInfo: ZoneGetResponseType | undefined;
setStadiumInfo: (info: ZoneGetResponseType | undefined) => void;
}

// 2. ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •
const StadiumContext = createContext<StadiumContextType | undefined>(undefined);

// 3. Context Provider ์ปดํฌ๋„ŒํŠธ
export const StadiumProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
// ํ”„๋ก ํŠธ์—์„œ ์„ ํƒํ•œ ์Šคํƒ€๋””์›€ ๊ฐ’ ์ „์—ญ ๊ด€๋ฆฌ ๋ณ€์ˆ˜
const [selectedStadium, setSelectedStadium] = useState<StadiumType>(StadiumType.JAMSIL); // ์ดˆ๊ธฐ๊ฐ’: ์ž ์‹ค

// ํ”„๋ก ํŠธ์—์„œ ์„ ํƒํ•œ ์Šคํƒ€๋””์›€์— ๋”ฐ๋ฅธ ์ขŒ์„(zone) ๋ฆฌ์ŠคํŠธ ์ „์—ญ ๊ด€๋ฆฌ ๋ณ€์ˆ˜
const [zoneNameList, setZoneNameList] = useState<string[]>([]);

// ํ”„๋ก ํŠธ์—์„œ ์„ ํƒํ•œ ์Šคํƒ€๋””์›€์— ๋”ฐ๋ฅธ ์ขŒ์„(zone) ๊ฐ’ ์ „์—ญ ๊ด€๋ฆฌ ๋ณ€์ˆ˜
const [selectedSection, setSelectedSection] = useState<string>("");

// ์ขŒ์„ ์ƒ‰์ƒ, selectedSection๊ณผ mapping
const [selectedSectionColor, setSelectedSectionColor] = useState<string>("");

// ๋ฐฑ์—”๋“œ๋กœ๋ถ€ํ„ฐ ๋ฐ›์•„์˜จ ์Šคํƒ€๋””์›€ ์ •๋ณด ๊ฐ’ ์ „์—ญ ๊ด€๋ฆฌ ๋ณ€์ˆ˜
const [stadiumInfo, setStadiumInfo] = useState<ZoneGetResponseType>();

return (
<StadiumContext.Provider value={{ selectedStadium, setSelectedStadium }}>
<StadiumContext.Provider value={{
selectedStadium, setSelectedStadium,
selectedSection, setSelectedSection,
selectedSectionColor, setSelectedSectionColor,
zoneNameList, setZoneNameList,
stadiumInfo, setStadiumInfo
}}>
{children}
</StadiumContext.Provider>
);
Expand Down
30 changes: 14 additions & 16 deletions src/hooks/useStadiumSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,27 @@ import { handleGetStadiumInfo } from "@/src/api/StadiumApiHandler"; // API ํ˜ธ
import { useStadiumContext } from "@/src/context/StadiumContext";

export const useStadiumSelector = () => {
const { selectedStadium, setSelectedStadium } = useStadiumContext();
const [selectedSection, setSelectedSection] = useState<string>("");
const [selectedSectionColor, setSelectedSectionColor] = useState<string>("");

// zoneName๋งŒ ์ถ”์ถœ
const [zoneNameList, setZoneNameList] = useState<string[]>([]);

// ์Šคํƒ€๋””์›€ API ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ
const [stadiumInfo, setStadiumInfo] = useState<ZoneGetResponseType>();
const {
selectedStadium, setSelectedStadium,
selectedSection, setSelectedSection,
selectedSectionColor, setSelectedSectionColor,
zoneNameList, setZoneNameList,
stadiumInfo, setStadiumInfo
} = useStadiumContext();

// ์Šคํƒ€๋””์›€ ๋ณ€๊ฒฝ ์‹œ, ์Šคํƒ€๋””์›€ ์ •๋ณด ๋ฐ ๊ตฌ์—ญ๋ช… ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
const handleStadiumInfo = async () => {
// ์œ ํšจํ•˜์ง€ ์•Š์œผ๋ฉด API ํ˜ธ์ถœํ•˜์ง€ ์•Š์Œ
if (!selectedStadium) {
console.log("์œ ํšจํ•˜์ง€ ์•Š์€ ์Šคํƒ€๋””์›€ ๊ฐ’์ž…๋‹ˆ๋‹ค: " + selectedStadium);
return;
}

const params: ZoneGetParamsType = {
stadiumName: selectedStadium as string,
};

// API ํ˜ธ์ถœ
const stadiumApiData = await handleGetStadiumInfo(params);
if (stadiumApiData) {
setStadiumInfo(stadiumApiData);
Expand All @@ -37,13 +42,6 @@ export const useStadiumSelector = () => {

// ์Šคํƒ€๋””์›€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ
useEffect(() => {
// ์œ ํšจํ•˜์ง€ ์•Š์œผ๋ฉด API ํ˜ธ์ถœํ•˜์ง€ ์•Š์Œ
if (!selectedStadium) {
console.log("์œ ํšจํ•˜์ง€ ์•Š์€ ์Šคํƒ€๋””์›€ ๊ฐ’์ž…๋‹ˆ๋‹ค: " + selectedStadium);
return;
}

// API ํ˜ธ์ถœ
handleStadiumInfo();
}, [selectedStadium, selectedSection]); // selectedStadium ๋˜๋Š” selectedSection์ด ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰

Expand Down

0 comments on commit afb9e4a

Please sign in to comment.