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
*/
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({});
});

View File

@@ -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',
});