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

graphコンポーネントのuseEffectをhooksにまとめる #73

Merged
merged 2 commits into from
Nov 3, 2023
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
33 changes: 9 additions & 24 deletions src/components/Graph.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use client';
import { usePopulation } from '@/hooks/api/usePopulation';
import { useHighcharts } from '@/hooks/useHighcharts';
import { useGrpph } from '@/hooks/useGraph';
import Highcharts from 'highcharts';
import HighchartsAccessibility from 'highcharts/modules/accessibility';
import HighchartsReact from 'highcharts-react-official';
import { type Dispatch, type SetStateAction, useEffect } from 'react';
import { type Dispatch, type SetStateAction } from 'react';
import type {
DisplayConditions,
PrefecturesList,
Expand Down Expand Up @@ -37,29 +38,13 @@ export const Graph = ({
displayCondition,
graphData,
});

useEffect(() => setIsLoading(isLoading), [isLoading, setIsLoading]);

useEffect(() => {
if (!populationData) return;

const data = populationData.result;
const prefName = currentPrefectures.prefName;

setGraphData((prev) => {
if (!prev) {
return [{ result: data, prefName }];
}

const exsistData = prev.filter((item) => item.prefName === prefName);

if (exsistData.length === 0) {
return [...prev, { result: data, prefName }];
}

return prev;
});
}, [currentPrefectures, populationData, setGraphData]);
useGrpph({
currentPrefectures,
isLoading,
populationData,
setGraphData,
setIsLoading,
});

return <HighchartsReact highcharts={Highcharts} options={options} />;
};
47 changes: 47 additions & 0 deletions src/hooks/useGraph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useEffect, type Dispatch, type SetStateAction } from 'react';
import type { PrefecturesList } from '@/interfaces/prefectures';
import type {
GetPopulationResponse,
PopulationGraphData,
} from '@/interfaces/population';

interface Props {
currentPrefectures: PrefecturesList;
isLoading: boolean;
populationData: GetPopulationResponse | undefined;
setGraphData: Dispatch<SetStateAction<PopulationGraphData[] | undefined>>;
setIsLoading: Dispatch<SetStateAction<boolean>>;
}

export const useGrpph = ({
currentPrefectures,
isLoading,
populationData,
setGraphData,
setIsLoading,
}: Props) => {
useEffect(() => setIsLoading(isLoading), [isLoading, setIsLoading]);

useEffect(() => {
if (!populationData) return;

const data = populationData.result;
const prefName = currentPrefectures.prefName;

setGraphData((prev) => {
if (!prev) {
return [{ result: data, prefName }];
}

const exsistData = prev.filter((item) => item.prefName === prefName);

if (exsistData.length === 0) {
return [...prev, { result: data, prefName }];
}

return prev;
});
}, [currentPrefectures, populationData, setGraphData]);

return {};
};
16 changes: 8 additions & 8 deletions src/test/container.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ describe('containerのテスト', () => {

const hokkaido = screen.getByDisplayValue('北海道');

await waitFor(async () => {
await userEvent.click(hokkaido);
});
await userEvent.click(hokkaido);

const element = screen.getByRole('heading', {
level: 2,
name: `${DisplayConditionsList['総人口']}グラフ`,
});
await waitFor(() => {
const element = screen.getByRole('heading', {
level: 2,
name: `${DisplayConditionsList['総人口']}グラフ`,
});

expect(element).toBeInTheDocument();
expect(element).toBeInTheDocument();
});
});
});