From ba465c29ec846791958069c70a3a7f84bffea8e9 Mon Sep 17 00:00:00 2001 From: crab85193 Date: Wed, 18 Dec 2024 00:15:57 +0900 Subject: [PATCH 01/18] =?UTF-8?q?refactor:=20=E3=82=B3=E3=83=A1=E3=83=B3?= =?UTF-8?q?=E3=83=88=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/PopulationSelector.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/PopulationSelector.tsx b/src/components/PopulationSelector.tsx index d0ec13c..a295cd3 100644 --- a/src/components/PopulationSelector.tsx +++ b/src/components/PopulationSelector.tsx @@ -1,7 +1,7 @@ import React, { useState } from "react"; -import { fetchPopulation } from "../api/population"; // 人口データを取得するAPI -import { Prefecture } from "../types/prefecture"; // 都道府県の型 -import { PopulationCategory } from "../types/population"; // 人口データのカテゴリ型 +import { fetchPopulation } from "../api/population"; +import { Prefecture } from "../types/prefecture"; +import { PopulationCategory } from "../types/population"; interface PopulationSelectorProps { prefectures: Prefecture[]; From 194d5df4c5335a5baca06b298c4226cf7a646d10 Mon Sep 17 00:00:00 2001 From: crab85193 Date: Wed, 18 Dec 2024 00:53:33 +0900 Subject: [PATCH 02/18] =?UTF-8?q?fix:=20API=E3=81=AEURL=E3=82=92=E6=AD=A3?= =?UTF-8?q?=E3=81=97=E3=81=84=E3=82=82=E3=81=AE=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/population.ts | 2 +- src/api/prefectures.ts | 2 +- src/tests/population.test.ts | 4 ++-- src/tests/prefectures.test.ts | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/api/population.ts b/src/api/population.ts index 5792d1f..1bb0580 100644 --- a/src/api/population.ts +++ b/src/api/population.ts @@ -6,7 +6,7 @@ export const fetchPopulation = async ( ): Promise => { try { const response = await axios.get( - `${process.env.REACT_APP_YUMEMI_API_URL}/population/${prefCode}`, + `${process.env.REACT_APP_YUMEMI_API_URL}/api/v1/population/composition/perYear?${prefCode}`, { headers: { "X-API-KEY": process.env.REACT_APP_YUMEMI_API_KEY!, diff --git a/src/api/prefectures.ts b/src/api/prefectures.ts index 66061b6..0bf3fc6 100644 --- a/src/api/prefectures.ts +++ b/src/api/prefectures.ts @@ -4,7 +4,7 @@ import { Prefecture } from "../types/prefecture"; export const fetchPrefectures = async (): Promise => { try { const response = await axios.get( - `${process.env.REACT_APP_YUMEMI_API_URL}/prefectures`, + `${process.env.REACT_APP_YUMEMI_API_URL}/api/v1/prefectures`, { headers: { "X-API-KEY": process.env.REACT_APP_YUMEMI_API_KEY!, diff --git a/src/tests/population.test.ts b/src/tests/population.test.ts index ef58e3c..259c8ac 100644 --- a/src/tests/population.test.ts +++ b/src/tests/population.test.ts @@ -30,7 +30,7 @@ describe("fetchPopulation", () => { expect(result).toEqual(mockData); expect(mockedAxios.get).toHaveBeenCalledWith( - `${process.env.REACT_APP_YUMEMI_API_URL}/population/${prefCode}`, + `${process.env.REACT_APP_YUMEMI_API_URL}/api/v1/population/composition/perYear?${prefCode}`, { headers: { "X-API-KEY": process.env.REACT_APP_YUMEMI_API_KEY!, @@ -48,7 +48,7 @@ describe("fetchPopulation", () => { "Error fetching population data: Error: API Error" ); expect(mockedAxios.get).toHaveBeenCalledWith( - `${process.env.REACT_APP_YUMEMI_API_URL}/population/${prefCode}`, + `${process.env.REACT_APP_YUMEMI_API_URL}/api/v1/population/composition/perYear?${prefCode}`, { headers: { "X-API-KEY": process.env.REACT_APP_YUMEMI_API_KEY!, diff --git a/src/tests/prefectures.test.ts b/src/tests/prefectures.test.ts index f7fb20a..0b4565f 100644 --- a/src/tests/prefectures.test.ts +++ b/src/tests/prefectures.test.ts @@ -19,7 +19,7 @@ describe("fetchPrefectures", () => { expect(result).toEqual(mockData); expect(mockedAxios.get).toHaveBeenCalledWith( - `${process.env.REACT_APP_YUMEMI_API_URL}/prefectures`, + `${process.env.REACT_APP_YUMEMI_API_URL}/api/v1/prefectures`, { headers: { "X-API-KEY": process.env.REACT_APP_YUMEMI_API_KEY!, @@ -33,7 +33,7 @@ describe("fetchPrefectures", () => { await expect(fetchPrefectures()).rejects.toThrow("API Error"); expect(mockedAxios.get).toHaveBeenCalledWith( - `${process.env.REACT_APP_YUMEMI_API_URL}/prefectures`, + `${process.env.REACT_APP_YUMEMI_API_URL}/api/v1/prefectures`, { headers: { "X-API-KEY": process.env.REACT_APP_YUMEMI_API_KEY!, From 21f202a14f03aa979f098390498c7686ea0990d3 Mon Sep 17 00:00:00 2001 From: crab85193 Date: Wed, 18 Dec 2024 19:08:58 +0900 Subject: [PATCH 03/18] =?UTF-8?q?feat:=20Zustand=E3=82=92=E7=94=A8?= =?UTF-8?q?=E3=81=84=E3=81=A6=E9=83=BD=E9=81=93=E5=BA=9C=E7=9C=8C=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=A2=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 +- src/components/PrefectureCheckbox.tsx | 34 ++++------ src/store/prefecturesStore.ts | 31 +++++++++ src/tests/PrefectureCheckbox.test.tsx | 93 ++++++++++++++++----------- yarn.lock | 5 ++ 5 files changed, 105 insertions(+), 61 deletions(-) create mode 100644 src/store/prefecturesStore.ts diff --git a/package.json b/package.json index 0489116..0e43eae 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "react-dom": "^19.0.0", "react-scripts": "5.0.1", "typescript": "^4.4.2", - "web-vitals": "^2.1.0" + "web-vitals": "^2.1.0", + "zustand": "^5.0.2" }, "scripts": { "start": "react-scripts start", diff --git a/src/components/PrefectureCheckbox.tsx b/src/components/PrefectureCheckbox.tsx index 039dbd3..447f226 100644 --- a/src/components/PrefectureCheckbox.tsx +++ b/src/components/PrefectureCheckbox.tsx @@ -1,39 +1,29 @@ import React from "react"; import { Prefecture } from "../types/prefecture"; +import usePrefecturesStore from "../store/prefecturesStore"; -interface PrefectureCheckboxProps { - prefectures: Prefecture[]; - selectedPrefectures: string[]; - onSelect: (selected: string[]) => void; -} - -const PrefectureCheckbox: React.FC = ({ +const PrefectureCheckbox: React.FC<{ prefectures: Prefecture[] }> = ({ prefectures, - selectedPrefectures, - onSelect, }) => { - const handleCheckboxChange = (prefCode: string) => { - if (selectedPrefectures.includes(prefCode)) { - onSelect(selectedPrefectures.filter((code) => code !== prefCode)); - } else { - onSelect([...selectedPrefectures, prefCode]); - } - }; + const { selectedPrefectures, togglePrefecture, selectAll, clearSelection } = + usePrefecturesStore(); return (

都道府県選択

+ + {prefectures.map((prefecture) => (
- handleCheckboxChange(prefecture.prefCode.toString()) - } + checked={selectedPrefectures.includes(prefecture.prefCode)} + onChange={() => togglePrefecture(prefecture.prefCode)} />