From e4b664a69754fee3ce73137a01aec0fa3c15a9ca Mon Sep 17 00:00:00 2001 From: johnthagen Date: Fri, 29 Jan 2021 09:39:15 -0500 Subject: [PATCH] Update signature of getCurrentUser() to return undefined instead of null --- src/backend/localStorage.tsx | 4 ++-- src/backend/tests/localStorage.test.tsx | 2 +- .../GoalTimeline/tests/GoalTimelineActions.test.tsx | 2 +- src/components/Login/AuthHeaders.tsx | 3 +-- src/components/Login/LoginActions.tsx | 2 +- src/components/Login/tests/AuthHeaders.test.tsx | 2 +- src/components/UserSettings/UserSettings.tsx | 4 ++-- .../tests/CharacterInventoryActions.test.tsx | 2 +- 8 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/backend/localStorage.tsx b/src/backend/localStorage.tsx index c9e8ac52b6..3bb2a8798e 100644 --- a/src/backend/localStorage.tsx +++ b/src/backend/localStorage.tsx @@ -24,9 +24,9 @@ export function setAvatar(src: string) { localStorage.setItem(LocalStorageKey.Avatar, src); } -export function getCurrentUser(): User | null { +export function getCurrentUser(): User | undefined { const userString = localStorage.getItem(LocalStorageKey.User); - return userString ? JSON.parse(userString) : null; + return userString ? JSON.parse(userString) : undefined; } export function setCurrentUser(user: User) { const userString = JSON.stringify(user); diff --git a/src/backend/tests/localStorage.test.tsx b/src/backend/tests/localStorage.test.tsx index da35654e26..d587b80c8e 100644 --- a/src/backend/tests/localStorage.test.tsx +++ b/src/backend/tests/localStorage.test.tsx @@ -38,7 +38,7 @@ afterAll(() => { function expectAllEmpty() { expect(LocalStorage.getAvatar()).toEqual(""); - expect(LocalStorage.getCurrentUser()).toEqual(null); + expect(LocalStorage.getCurrentUser()).toEqual(undefined); expect(LocalStorage.getMergeDupsBlacklist()).toEqual({}); expect(LocalStorage.getProjectId()).toEqual(""); expect(LocalStorage.getUserId()).toEqual(""); diff --git a/src/components/GoalTimeline/tests/GoalTimelineActions.test.tsx b/src/components/GoalTimeline/tests/GoalTimelineActions.test.tsx index 1349c48fec..6a0b60bfbf 100644 --- a/src/components/GoalTimeline/tests/GoalTimelineActions.test.tsx +++ b/src/components/GoalTimeline/tests/GoalTimelineActions.test.tsx @@ -60,7 +60,7 @@ let mockGoalData: MergeDupData; const createMockStore = configureMockStore([thunk]); let mockStore: MockStoreEnhanced; let oldProjectId: string; -let oldUser: User | null; +let oldUser: User | undefined; const mockProjectId = "123"; const mockUserEditId = "456"; diff --git a/src/components/Login/AuthHeaders.tsx b/src/components/Login/AuthHeaders.tsx index f68e554607..940dfb3af2 100644 --- a/src/components/Login/AuthHeaders.tsx +++ b/src/components/Login/AuthHeaders.tsx @@ -1,5 +1,4 @@ import { getCurrentUser } from "backend/localStorage"; -import { User } from "types/user"; export interface AuthHeader { authorization?: string; @@ -15,7 +14,7 @@ export interface AuthHeader { */ export default function authHeader(): AuthHeader { - const user: User | null = getCurrentUser(); + const user = getCurrentUser(); if (user && user.token) { return { authorization: "Bearer " + user.token }; } else { diff --git a/src/components/Login/LoginActions.tsx b/src/components/Login/LoginActions.tsx index f4cab91a78..165aaba848 100644 --- a/src/components/Login/LoginActions.tsx +++ b/src/components/Login/LoginActions.tsx @@ -105,7 +105,7 @@ export function loginReset(): UserAction { export function logoutAndResetStore() { return (dispatch: StoreStateDispatch) => { - const user: User | null = LocalStorage.getCurrentUser(); + const user = LocalStorage.getCurrentUser(); if (user) { dispatch(logout(user.username)); } diff --git a/src/components/Login/tests/AuthHeaders.test.tsx b/src/components/Login/tests/AuthHeaders.test.tsx index 9c18c2bb9f..afb5cfdd39 100644 --- a/src/components/Login/tests/AuthHeaders.test.tsx +++ b/src/components/Login/tests/AuthHeaders.test.tsx @@ -2,7 +2,7 @@ import * as LocalStorage from "backend/localStorage"; import { User } from "types/user"; import authHeader from "components/Login/AuthHeaders"; -let oldUser: User | null; +let oldUser: User | undefined; beforeAll(() => { oldUser = LocalStorage.getCurrentUser(); diff --git a/src/components/UserSettings/UserSettings.tsx b/src/components/UserSettings/UserSettings.tsx index 376921b4a9..3a9637d783 100644 --- a/src/components/UserSettings/UserSettings.tsx +++ b/src/components/UserSettings/UserSettings.tsx @@ -98,8 +98,8 @@ class UserSettings extends React.Component< > { constructor(props: LocalizeContextProps) { super(props); - const potentialUser: User | null = getCurrentUser(); - const user: User = potentialUser ? potentialUser : new User("", "", ""); + const potentialUser = getCurrentUser(); + const user = potentialUser ? potentialUser : new User("", "", ""); this.state = { user: user, name: user.name, diff --git a/src/goals/CharInventoryCreation/tests/CharacterInventoryActions.test.tsx b/src/goals/CharInventoryCreation/tests/CharacterInventoryActions.test.tsx index 5243152100..56a75320e8 100644 --- a/src/goals/CharInventoryCreation/tests/CharacterInventoryActions.test.tsx +++ b/src/goals/CharInventoryCreation/tests/CharacterInventoryActions.test.tsx @@ -55,7 +55,7 @@ const MOCK_STATE = { }; let oldProjectId: string; -let oldUser: User | null; +let oldUser: User | undefined; const mockProjectId = "123"; const mockUserEditId = "456"; const mockUserId = "789";