Skip to content

Commit

Permalink
fix: issue where error is displayed after resetting password
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Mar 17, 2024
1 parent bc21347 commit f2f3881
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 60 deletions.
5 changes: 5 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const SearchPage = lazy(() => import('./pages/SearchPage'));
const LoginPage = lazy(() => import('./pages/LoginPage'));
const NewPasswordPage = lazy(() => import('./pages/NewPasswordPage'));
const DownloadsPage = lazy(() => import('./pages/DownloadsPage'));
const ForgotPasswordPage = lazy(() => import('./pages/ForgotPasswordPage'));

function App() {
const [cookies, setCookie] = useCookies(['token']);
Expand Down Expand Up @@ -76,6 +77,10 @@ function App() {
path="/login"
element={<LoginPage setErrorMessage={handledError} />}
/>
<Route
path="/forgot"
element={<ForgotPasswordPage setErrorMessage={handledError} />}
/>
<Route
path="/users/r/:id"
element={<NewPasswordPage setErrorMessage={handledError} />}
Expand Down
23 changes: 23 additions & 0 deletions src/components/forms/ForgotPasswordForm/ForgotPassword.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
interface ForgotPasswordProp {
didReset: boolean;
loading: boolean;
}

export function ForgotPassword({ didReset, loading }: ForgotPasswordProp) {
if (didReset) {
return <p>You should receive an email if your account exists.</p>;
}

return <div className="field">
<div className="control" style={{ width: '100%' }}>
<button
type="submit"
className="button is-link is-medium"
style={{ width: '100%' }}
disabled={loading}
>
Reset my password
</button>
</div>
</div>;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import styled from 'styled-components';
import { SyntheticEvent, useState } from 'react';
import Backend from '../../lib/backend';
import { ErrorHandlerType } from '../errors/helpers/getErrorMessage';

const FormContainer = styled.div`
max-width: 720px;
margin: 0 auto;
`;
import Backend from '../../../lib/backend';
import { ErrorHandlerType } from '../../errors/helpers/getErrorMessage';
import { ForgotPassword } from './ForgotPassword';
import { FormContainer } from './styled';

interface ForgotPasswordProps {
setError: ErrorHandlerType;
Expand All @@ -19,7 +16,6 @@ function ForgotPasswordForm({ setError }: ForgotPasswordProps) {

const handleSubmit = async (event: SyntheticEvent) => {
event.preventDefault();
setError(null);
setLoading(true);
setDidReset(false);

Expand Down Expand Up @@ -61,23 +57,7 @@ function ForgotPasswordForm({ setError }: ForgotPasswordProps) {
/>
</label>
</div>
{!didReset && (
<div className="field">
<div className="control" style={{ width: '100%' }}>
<button
type="submit"
className="button is-link is-medium"
style={{ width: '100%' }}
disabled={loading}
>
Reset my password
</button>
</div>
</div>
)}
{didReset && (
<p>You should receive an email if your account exists.</p>
)}
<ForgotPassword didReset={didReset} loading={loading} />
</form>
</div>
</div>
Expand Down
7 changes: 7 additions & 0 deletions src/components/forms/ForgotPasswordForm/styled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import styled from 'styled-components';

export const FormContainer = styled.div`
max-width: 720px;
margin: 0 auto;
`;

21 changes: 4 additions & 17 deletions src/components/forms/LoginForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import { useHandleLoginSubmit } from './helpers/useHandleLoginSubmit';
import { FormContainer, SubmitButton } from './styled';

interface LoginFormProps {
onForgotPassword: () => void;
onError: ErrorHandlerType;
}

function LoginForm({ onForgotPassword, onError }: LoginFormProps) {
function LoginForm({ onError }: LoginFormProps) {
const { email, password, loading, onSubmit, setEmail, setPassword } =
useHandleLoginSubmit(onError);

Expand Down Expand Up @@ -60,21 +59,9 @@ function LoginForm({ onForgotPassword, onError }: LoginFormProps) {
</label>
</div>

<div
tabIndex={-9}
role="button"
className="field"
onClick={() => onForgotPassword()}
onKeyDown={(event) => {
if (event.key === 'F9') {
onForgotPassword();
}
}}
>
<a rel="noreferrer" href="#forgot">
I forgot my password
</a>
</div>
<a rel="noreferrer" href="/forgot">
I forgot my password
</a>

<div className="field">
<div className="control">
Expand Down
5 changes: 2 additions & 3 deletions src/components/forms/NewPasswordForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Backend from '../../lib/backend';
import { ErrorHandlerType } from '../errors/helpers/getErrorMessage';

const FormContainer = styled.div`
max-width: 720px;
margin: 0 auto;
max-width: 720px;
margin: 0 auto;
`;

interface Props {
Expand All @@ -22,7 +22,6 @@ function NewPasswordForm({ setErrorMessage }: Props) {

const handleSubmit = async (event: SyntheticEvent) => {
event.preventDefault();
setErrorMessage('');
setLoading(true);

try {
Expand Down
5 changes: 2 additions & 3 deletions src/components/forms/RegisterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import Backend from '../../lib/backend';
import { ErrorHandlerType } from '../errors/helpers/getErrorMessage';

const FormContainer = styled.div`
max-width: 720px;
margin: 0 auto;
max-width: 720px;
margin: 0 auto;
`;

interface Props {
Expand All @@ -31,7 +31,6 @@ function RegisterForm({ setErrorMessage }: Props) {

const handleSubmit = async (event: SyntheticEvent) => {
event.preventDefault();
setErrorMessage('');
setLoading(true);

try {
Expand Down
4 changes: 3 additions & 1 deletion src/components/shared/canShowNavbar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const isLoginPage = (path: string) => path.includes('/login');
const isForgotPasswordPage = (path: string) => path.includes('/forgot');

export const isSimplePage = () => window.location.pathname.endsWith('simple');

export const canShowNavbar = (path: string) =>
!isLoginPage(path) && !isSimplePage();
!isLoginPage(path) && !isSimplePage() && !isForgotPasswordPage(path);
24 changes: 24 additions & 0 deletions src/pages/ForgotPasswordPage/ForgotPasswordPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import ForgotPasswordForm from '../../components/forms/ForgotPasswordForm/ForgotPasswordForm';
import NavButtonCTA from '../../components/buttons/NavButtonCTA';
import { Container } from '../../components/styled';
import TopSection from './TopSection';
import { ErrorHandlerType } from '../../components/errors/helpers/getErrorMessage';

interface Props {
setErrorMessage: ErrorHandlerType;
}

export function ForgotPasswordPage({ setErrorMessage }: Props) {
const onClickRegister = () => {
window.location.href = '/register';
};
return (
<Container>
<TopSection onClick={onClickRegister}>
Don&apos;t have an account?
<NavButtonCTA href="/register">Register</NavButtonCTA>
</TopSection>
<ForgotPasswordForm setError={setErrorMessage} />
</Container>
);
}
11 changes: 11 additions & 0 deletions src/pages/ForgotPasswordPage/TopSection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import styled from 'styled-components';

const TopSection = styled.div`
display: flex;
align-items: center;
justify-content: flex-end;
grid-gap: 1rem;
padding: 1rem;
`;

export default TopSection;
3 changes: 3 additions & 0 deletions src/pages/ForgotPasswordPage/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ForgotPasswordPage } from './ForgotPasswordPage';

export default ForgotPasswordPage;
12 changes: 1 addition & 11 deletions src/pages/LoginPage/LoginPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { useState } from 'react';
import LoginForm from '../../components/forms/LoginForm';
import ForgotPasswordForm from '../../components/forms/ForgotPasswordForm';
import NavButtonCTA from '../../components/buttons/NavButtonCTA';
import { Container } from '../../components/styled';
import TopSection from './TopSection';
Expand All @@ -11,24 +9,16 @@ interface Props {
}

export function LoginPage({ setErrorMessage }: Props) {
const [isForgot, setIsForgot] = useState(window.location.hash === '#forgot');
const onClickRegister = () => {
window.location.href = '/register';
};
const login = (
<LoginForm
onForgotPassword={() => setIsForgot(true)}
onError={setErrorMessage}
/>
);
return (
<Container>
<TopSection onClick={onClickRegister}>
Don&apos;t have an account?
<NavButtonCTA href="/register">Register</NavButtonCTA>
</TopSection>
{!isForgot && login}
{isForgot && <ForgotPasswordForm setError={setErrorMessage} />}
<LoginForm onError={setErrorMessage} />
</Container>
);
}

0 comments on commit f2f3881

Please sign in to comment.