Skip to content

Commit

Permalink
fix: non logged in users when press filter in activity view are direc…
Browse files Browse the repository at this point in the history
…ted to project
  • Loading branch information
sajald77 committed Mar 8, 2023
1 parent 6139be7 commit 282e6d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/context/filter.tsx
@@ -1,6 +1,7 @@
import { createContext, useContext, useEffect, useState } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'
import { useLocation, useMatch, useNavigate } from 'react-router-dom'

import { getPath } from '../constants'
import { disableSortByTrending } from '../pages/landing/filters/sort'
import {
ActivityResourceType,
Expand Down Expand Up @@ -43,14 +44,26 @@ const defaultSort = { createdAt: 'desc' } as SortType

export const FilterContext = createContext<FilterState>(defaultFilterContext)

export const FilterProvider = ({ children }: { children: React.ReactNode }) => {
export const FilterProvider = ({
children,
isLoggedIn,
}: {
children: React.ReactNode
isLoggedIn?: boolean
}) => {
const [filters, setFilters] = useState<FilterType>({} as FilterType)
const [sort, setSort] = useState<SortType>(defaultSort)

const isLandingFeedPage = useMatch(getPath('landingFeed'))

const location = useLocation()
const navigate = useNavigate()

const updateFilter = (value: Partial<FilterType>) => {
if (isLandingFeedPage && !isLoggedIn) {
navigate('/', { state: { save: true } })
}

setFilters({ ...filters, recent: false, ...value })
}

Expand All @@ -61,15 +74,16 @@ export const FilterProvider = ({ children }: { children: React.ReactNode }) => {
}
}
}, [filters, sort])

useEffect(() => {
if (location.state?.tagId) {
updateFilter({ tagIds: [toInt(location.state?.tagId)] })
navigate('', { state: null })
} else if (location.state?.save) {
navigate('', { state: null })
} else {
setFilters({})
}
}, [location])
}, [location.pathname])

const updateSort = (value: Partial<SortType>) => {
setSort({ ...value })
Expand Down
5 changes: 3 additions & 2 deletions src/pages/landing/LandingPage.tsx
Expand Up @@ -3,7 +3,7 @@ import { Outlet } from 'react-router-dom'

import { StickToTop } from '../../components/layouts'
import { dimensions, ID } from '../../constants'
import { FilterProvider } from '../../context'
import { FilterProvider, useAuthContext } from '../../context'
import { useMobileMode } from '../../utils'
import { GradientBanner } from './components'
import { TabBar } from './components/TabBar'
Expand All @@ -12,8 +12,9 @@ import { ProjectLeaderboard } from './projectLeaderboard'

export const LandingPage = () => {
const isMobile = useMobileMode()
const { isLoggedIn } = useAuthContext()
return (
<FilterProvider>
<FilterProvider isLoggedIn={isLoggedIn}>
<VStack
marginTop={`-${dimensions.topNavBar.desktop.height}px`}
position="relative"
Expand Down

0 comments on commit 282e6d9

Please sign in to comment.