From 282e6d98ecf5065471c343cb337df9f6bd8f94f2 Mon Sep 17 00:00:00 2001 From: sajald77 Date: Wed, 8 Mar 2023 18:53:30 -0500 Subject: [PATCH] fix: non logged in users when press filter in activity view are directed to project --- src/context/filter.tsx | 22 ++++++++++++++++++---- src/pages/landing/LandingPage.tsx | 5 +++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/context/filter.tsx b/src/context/filter.tsx index ba02dc5b0..3fa1c0639 100644 --- a/src/context/filter.tsx +++ b/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, @@ -43,14 +44,26 @@ const defaultSort = { createdAt: 'desc' } as SortType export const FilterContext = createContext(defaultFilterContext) -export const FilterProvider = ({ children }: { children: React.ReactNode }) => { +export const FilterProvider = ({ + children, + isLoggedIn, +}: { + children: React.ReactNode + isLoggedIn?: boolean +}) => { const [filters, setFilters] = useState({} as FilterType) const [sort, setSort] = useState(defaultSort) + const isLandingFeedPage = useMatch(getPath('landingFeed')) + const location = useLocation() const navigate = useNavigate() const updateFilter = (value: Partial) => { + if (isLandingFeedPage && !isLoggedIn) { + navigate('/', { state: { save: true } }) + } + setFilters({ ...filters, recent: false, ...value }) } @@ -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) => { setSort({ ...value }) diff --git a/src/pages/landing/LandingPage.tsx b/src/pages/landing/LandingPage.tsx index e146e8743..a4b6b91a6 100644 --- a/src/pages/landing/LandingPage.tsx +++ b/src/pages/landing/LandingPage.tsx @@ -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' @@ -12,8 +12,9 @@ import { ProjectLeaderboard } from './projectLeaderboard' export const LandingPage = () => { const isMobile = useMobileMode() + const { isLoggedIn } = useAuthContext() return ( - +