Skip to content

Commit

Permalink
show card when opening sharelink with conversationId as search parame…
Browse files Browse the repository at this point in the history
…ter (#554)
  • Loading branch information
Svenstar74 authored May 14, 2024
1 parent 694569a commit 2e35ed8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/features/auth/hooks/useAutoLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function useAutoLogin() {
}),
);

navigate(location.pathname);
navigate(location.pathname + location.search);
}
}
}, []);
Expand Down
11 changes: 7 additions & 4 deletions src/features/conversations/components/ConversationCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useRef, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { useLocation, useNavigate, useSearchParams } from 'react-router-dom';
import { Collapse, IconButton } from '@mui/material';
import EditIcon from '@mui/icons-material/Edit';
import DeleteIcon from '@mui/icons-material/Delete';
Expand All @@ -24,17 +24,20 @@ interface Props {
function ConversationCard({ conversationId, userBName, conversationState, onDeleteConversation, userARating }: Props) {
const navigate = useNavigate();
const location = useLocation();
const [searchParams, _] = useSearchParams();

const cardRef = useRef<HTMLDivElement>(null);
const { showSuccessToast } = useToastMessage();

const USER_B_NAME = capitalizeFirstLetter(userBName);
const [expanded, setExpanded] = useState(location.state?.id === conversationId);

const focusOnCard = location.state?.id === conversationId || searchParams.get('conversation') === conversationId;
const [expanded, setExpanded] = useState(focusOnCard);

useEffect(() => {
if (location.state?.id === conversationId && cardRef.current) {
if (focusOnCard && cardRef.current) {
cardRef.current.scrollIntoView({ behavior: 'smooth' });
navigate(location.pathname, { state: {} });
navigate(location.pathname + location.search, { state: {} });
}
}, []);

Expand Down
9 changes: 7 additions & 2 deletions src/pages/SharedPages/AuthorizedPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from 'react';
import { useEffect, useState } from 'react';
import { Outlet, useLocation, useNavigate } from 'react-router-dom';

import ROUTES from 'router/RouteConfig';
Expand All @@ -9,11 +9,16 @@ function AuthorizedPage() {
const location = useLocation();

const isLoggedIn = useAppSelector((state) => state.auth.userA.isLoggedIn);
const [lastLoggedIn, setLastLoggedIn] = useState(isLoggedIn);

useEffect(() => {
if (!isLoggedIn) {
if (!isLoggedIn && lastLoggedIn) {
navigate(ROUTES.HOME_PAGE);
} else if (!isLoggedIn && !lastLoggedIn) {
navigate(ROUTES.LOGIN_PAGE, { state: { from: location.pathname + location.search } });
}

setLastLoggedIn(isLoggedIn);
}, [isLoggedIn, navigate, location.pathname]);

return <>{isLoggedIn ? <Outlet /> : null}</>;
Expand Down
6 changes: 4 additions & 2 deletions src/pages/UserAAuthorizedPages/ConversationsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useEffect, useState } from 'react';
import { useLocation } from 'react-router-dom';
import { useLocation, useSearchParams } from 'react-router-dom';
import { useMediaQuery } from '@mui/material';

import { CmButton, CmTextInput, CmTypography, Page, PageContent } from 'shared/components';
import { ConversationsDrawer, CopyLinkModal, useConversationInvite } from 'features/conversations';

function ConversationsPage() {
const location = useLocation();
const [searchParams, _] = useSearchParams();

const isSmall = useMediaQuery('(max-width: 960px)');

// Logic for create link
Expand All @@ -29,7 +31,7 @@ function ConversationsPage() {
}

useEffect(() => {
if (location.state?.id) {
if (location.state?.id || searchParams.get('conversation')) {
setConversationDrawerOpen(true);
}
}, []);
Expand Down
12 changes: 10 additions & 2 deletions src/pages/UserAUnauthorizedPages/LoginPage.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useLocation, useNavigate } from 'react-router-dom';

import ROUTES from 'router/RouteConfig';
import { CmTypography, Page, PageContent } from 'shared/components';
import { LoginForm, RequestPasswordResetModal, useLogin, useResetPassword } from 'features/auth';

function LoginPage() {
const navigate = useNavigate();
const location = useLocation();

// Logic for login
const [isLoading, setIsLoading] = useState(false);
Expand All @@ -15,7 +16,14 @@ function LoginPage() {
async function handleSubmit(email: string, password: string, recaptchaToken?: string) {
setIsLoading(true);
const isSuccessful = await loginUserA(email, password, recaptchaToken);
if (isSuccessful) navigate(ROUTES.CLIMATE_FEED_PAGE);
if (isSuccessful) {
if (location.state && 'from' in location.state) {
navigate(location.state.from);
} else {
navigate(ROUTES.CLIMATE_FEED_PAGE);
}
}

setIsLoading(false);
}

Expand Down

0 comments on commit 2e35ed8

Please sign in to comment.