Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Go back from settings to location / edit location / review / map
Browse files Browse the repository at this point in the history
wbazant committed Oct 27, 2024
1 parent d1fc4fd commit 07ac56f
Showing 4 changed files with 57 additions and 6 deletions.
19 changes: 19 additions & 0 deletions src/components/connect/DisconnectReview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'

import { clearReview } from '../../redux/reviewSlice'

const DisconnectReview = () => {
const dispatch = useDispatch()
const { review } = useSelector((state) => state.review)

useEffect(() => {
if (review) {
dispatch(clearReview())
}
}, [dispatch, review?.id])

Check warning on line 14 in src/components/connect/DisconnectReview.js

GitHub Actions / build

React Hook useEffect has a missing dependency: 'review'. Either include it or remove the dependency array

return null
}

export default DisconnectReview
11 changes: 11 additions & 0 deletions src/components/connect/connectRoutes.js
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import ConnectPath from './ConnectPath'
import ConnectReview from './ConnectReview'
import ConnectTypes from './ConnectTypes'
import DisconnectLocation from './DisconnectLocation'
import DisconnectReview from './DisconnectReview'

const connectRoutes = [
/*
@@ -131,6 +132,16 @@ const connectRoutes = [
<Route key="disconnect-location" path={['/map']}>
{({ match }) => match && <DisconnectLocation />}
</Route>,

/*
* DisconnectReview
* why: if we start editing the review and then go back to location or to the map, and then open settings, the back button should not take us to the abandoned review
*
* action: clear review in Redux
*/
<Route key="disconnect-review" path={['/map', '/locations']}>
{({ match }) => match && <DisconnectReview />}
</Route>,
]

export default connectRoutes
24 changes: 19 additions & 5 deletions src/components/desktop/SidePane.js
Original file line number Diff line number Diff line change
@@ -46,15 +46,14 @@ const SidePane = () => {
const history = useAppHistory()
const { t } = useTranslation()
const { review } = useSelector((state) => state.review)
const { locationId, isBeingEdited: isEditingLocation } = useSelector(
(state) => state.location,
)

const goToMap = (event) => {
event.stopPropagation()
history.push('/map')
}
const goBack = (event) => {
event.stopPropagation()
history.goBack()
}

return (
<FullHeightPane>
@@ -110,7 +109,22 @@ const SidePane = () => {
</Route>
<Route path="/settings">
<StyledNavBack>
<BackButton onClick={goBack}>
<BackButton
onClick={(event) => {
event.stopPropagation()
if (review) {
history.push(`/reviews/${review.id}/edit`)
} else if (locationId) {
if (isEditingLocation) {
history.push(`/locations/${locationId}/edit`)
} else {
history.push(`/locations/${locationId}`)
}
} else {
history.push('/map')
}
}}
>
<ArrowBack />
{t('back')}
</BackButton>
9 changes: 8 additions & 1 deletion src/redux/reviewSlice.js
Original file line number Diff line number Diff line change
@@ -14,10 +14,15 @@ export const fetchReviewData = createAsyncThunk(
const reviewSlice = createSlice({
name: 'review',
initialState: {
reviewId: null,
isLoading: false,
review: null,
},
reducers: {
clearReview: (state) => {
state.isLoading = false
state.review = null
},
},
extraReducers: {
[fetchReviewData.pending]: (state) => {
state.isLoading = true
@@ -35,4 +40,6 @@ const reviewSlice = createSlice({
},
})

export const { clearReview } = reviewSlice.actions

export default reviewSlice.reducer

0 comments on commit 07ac56f

Please sign in to comment.