From f8e326f4d5052ecfdb74721cdc7a4ce8a38c3725 Mon Sep 17 00:00:00 2001 From: Luke De Feo Date: Thu, 30 Mar 2023 10:05:59 -0700 Subject: [PATCH] Rename login/logout of reducer to setUserProfile Summary: The source of truth for logging in is the atom, the userprofile is a side effect and is cached so calling these actions login/logout is very confusing, especially considering those functions exist elsewhere that manage the atoms Reviewed By: aigoncharov Differential Revision: D44502481 fbshipit-source-id: 77080cc02134684a58f52fcad74041b4508a22f0 --- .../src/reducers/__tests__/user.node.tsx | 6 ++-- desktop/flipper-ui-core/src/reducers/user.tsx | 29 +++++-------------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/desktop/flipper-ui-core/src/reducers/__tests__/user.node.tsx b/desktop/flipper-ui-core/src/reducers/__tests__/user.node.tsx index 3a43d6b91..51eb9de65 100644 --- a/desktop/flipper-ui-core/src/reducers/__tests__/user.node.tsx +++ b/desktop/flipper-ui-core/src/reducers/__tests__/user.node.tsx @@ -7,11 +7,11 @@ * @format */ -import {default as reducer, login, logout} from '../user'; +import {default as reducer, setUserProfile} from '../user'; test('login', () => { const userData = {name: 'Jane Doe'}; - const res = reducer({}, login(userData)); + const res = reducer({}, setUserProfile(userData)); expect(res).toEqual(userData); }); @@ -20,7 +20,7 @@ test('logout', () => { { name: 'Jane Doe', }, - logout(), + setUserProfile(undefined), ); expect(res).toEqual({}); }); diff --git a/desktop/flipper-ui-core/src/reducers/user.tsx b/desktop/flipper-ui-core/src/reducers/user.tsx index 270db7217..4bf8a9338 100644 --- a/desktop/flipper-ui-core/src/reducers/user.tsx +++ b/desktop/flipper-ui-core/src/reducers/user.tsx @@ -12,14 +12,10 @@ import {User} from 'flipper-common'; export type State = User; -export type Action = - | { - type: 'LOGIN'; - payload: User; - } - | { - type: 'LOGOUT'; - }; +export type Action = { + type: 'SET_USER_PROFILE'; + payload?: User; +}; const INITIAL_STATE: State = {}; @@ -27,23 +23,14 @@ export default function reducer( state: State = INITIAL_STATE, action: Actions, ): State { - if (action.type === 'LOGOUT') { - return {}; - } else if (action.type === 'LOGIN') { - return { - ...state, - ...action.payload, - }; + if (action.type === 'SET_USER_PROFILE') { + return action.payload ?? {}; } else { return state; } } -export const login = (payload: User): Action => ({ - type: 'LOGIN', +export const setUserProfile = (payload: User | undefined): Action => ({ + type: 'SET_USER_PROFILE', payload, }); - -export const logout = (): Action => ({ - type: 'LOGOUT', -});