Skip to content

Commit

Permalink
KL-176/refactor: separate location state initialization into custom hook
Browse files Browse the repository at this point in the history
  • Loading branch information
seoulyego committed Oct 16, 2024
1 parent ebe5eb2 commit 2714b70
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
25 changes: 25 additions & 0 deletions src/hooks/useInitializeLocationState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useEffect } from 'react'
import { useLocation } from 'react-router-dom'
import useFeedStore from '@stores/useFeedStore'

const useInitializeLocationState = () => {
const location = useLocation()
const resetSelectedField = useFeedStore((state) => state.resetSelectedField)

useEffect(() => {
const deleteDataState = (state) => {
if (!state?.usr) return state
const newState = { ...state, usr: { ...state.usr } }
if ('data' in newState.usr) delete newState.usr.data
return newState
}
const { history } = window
if (history.state) {
const newState = deleteDataState(history.state)
if (newState) history.replaceState(newState, '')
}
return resetSelectedField
}, [location.state])
}

export default useInitializeLocationState
24 changes: 3 additions & 21 deletions src/pages/FeedPage/FeedPage.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { useEffect } from 'react'
import { useLocation } from 'react-router-dom'
import useFeedStore from '@stores/useFeedStore'
import React from 'react'
import useInitializeState from '@hooks/useInitializeState'
import useNavIndex from '@hooks/useNavIndex'
import useInitializeLocationState from '@hooks/useInitializeLocationState'
import Thumbnail from './components/Thumbnail/Thumbnail'
import BasicFilter from './components/BasicFilter/BasicFilter'
import AdditionalFilter from './components/AdditionalFilter/AdditionalFilter'
Expand All @@ -13,24 +12,7 @@ import { FeedPageLayout, FeedPageContent, FeedArea } from './FeedPage.style'
function FeedPage() {
useInitializeState()
useNavIndex('FEED')

const location = useLocation()
const resetSelectedField = useFeedStore((state) => state.resetSelectedField)

useEffect(() => {
const deleteDataState = (state) => {
if (!state?.usr) return state
const newState = { ...state, usr: { ...state.usr } }
if ('data' in newState.usr) delete newState.usr.data
return newState
}
const { history } = window
if (history.state) {
const newState = deleteDataState(history.state)
if (newState) history.replaceState(newState, '')
}
return resetSelectedField
}, [location.state])
useInitializeLocationState()

return (
<FeedPageLayout>
Expand Down

0 comments on commit 2714b70

Please sign in to comment.