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

fix: 사용량 통계 부분 렌더링을 개선한다 #864

Merged
merged 5 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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 frontend/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const config: StorybookConfig = {
'@constants': path.resolve(__dirname, '../src/constants'),
'@assets': path.resolve(__dirname, '../src/assets'),
'@type': path.resolve(__dirname, '../src/types'),
'@tools': path.resolve(__dirname, '../src/tools'),
};
}

Expand Down
3 changes: 2 additions & 1 deletion frontend/jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"^@constants": "<rootDir>/src/constants/index.ts",
"^@constants/(.*)$": "<rootDir>/src/constants/$1",
"^@assets/(.*)$": "<rootDir>/fileMock.ts",
"^@type/(.*)$": "<rootDir>/src/types/$1"
"^@type/(.*)$": "<rootDir>/src/types/$1",
"^@tools/(.*)$": "<rootDir>/src/tools/$1"
},
"setupFilesAfterEnv": ["<rootDir>/jest.setup.ts"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const StationDetailsView = ({ station }: StationDetailsViewProps) => {

<ChargerList chargers={chargers} stationId={stationId} reportCount={reportCount} />

<CongestionStatistics stationId={stationId} />
<CongestionStatistics stationId={stationId} chargers={chargers} />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chargers를 리액트 쿼리에서 직접 가져오는 것은 어떤가요?! 나중에 CongestionStatistics 컴포넌트를 독립적으로 사용하게 되는 경우엔 chagers를 컴포넌트 내부에서 가져오는 것이 더 좋을 것 같다는 생각이 드네요!


<ReviewPreview stationId={stationId} />
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface ChargingSpeedButtonsProps {

const ChargingSpeedButtons = ({ chargingSpeed, setChargingSpeed }: ChargingSpeedButtonsProps) => {
return (
<FlexBox nowrap mt={4} columnGap={2}>
<FlexBox nowrap columnGap={2}>
<Button
aria-label="완속 충전기 기준 시간별 사용량 보기"
variant={chargingSpeed === 'standard' ? 'contained' : 'outlined'}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getChargerCountsAndAvailability } from '@tools/getChargerCountsAndAvailability';

import { useState } from 'react';

import { useStationCongestionStatistics } from '@hooks/tanstack-query/station-details/useStationCongestionStatistics';
Expand All @@ -7,19 +9,22 @@ import Tab from '@common/Tab';

import Error from '@ui/Error';

import type { CHARGING_SPEED } from '@constants/chargers';
import { ENGLISH_DAYS_OF_WEEK, SHORT_KOREAN_DAYS_OF_WEEK } from '@constants/congestion';

import type { EnglishDaysOfWeek } from '@type';
import type { Charger, EnglishDaysOfWeek } from '@type';

import ChargingSpeedButtons from './ChargingSpeedButtons';
import StatisticsContent from './StatisticsContent';
import Title from './Title';
import BarsSkeleton from './bar/BarsSkeleton';

interface CongestionStatisticsProps {
stationId: string;
chargers: Charger[];
}

const CongestionStatistics = ({ stationId }: CongestionStatisticsProps) => {
const CongestionStatistics = ({ stationId, chargers }: CongestionStatisticsProps) => {
const todayIndex = (new Date().getDay() + 6) % 7; // 월요일 0 ~ 일요일 6

const [dayOfWeek, setDayOfWeek] = useState<EnglishDaysOfWeek>(ENGLISH_DAYS_OF_WEEK[todayIndex]);
Expand All @@ -39,6 +44,17 @@ const CongestionStatistics = ({ stationId }: CongestionStatisticsProps) => {
setDayOfWeek(ENGLISH_DAYS_OF_WEEK[index]);
};

const { standardChargerCount, quickChargerCount } = getChargerCountsAndAvailability(chargers);

const hasOnlyStandardChargers = quickChargerCount === 0;
const hasOnlyQuickChargers = standardChargerCount === 0;

const hasOnlyOneChargerType = hasOnlyStandardChargers || hasOnlyQuickChargers;

const [chargingSpeed, setChargingSpeed] = useState<keyof typeof CHARGING_SPEED>(
hasOnlyStandardChargers ? 'standard' : 'quick'
);

return (
<Box my={5}>
<Title />
Expand All @@ -63,15 +79,24 @@ const CongestionStatistics = ({ stationId }: CongestionStatisticsProps) => {
minHeight={40.4}
/>
) : (
SHORT_KOREAN_DAYS_OF_WEEK.map((day, index) => (
<Tab.Content key={`${day}-statistics-content`} index={index} width="100%">
{isLoading ? (
<BarsSkeleton />
) : (
<StatisticsContent congestionStatistics={congestionStatistics} />
)}
</Tab.Content>
))
<>
{SHORT_KOREAN_DAYS_OF_WEEK.map((day, index) => (
<Tab.Content key={`${day}-statistics-content`} index={index} width="100%">
{isLoading ? (
<BarsSkeleton />
) : (
<StatisticsContent congestion={congestionStatistics.congestion[chargingSpeed]} />
)}
</Tab.Content>
))}

{!hasOnlyOneChargerType && (
<ChargingSpeedButtons
chargingSpeed={chargingSpeed}
setChargingSpeed={setChargingSpeed}
/>
)}
</>
)}
</Tab>
</Box>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,20 @@
import { useEffect, useState } from 'react';

import FlexBox from '@common/FlexBox';

import type { CHARGING_SPEED } from '@constants/chargers';
import { NO_RATIO } from '@constants/congestion';

import type { CongestionStatistics } from '@type';
import type { Congestion } from '@type';

import ChargingSpeedButtons from './ChargingSpeedButtons';
import Bar from './bar/Bar';

interface StatisticsContentProps {
congestionStatistics: CongestionStatistics;
congestion: Congestion[];
}

const StatisticsContent = ({ congestionStatistics }: StatisticsContentProps) => {
const hasOnlyStandardChargers = congestionStatistics?.congestion['quick'].every(
(congestion) => congestion.ratio === NO_RATIO
);
const hasOnlyQuickChargers = congestionStatistics?.congestion['standard'].every(
(congestion) => congestion.ratio === NO_RATIO
);
const hasOnlyOneChargerType = hasOnlyStandardChargers || hasOnlyQuickChargers;

const [chargingSpeed, setChargingSpeed] = useState<keyof typeof CHARGING_SPEED>('standard');

useEffect(() => {
if (hasOnlyQuickChargers) {
setChargingSpeed('quick');
}
}, [hasOnlyQuickChargers]);

const StatisticsContent = ({ congestion }: StatisticsContentProps) => {
return (
<>
<FlexBox tag="ul" direction="column" nowrap alignItems="start" width="100%" height="auto">
{congestionStatistics?.congestion[chargingSpeed].map(({ hour, ratio }) => (
<Bar key={`statistics-${hour}`} hour={`${hour + 1}`.padStart(2, '0')} ratio={ratio} />
))}
</FlexBox>

{!hasOnlyOneChargerType && (
<ChargingSpeedButtons chargingSpeed={chargingSpeed} setChargingSpeed={setChargingSpeed} />
)}
</>
<FlexBox tag="ul" direction="column" nowrap alignItems="start" width="100%" height="auto">
{congestion.map(({ hour, ratio }) => (
<Bar key={`statistics-${hour}`} hour={`${hour + 1}`.padStart(2, '0')} ratio={ratio} />
))}
</FlexBox>
);
};

Expand Down
34 changes: 2 additions & 32 deletions frontend/src/components/ui/StationInfoWindow/StationInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { XMarkIcon } from '@heroicons/react/24/outline';
import { getChargerCountsAndAvailability } from '@tools/getChargerCountsAndAvailability';

import type { MouseEvent } from 'react';
import { HiChevronRight } from 'react-icons/hi2';
Expand All @@ -10,45 +11,14 @@ import Button from '@common/Button';
import FlexBox from '@common/FlexBox';
import Text from '@common/Text';

import { QUICK_CHARGER_CAPACITY_THRESHOLD } from '@constants/chargers';

import type { Charger, StationDetails } from '@type';
import type { StationDetails } from '@type';

export interface StationInfoProps {
stationDetails: StationDetails;
handleOpenStationDetail: () => void;
handleCloseStationWindow: (event: MouseEvent<HTMLButtonElement>) => void;
}

const getChargerCountsAndAvailability = (chargers: Charger[]) => {
const isAvailable = chargers.some(({ state }) => state === 'STANDBY');

const standardChargers = chargers.filter(
({ capacity }) => capacity < QUICK_CHARGER_CAPACITY_THRESHOLD
);
const quickChargers = chargers.filter(
({ capacity }) => capacity >= QUICK_CHARGER_CAPACITY_THRESHOLD
);

const availableStandardChargerCount = standardChargers.filter(
({ state }) => state === 'STANDBY'
).length;
const availableQuickChargerCount = quickChargers.filter(
({ state }) => state === 'STANDBY'
).length;

const standardChargerCount = standardChargers.length;
const quickChargerCount = quickChargers.length;

return {
isAvailable,
availableStandardChargerCount,
availableQuickChargerCount,
standardChargerCount,
quickChargerCount,
};
};

const StationInfo = ({
stationDetails,
handleOpenStationDetail,
Expand Down
32 changes: 32 additions & 0 deletions frontend/src/tools/getChargerCountsAndAvailability.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { QUICK_CHARGER_CAPACITY_THRESHOLD } from '@constants/chargers';

import type { Charger } from '@type';

export const getChargerCountsAndAvailability = (chargers: Charger[]) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 좋네요 👍

const isAvailable = chargers.some(({ state }) => state === 'STANDBY');

const standardChargers = chargers.filter(
({ capacity }) => capacity < QUICK_CHARGER_CAPACITY_THRESHOLD
);
const quickChargers = chargers.filter(
({ capacity }) => capacity >= QUICK_CHARGER_CAPACITY_THRESHOLD
);

const availableStandardChargerCount = standardChargers.filter(
({ state }) => state === 'STANDBY'
).length;
const availableQuickChargerCount = quickChargers.filter(
({ state }) => state === 'STANDBY'
).length;

const standardChargerCount = standardChargers.length;
const quickChargerCount = quickChargers.length;

return {
isAvailable,
availableStandardChargerCount,
availableQuickChargerCount,
standardChargerCount,
quickChargerCount,
};
};
3 changes: 2 additions & 1 deletion frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"@style/*": ["style/*"],
"@assets/*": ["assets/*"],
"@type": ["types/index"],
"@type/*": ["types/*"]
"@type/*": ["types/*"],
"@tools/*": ["tools/*"]
}
}
}
1 change: 1 addition & 0 deletions frontend/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ module.exports = function () {
'@assets': path.resolve(__dirname, './src/assets'),
'@style': path.resolve(__dirname, './src/style'),
'@type': path.resolve(__dirname, './src/types'),
'@tools': path.resolve(__dirname, './src/tools'),
},
},
plugins: [
Expand Down
Loading