Skip to content

Commit

Permalink
no-issue: Fix location.state null (#5179)
Browse files Browse the repository at this point in the history
* extract useFromLocation

* extract `useFromLocation` to own file and use
  • Loading branch information
jaskfla authored Nov 15, 2023
1 parent 4026d98 commit 64259c4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
13 changes: 3 additions & 10 deletions packages/datatrak-web/src/api/mutations/useLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/

import { useMutation, useQueryClient } from 'react-query';
import { useNavigate, useLocation } from 'react-router-dom';
import { useFromLocation } from '../../utils';
import { useNavigate } from 'react-router-dom';
import { post } from '../api';
import { ROUTES } from '../../constants';

Expand All @@ -13,18 +14,10 @@ type LoginCredentials = {
password: string;
};

function hasFrom(state: unknown): state is { from: string } {
if (state !== null && typeof state === 'object' && 'from' in state) {
return typeof state.from === 'string';
}
return false;
}
export const useLogin = () => {
const queryClient = useQueryClient();
const navigate = useNavigate();
const location = useLocation();

const from = hasFrom(location.state) ? location.state.from : undefined;
const from = useFromLocation();

return useMutation<any, Error, LoginCredentials, unknown>(
({ email, password }: LoginCredentials) => {
Expand Down
1 change: 1 addition & 0 deletions packages/datatrak-web/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

export { errorToast, successToast } from './toast';
export { useDebounce } from './useDebounce';
export { useFromLocation } from './useFromLocation';
export { useIsMobile } from './useIsMobile';
export * from './date';
18 changes: 18 additions & 0 deletions packages/datatrak-web/src/utils/useFromLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Tupaia
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
*/

import { useLocation } from 'react-router-dom';

function hasFrom(state: unknown): state is { from: string } {
if (state !== null && typeof state === 'object' && 'from' in state) {
return typeof state.from === 'string';
}
return false;
}

export function useFromLocation() {
const location = useLocation();
return hasFrom(location.state) ? location.state.from : undefined;
}
8 changes: 3 additions & 5 deletions packages/datatrak-web/src/views/ProjectSelectPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import React from 'react';
import styled from 'styled-components';
import { Paper } from '@material-ui/core';
import { useNavigate, useLocation } from 'react-router-dom';
import { useNavigate } from 'react-router-dom';
import { useFromLocation } from '../utils';
import { ProjectSelectForm } from '../features';
import { ROUTES } from '../constants';

Expand All @@ -19,10 +20,7 @@ const Container = styled(Paper).attrs({

export const ProjectSelectPage = () => {
const navigate = useNavigate();
const location = useLocation();
const { from } = location.state as {
from?: string;
};
const from = useFromLocation();

const onSuccess = () => {
navigate(from || ROUTES.HOME, {
Expand Down

0 comments on commit 64259c4

Please sign in to comment.