diff --git a/frontend/src/components/google-maps/map/CarFfeineMap.tsx b/frontend/src/components/google-maps/map/CarFfeineMap.tsx index 3cd6442ad..1877eadf9 100644 --- a/frontend/src/components/google-maps/map/CarFfeineMap.tsx +++ b/frontend/src/components/google-maps/map/CarFfeineMap.tsx @@ -10,7 +10,6 @@ import { setLocalStorage } from '@utils/storage'; import { getGoogleMapStore } from '@stores/google-maps/googleMapStore'; import { toastActions } from '@stores/layout/toastStore'; -import { memberTokenStore } from '@stores/login/memberTokenStore'; import { serverStationFilterAction } from '@stores/station-filters/serverStationFiltersStore'; import { useMemberFilters } from '@hooks/tanstack-query/station-filters/useMemberFilters'; @@ -73,18 +72,11 @@ const CarFfeineMapListener = () => { return <>; }; -// TODO: 유저 필터링이 로그인 된 이후 바로 적용되지 않는 문제 다시 발생. 이 부분 어떻게 수정할지 고민해보기 const UserFilterListener = () => { const queryClient = useQueryClient(); const { data: memberFilters } = useMemberFilters(); const { setAllServerStationFilters } = serverStationFilterAction; - console.log('현재 로그인한 유저가 등록한 필터 정보', memberFilters); - console.log( - '클라이언트 전역 상태에 저장된 필터 정보', - serverStationFilterAction.getAllServerStationFilters() - ); - if (memberFilters !== undefined) { setAllServerStationFilters(memberFilters); queryClient.invalidateQueries([{ queryKey: [QUERY_KEY_STATIONS] }]); diff --git a/frontend/src/components/login/GoogleLogin.tsx b/frontend/src/components/login/GoogleLogin.tsx index fd1ef319c..c8daf092d 100644 --- a/frontend/src/components/login/GoogleLogin.tsx +++ b/frontend/src/components/login/GoogleLogin.tsx @@ -1,13 +1,15 @@ import { useEffect, useState } from 'react'; +import { fetchUtils } from '@utils/fetch'; import { getMemberToken } from '@utils/login'; import { getAPIEndPoint } from '@utils/login/index'; import { setSessionStorage } from '@utils/storage'; -import type { MemberInfo } from '@stores/login/memberInfoStore'; +import { memberTokenStore } from '@stores/login/memberTokenStore'; import ButtonNext from '@common/ButtonNext'; import FlexBox from '@common/FlexBox'; +import Loading from '@common/Loading'; import Text from '@common/Text'; import LogoIcon from '@ui/Svg/LogoIcon'; @@ -24,12 +26,9 @@ const GoogleLogin = () => { getMemberToken(code, 'google') .then(async (token) => { - const memberInfo = await fetch(`${APIEndPoint}/members/me`, { - method: 'GET', - headers: { - Authorization: `Bearer ${token}`, - }, - }).then((response) => response.json()); + memberTokenStore.setState(token); + + const memberInfo = await fetchUtils.get(`${APIEndPoint}/members/me`); setSessionStorage(SESSION_KEY_MEMBER_TOKEN, token); setSessionStorage(SESSION_KEY_MEMBER_INFO, JSON.stringify(memberInfo)); @@ -62,12 +61,7 @@ const GoogleLogin = () => { ); } - return ( - - - 로딩중... - - ); + return ; }; export default GoogleLogin; diff --git a/frontend/src/components/ui/ServerStationFilters/ServerStationFilters.tsx b/frontend/src/components/ui/ServerStationFilters/ServerStationFilters.tsx index 4356d2104..8d71ce765 100644 --- a/frontend/src/components/ui/ServerStationFilters/ServerStationFilters.tsx +++ b/frontend/src/components/ui/ServerStationFilters/ServerStationFilters.tsx @@ -1,17 +1,11 @@ import { ArrowLeftIcon, ArrowPathIcon } from '@heroicons/react/24/outline'; import { css } from 'styled-components'; -import { useQueryClient } from '@tanstack/react-query'; - -import { getAPIEndPoint } from '@utils/login'; - -import { toastActions } from '@stores/layout/toastStore'; -import { memberInfoStore } from '@stores/login/memberInfoStore'; import { memberTokenStore } from '@stores/login/memberTokenStore'; import { serverStationFilterAction } from '@stores/station-filters/serverStationFiltersStore'; import { useServerStationFilters } from '@hooks/tanstack-query/station-filters/useServerStationFilters'; -import { useServerStationFilterActions } from '@hooks/useServerStationFilterActions'; +import { useServerStationFilterStoreActions } from '@hooks/useServerStationFilterActions'; import Button from '@common/Button'; import ButtonNext from '@common/ButtonNext'; @@ -22,18 +16,16 @@ import ServerStationFiltersSkeleton from '@ui/ServerStationFilters/ServerStation import { useNavigationBar } from '@ui/compound/NavigationBar/hooks/useNavigationBar'; import { CONNECTOR_TYPES, COMPANIES } from '@constants/chargers'; -import { QUERY_KEY_STATIONS, QUERY_KEY_MEMBER_SELECTED_FILTERS } from '@constants/queryKeys'; -import type { Capacity, StationFilters } from '@type'; +import type { Capacity } from '@type'; import FilterSection from './FilterOption'; +import { useServerStationFiltersComponentActions } from './hooks/useServerStationFiltersComponentActions'; const ServerStationFilters = () => { - const queryClient = useQueryClient(); - const { showToast } = toastActions; const { data: serverStationFilters, isLoading } = useServerStationFilters(); const { closeBasePanel } = useNavigationBar(); - + const { handleStationsRefetch, submitMemberFilters } = useServerStationFiltersComponentActions(); const { toggleCapacityFilter, toggleConnectorTypeFilter, @@ -42,7 +34,7 @@ const ServerStationFilters = () => { getIsConnectorTypeSelected, getIsCompanySelected, resetAllFilters, - } = useServerStationFilterActions(); + } = useServerStationFilterStoreActions(); if (isLoading) { return ; @@ -51,60 +43,14 @@ const ServerStationFilters = () => { const { connectorTypes, capacities, companies } = serverStationFilters; const handleApplySelectedFilters = async () => { - const APIEndPoint = getAPIEndPoint(); - const memberId = memberInfoStore.getState()?.memberId; - const memberToken = memberTokenStore.getState(); - - const { getAllServerStationFilters, setAllServerStationFilters, resetAllServerStationFilters } = - serverStationFilterAction; - const selectedFilters = getAllServerStationFilters(); - const { capacities, companies, connectorTypes } = selectedFilters; - - if (memberId === undefined) { - queryClient.invalidateQueries({ queryKey: [QUERY_KEY_STATIONS] }); - showToast('필터가 적용되었습니다'); + const isLoggedInUser = memberTokenStore.getState() === ''; + + if (isLoggedInUser !== true) { + handleStationsRefetch(); return; } - try { - const stationFilters = await fetch(`${APIEndPoint}/members/${memberId}/filters`, { - method: 'POST', - headers: { - Authorization: `Bearer ${memberToken}`, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - filters: [ - ...capacities.map((capacity) => ({ - type: 'capacity', - name: capacity, - })), - ...companies.map((company) => ({ - type: 'company', - name: company, - })), - ...connectorTypes.map((connectorType) => ({ - type: 'connectorType', - name: connectorType, - })), - ], - }), - }).then((response) => response.json()); - - setAllServerStationFilters(stationFilters); - queryClient.invalidateQueries({ queryKey: [QUERY_KEY_STATIONS] }); - - showToast('필터가 적용되었습니다'); - } catch { - const stationFilters = queryClient.getQueryData([ - QUERY_KEY_MEMBER_SELECTED_FILTERS, - ]); - resetAllServerStationFilters(stationFilters); - - queryClient.invalidateQueries({ queryKey: [QUERY_KEY_STATIONS] }); - - showToast('필터 적용에 실패했습니다', 'error'); - } + submitMemberFilters(); }; return ( diff --git a/frontend/src/components/ui/ServerStationFilters/hooks/useServerStationFiltersComponentActions.ts b/frontend/src/components/ui/ServerStationFilters/hooks/useServerStationFiltersComponentActions.ts new file mode 100644 index 000000000..9525bde33 --- /dev/null +++ b/frontend/src/components/ui/ServerStationFilters/hooks/useServerStationFiltersComponentActions.ts @@ -0,0 +1,67 @@ +import { useQueryClient } from '@tanstack/react-query'; + +import { fetchUtils } from '@utils/fetch'; +import { getAPIEndPoint } from '@utils/login'; + +import { toastActions } from '@stores/layout/toastStore'; +import { memberInfoStore } from '@stores/login/memberInfoStore'; +import { serverStationFilterAction } from '@stores/station-filters/serverStationFiltersStore'; + +import { QUERY_KEY_MEMBER_SELECTED_FILTERS, QUERY_KEY_STATIONS } from '@constants/queryKeys'; + +import type { StationFilters } from '@type'; + +export const useServerStationFiltersComponentActions = () => { + const queryClient = useQueryClient(); + const { showToast } = toastActions; + + const fallbackToPreviousFilters = () => { + const { resetAllServerStationFilters } = serverStationFilterAction; + const stationFilters = queryClient.getQueryData([ + QUERY_KEY_MEMBER_SELECTED_FILTERS, + ]); + resetAllServerStationFilters(stationFilters); + + queryClient.invalidateQueries({ queryKey: [QUERY_KEY_STATIONS] }); + + showToast('필터 적용에 실패했습니다', 'error'); + }; + + const handleStationsRefetch = () => { + queryClient.invalidateQueries({ queryKey: [QUERY_KEY_STATIONS] }); + showToast('필터가 적용되었습니다'); + }; + + const applyMemberFilters = async () => { + const APIEndPoint = getAPIEndPoint(); + const memberId = memberInfoStore.getState()?.memberId; + + const { getMemberFilterRequestBody } = serverStationFilterAction; + const memberFilterRequestBody = getMemberFilterRequestBody(); + + return await fetchUtils.post( + `${APIEndPoint}/members/${memberId}/filters`, + memberFilterRequestBody + ); + }; + + const submitMemberFilters = async () => { + const { setAllServerStationFilters } = serverStationFilterAction; + + try { + const stationFilters = await applyMemberFilters(); + + setAllServerStationFilters(stationFilters); + handleStationsRefetch(); + } catch { + fallbackToPreviousFilters(); + } + }; + + return { + fallbackToPreviousFilters, + handleStationsRefetch, + applyMemberFilters, + submitMemberFilters, + }; +}; diff --git a/frontend/src/components/ui/StationDetailsWindow/chargers/ChargerList.tsx b/frontend/src/components/ui/StationDetailsWindow/chargers/ChargerList.tsx index 76ab32df9..2b70e316c 100644 --- a/frontend/src/components/ui/StationDetailsWindow/chargers/ChargerList.tsx +++ b/frontend/src/components/ui/StationDetailsWindow/chargers/ChargerList.tsx @@ -27,6 +27,10 @@ const ChargerList = ({ chargers, stationId, reportCount }: ChargerListProps) => const availableChargersSize = chargers.filter((charger) => charger.state === 'STANDBY').length; const loadedChargersSize = page * CHARGER_SIZE; + const handleShowMoreChargers = () => { + setPage((prev) => prev + 1); + }; + return ( <> @@ -51,7 +55,7 @@ const ChargerList = ({ chargers, stationId, reportCount }: ChargerListProps) => ))} {totalChargersSize - loadedChargersSize > 0 && ( -