Skip to content

Commit

Permalink
Fix #noissue : 이미 매칭된 오더 알림 팝업 계속 뜨느 문제 해결
Browse files Browse the repository at this point in the history
- 데이터가 저장되어있어 계속 남아있는 것이 문제
  • Loading branch information
hoo00nn committed Dec 10, 2020
1 parent b26c8dc commit 46418a6
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions client/src/routes/Driver/Main/Main.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { FC, useState, useCallback, useEffect } from 'react';
import React, { FC, useState, useCallback, useEffect, useRef } from 'react';
import { useSubscription } from '@apollo/client';
import { useCustomQuery, useCustomMutation } from '@hooks/useApollo';
import {
UpdateDriverLocation,
GetUnassignedOrders,
GetUnassignedOrders_getUnassignedOrders_unassignedOrders as Order,
SubNewOrder,
} from '@/types/api';
import styled from '@theme/styled';
import MapFrame from '@components/MapFrame';
Expand Down Expand Up @@ -45,11 +46,33 @@ const Main: FC = () => {
const [isModal, openModal, closeModal] = useModal();
const [orderItem, setOrderItem] = useState<Order>();
const [currentLocation, setCurrentLocation] = useState<Location>();
const orderTimerRef = useRef<NodeJS.Timeout>();
const [updateDriverLocation] = useCustomMutation<UpdateDriverLocation>(UPDATE_DRIVER_LOCATION);
const { data: addedOrder, loading } = useSubscription(SUB_NEW_ORDER, {

useSubscription<SubNewOrder>(SUB_NEW_ORDER, {
variables: { lat: currentLocation?.lat, lng: currentLocation?.lng },
onSubscriptionData: ({ subscriptionData }) => {
const { newOrder } = subscriptionData.data?.subNewOrder || {};

if (newOrder) {
openModal();
setOrderItem(newOrder);

orderTimerRef.current = setTimeout(() => {
closeModal();
}, DRIVER.NEW_ORDER_DURATION);
}
},
});

const onClickCloseModal = useCallback(() => {
if (orderTimerRef.current) {
clearTimeout(orderTimerRef.current);
orderTimerRef.current = undefined;
}
closeModal();
}, [orderTimerRef.current]);

useSubscription(UPDATE_ORDER_LIST, {
onSubscriptionData: async () => {
const newData = await callQuery();
Expand Down Expand Up @@ -92,19 +115,6 @@ const Main: FC = () => {
};
}, []);

useEffect(() => {
if (!loading && addedOrder) {
const { newOrder } = addedOrder.subNewOrder;
openModal();
setOrderItem(newOrder);

const timer = setTimeout(() => {
closeModal();
}, DRIVER.NEW_ORDER_DURATION);
return () => clearTimeout(timer);
}
}, [addedOrder]);

useEffect(() => {
setOrderData(unassignedOrders);
}, [unassignedOrders]);
Expand All @@ -128,8 +138,8 @@ const Main: FC = () => {
</StyledOrderLogList>
</MapFrame>
)}
<Modal visible={isModal} onClose={closeModal}>
{orderItem && <OrderModalItem order={orderItem} closeModal={closeModal} />}
<Modal visible={isModal} onClose={onClickCloseModal}>
{orderItem && <OrderModalItem order={orderItem} closeModal={onClickCloseModal} />}
</Modal>
</>
);
Expand Down

0 comments on commit 46418a6

Please sign in to comment.