1- import { useEffect , useState } from 'react' ;
1+ import React , { useEffect , useState } from 'react' ;
22import { useNavigate } from 'react-router-dom' ;
33import useAxios from '../../hooks/useAxios' ;
44import styled from 'styled-components' ;
@@ -22,7 +22,9 @@ function Member() {
2222 const [ isMyPage , setIsMyPage ] = useState ( false ) ;
2323
2424 // title이랑 엮여 있어서 여기에 놔둠. 매대의 붕어빵 갯수 관련
25- const [ fishSizeAll , setFishSizeAll ] = useState ( ) ;
25+ const [ fishSizeAll , setFishSizeAll ] = useState ( '?' ) ;
26+ const [ fishSizeUnread , setFishSizeUnread ] = useState ( ) ;
27+ const [ fishSizeMyUnread , setFishSizeMyUnread ] = useState ( ) ;
2628 const [ displayFishImage , setDisplayFishImage ] = useState ( 'cat_truck_0.png' ) ;
2729
2830 const memeberCheck = ( isLogin ) => {
@@ -56,44 +58,76 @@ function Member() {
5658 // console.log('토큰이 만료 되었습니다.');
5759 return false ;
5860 } else {
59- // console.log(`로그인 상태입니다. ${user.nickname}`);
61+ // console.log(`로그인 상태입니다. ${user? .nickname}`);
6062 return true ;
6163 }
6264 } ;
6365
64- async function getFishSizeAll ( ) {
65- const response = await requestApi ( 'get' , `count/${ 'U184ce3ac09d0003' } ` ) ;
66- try {
67- console . log ( response ) ;
68- if ( response . status === 200 ) {
69- // setFishSizeAll()
66+ // this를 쓰고 싶었음
67+ const fish = {
68+ api : '/uuid' ,
69+
70+ fetchCount : async function ( ) {
71+ try {
72+ const response = await requestApi ( 'get' , '/fishbread/' + this . api ) ;
73+ return response . status === 200 ? response . data : '' ;
74+ } catch ( error ) {
75+ console . log ( error . code ) ;
7076 }
71- } catch ( error ) {
72- console . log ( error . code ) ;
73- } finally {
74- }
75- }
77+ } ,
78+ } ;
7679
7780 // 붕어빵 갯수 보여주기
78- let countUp = 2 ; // sizeAll 받아오면 이거 삭제
79- const matchCatTruckImage = ( countUp ) => {
80- if ( countUp < 6 ) {
81- setDisplayFishImage ( countFishTruckImages . default [ countUp ] . imageURL ) ;
81+ const matchCatTruckImage = ( fishCount ) => {
82+ if ( fishCount < 6 ) {
83+ setDisplayFishImage ( countFishTruckImages . default [ fishCount ] . imageURL ) ;
8284 } else {
8385 // 이 부분 교체
8486 setDisplayFishImage ( 'cat_truck_6.png' ) ;
8587 }
86- setFishSizeAll ( countUp ) ;
8788 } ;
8889
90+ async function fetchFishCountHandler ( ) {
91+ if ( isMyPage ) {
92+ // 내 페이지에서 붕어빵 갯수값이 없으면 요청
93+ if ( isNaN ( fishSizeAll ) ) {
94+ const fetchedCount = await fish . fetchCount . call ( { api : `${ user ?. uuid } /total` } ) ;
95+ setFishSizeAll ( fetchedCount ) ;
96+ }
97+ if ( ! fishSizeMyUnread ) {
98+ const fetchedCount = await fish . fetchCount . call ( { api : `${ user ?. uuid } /myunread` } ) ;
99+ setFishSizeMyUnread ( fetchedCount ) ;
100+ }
101+ }
102+ if ( ! fishSizeUnread ) {
103+ const fetchedCount = await fish . fetchCount . call ( { api : `${ pageUuid } /unread` } ) ;
104+ setFishSizeUnread ( fetchedCount ) ;
105+ }
106+
107+ // api response 통합하면 여기로 값 return해서 쓰기
108+ // return {}
109+ }
110+
89111 useEffect ( ( ) => {
90- // getFishSizeAll();
112+ const fishes = fetchFishCountHandler ( ) ;
113+ if ( fishes . unread ) matchCatTruckImage ( fishes . unread ) ;
91114
92- matchCatTruckImage ( countUp ) ;
93115 const isLogin = userTokenHandler ( user , logout ) ;
94116 setIsLoggedUser ( isLogin ) ;
95117
96118 if ( isLogin ) memeberCheck ( isLogin ) ;
119+
120+ const preventGoBack = ( ) => {
121+ // change start
122+ history . pushState ( null , '' , location . href ) ;
123+ // change end
124+ console . log ( 'prevent go back!' ) ;
125+ } ;
126+
127+ history . pushState ( null , '' , location . href ) ;
128+ window . addEventListener ( 'popstate' , preventGoBack ) ;
129+
130+ return ( ) => window . removeEventListener ( 'popstate' , preventGoBack ) ;
97131 } , [ location , matchCatTruckImage , userTokenHandler , setIsLoggedUser ] ) ;
98132
99133 return (
@@ -115,6 +149,8 @@ function Member() {
115149 isMyPage = { isMyPage }
116150 displayFishImage = { displayFishImage }
117151 pageUuid = { pageUuid }
152+ fishSizeUnread = { fishSizeUnread }
153+ fishSizeMyUnread = { fishSizeMyUnread }
118154 />
119155
120156 { /* 로그인 여부에 따라 바뀌는 버튼 */ }
0 commit comments