-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearchHeader.tsx
More file actions
134 lines (120 loc) · 4.21 KB
/
SearchHeader.tsx
File metadata and controls
134 lines (120 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { MESSAGE_TYPES } from '../../config/webview';
import { useNavigate } from 'react-router-dom';
import useAuthInfo from '@hooks/useAuthInfo';
import useLocalStorageState from '@hooks/useLocalStorageState';
import useToast from '@hooks/useToast';
import ConfirmModal from '@components/Modal/Confirm/ConfirmModal';
import Toast from '@components/Toast/Toast';
import {
useAddBookmarkMutation,
useDeleteBookmarkMutation,
useStockPreferenceQuery,
useToggleNotificationMutation,
} from '@controllers/preference/query';
import { StockDetailInfo } from '@controllers/stocks/types';
import BackIcon from '@assets/icons/arrowLeft.svg?react';
import BellSVG from '@assets/icons/bell.svg?react';
import HeartIcon from '@assets/icons/heart.svg?react';
import ToastBellSVG from '@assets/icons/toast/bell.svg?react';
import ToastBellCrossSVG from '@assets/icons/toast/bell_cross.svg?react';
import ToastHeartSVG from '@assets/icons/toast/heart.svg?react';
import { IconButton, RightSection, SearchHeaderWrapper } from './SearchHeader.Style';
const SearchHeader = ({ stockInfo }: { stockInfo: StockDetailInfo }) => {
const navigate = useNavigate();
const { isLogin, handleNavigateLogin } = useAuthInfo();
const { toast, showToast, hideToast } = useToast();
const { data: stockPreference } = useStockPreferenceQuery(stockInfo.stockId);
const { mutate: addBookMark } = useAddBookmarkMutation();
const { mutate: deleteBookmark } = useDeleteBookmarkMutation();
const { mutate: toggleNotification } = useToggleNotificationMutation();
const isBookmark = stockPreference?.isBookmarked ?? false;
const isNotification = stockPreference?.isNotificationEnabled ?? false;
const [accessToken] = useLocalStorageState<string>('access_token');
const checkAndRequestNotificationPermission = async () => {
const isWebView = !!(window as any).ReactNativeWebView;
if (isWebView) {
(window as any).ReactNativeWebView.postMessage(
JSON.stringify({
type: MESSAGE_TYPES.REQUEST_NOTIFICATION_PERMISSION,
token: accessToken,
}),
);
}
};
const onHeartClick = async () => {
if (!stockInfo) return;
if (!isLogin) {
openLoginModal();
return;
}
if (!isBookmark) {
// 관심 등록 시 알림 권한 체크 및 요청
await checkAndRequestNotificationPermission();
showToast(
<>
<ToastHeartSVG />
<p>관심 등록 완료! 민심 급변 시 알림 드릴게요</p>
</>,
);
addBookMark(stockInfo.stockId);
} else {
deleteBookmark(stockInfo.stockId);
}
};
const onBellClick = async () => {
if (!stockInfo) return;
if (!isLogin) {
openLoginModal();
return;
}
if (isNotification) {
showToast(
<>
<ToastBellCrossSVG />
<p>알림이 해제되었어요</p>
</>,
);
} else {
await checkAndRequestNotificationPermission();
showToast(
<>
<ToastBellSVG />
<p>알림이 설정되었어요</p>
</>,
);
}
toggleNotification(stockInfo.stockId);
};
const handleLoginWithState = () => {
handleNavigateLogin({
returnState: {
symbolName: stockInfo.symbolName,
country: stockInfo.country,
},
});
};
const [LoginModal, openLoginModal] = ConfirmModal({
title: '관심종목 알림을 받으려면, 로그인이 필요해요!',
description: '관심종목의 심리가 급등/급락할때 알림을 받고싶다면, 로그인을 진행해주세요',
onConfirm: handleLoginWithState,
isInverse: true,
actionText: ['로그인하기', '취소'],
});
return (
<SearchHeaderWrapper>
<LoginModal />
<BackIcon onClick={() => navigate(-1)} />
<RightSection>
<IconButton isActive={isBookmark}>
<HeartIcon onClick={onHeartClick} />
</IconButton>
<IconButton isActive={isNotification}>
<BellSVG onClick={onBellClick} />
</IconButton>
</RightSection>
<Toast toast={toast} hideToast={hideToast} />
{/* {toast.enabled && <SearchHeaderToast closing={toast.closing}>{toast.message}</SearchHeaderToast>} */}
</SearchHeaderWrapper>
);
};
export default SearchHeader;