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
This commit is contained in:
Luke De Feo
2023-03-30 10:05:59 -07:00
committed by Facebook GitHub Bot
parent ea22ed8f56
commit f8e326f4d5
2 changed files with 11 additions and 24 deletions

View File

@@ -7,11 +7,11 @@
* @format * @format
*/ */
import {default as reducer, login, logout} from '../user'; import {default as reducer, setUserProfile} from '../user';
test('login', () => { test('login', () => {
const userData = {name: 'Jane Doe'}; const userData = {name: 'Jane Doe'};
const res = reducer({}, login(userData)); const res = reducer({}, setUserProfile(userData));
expect(res).toEqual(userData); expect(res).toEqual(userData);
}); });
@@ -20,7 +20,7 @@ test('logout', () => {
{ {
name: 'Jane Doe', name: 'Jane Doe',
}, },
logout(), setUserProfile(undefined),
); );
expect(res).toEqual({}); expect(res).toEqual({});
}); });

View File

@@ -12,13 +12,9 @@ import {User} from 'flipper-common';
export type State = User; export type State = User;
export type Action = export type Action = {
| { type: 'SET_USER_PROFILE';
type: 'LOGIN'; payload?: User;
payload: User;
}
| {
type: 'LOGOUT';
}; };
const INITIAL_STATE: State = {}; const INITIAL_STATE: State = {};
@@ -27,23 +23,14 @@ export default function reducer(
state: State = INITIAL_STATE, state: State = INITIAL_STATE,
action: Actions, action: Actions,
): State { ): State {
if (action.type === 'LOGOUT') { if (action.type === 'SET_USER_PROFILE') {
return {}; return action.payload ?? {};
} else if (action.type === 'LOGIN') {
return {
...state,
...action.payload,
};
} else { } else {
return state; return state;
} }
} }
export const login = (payload: User): Action => ({ export const setUserProfile = (payload: User | undefined): Action => ({
type: 'LOGIN', type: 'SET_USER_PROFILE',
payload, payload,
}); });
export const logout = (): Action => ({
type: 'LOGOUT',
});