user reducer
Reviewed By: jknoxville Differential Revision: D14224403 fbshipit-source-id: 8341dd8af03148c4b1f648641bda522804acb22d
This commit is contained in:
committed by
Facebook Github Bot
parent
ffa586dd49
commit
ff0de8caaa
@@ -13,6 +13,7 @@ import tracking from './tracking';
|
|||||||
import server from './server';
|
import server from './server';
|
||||||
import notifications from './notifications';
|
import notifications from './notifications';
|
||||||
import plugins from './plugins';
|
import plugins from './plugins';
|
||||||
|
import user from './user';
|
||||||
|
|
||||||
import type {Logger} from '../fb-interfaces/Logger.js';
|
import type {Logger} from '../fb-interfaces/Logger.js';
|
||||||
import type {Store} from '../reducers/index.js';
|
import type {Store} from '../reducers/index.js';
|
||||||
@@ -27,4 +28,5 @@ export default (store: Store, logger: Logger) =>
|
|||||||
server,
|
server,
|
||||||
notifications,
|
notifications,
|
||||||
plugins,
|
plugins,
|
||||||
|
user,
|
||||||
].forEach(fn => fn(store, logger));
|
].forEach(fn => fn(store, logger));
|
||||||
|
|||||||
27
src/dispatcher/user.js
Normal file
27
src/dispatcher/user.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2018-present Facebook.
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
* @format
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type {Store} from '../reducers/index.js';
|
||||||
|
import type {Logger} from '../fb-interfaces/Logger.js';
|
||||||
|
import {login} from '../reducers/user';
|
||||||
|
import {getUser, logoutUser} from '../fb-stubs/user';
|
||||||
|
|
||||||
|
export default (store: Store, logger: Logger) => {
|
||||||
|
getUser()
|
||||||
|
.then(user => {
|
||||||
|
store.dispatch(login(user));
|
||||||
|
})
|
||||||
|
.catch(console.debug);
|
||||||
|
|
||||||
|
let prevUserName = store.getState().user.name;
|
||||||
|
store.subscribe(() => {
|
||||||
|
if (prevUserName && !store.getState().user.name) {
|
||||||
|
logoutUser();
|
||||||
|
}
|
||||||
|
prevUserName = store.getState().user.name;
|
||||||
|
});
|
||||||
|
};
|
||||||
18
src/fb-stubs/user.js
Normal file
18
src/fb-stubs/user.js
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2018-present Facebook.
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
* @format
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function getUser() {
|
||||||
|
return Promise.reject();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function graphQLQuery(query: string) {
|
||||||
|
return Promise.reject();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function logoutUser(): Promise<void> {
|
||||||
|
return Promise.reject();
|
||||||
|
}
|
||||||
24
src/reducers/__tests__/user.node.js
Normal file
24
src/reducers/__tests__/user.node.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2018-present Facebook.
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
* @format
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {default as reducer, login, logout} from '../user';
|
||||||
|
|
||||||
|
test('login', () => {
|
||||||
|
const userData = {username: 'Jane Doe'};
|
||||||
|
const res = reducer({}, login(userData));
|
||||||
|
expect(res).toEqual(userData);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('logout', () => {
|
||||||
|
const res = reducer(
|
||||||
|
{
|
||||||
|
username: 'Jane Doe',
|
||||||
|
},
|
||||||
|
logout(),
|
||||||
|
);
|
||||||
|
expect(res).toEqual({});
|
||||||
|
});
|
||||||
@@ -11,6 +11,7 @@ import connections from './connections.js';
|
|||||||
import pluginStates from './pluginStates.js';
|
import pluginStates from './pluginStates.js';
|
||||||
import notifications from './notifications.js';
|
import notifications from './notifications.js';
|
||||||
import plugins from './plugins.js';
|
import plugins from './plugins.js';
|
||||||
|
import user from './user.js';
|
||||||
|
|
||||||
import {persistReducer} from 'redux-persist';
|
import {persistReducer} from 'redux-persist';
|
||||||
import storage from 'redux-persist/lib/storage/index.js';
|
import storage from 'redux-persist/lib/storage/index.js';
|
||||||
@@ -35,6 +36,7 @@ import type {
|
|||||||
State as PluginsState,
|
State as PluginsState,
|
||||||
Action as PluginsAction,
|
Action as PluginsAction,
|
||||||
} from './plugins.js';
|
} from './plugins.js';
|
||||||
|
import type {State as UserState, Action as UserAction} from './user.js';
|
||||||
import type {Store as ReduxStore} from 'redux';
|
import type {Store as ReduxStore} from 'redux';
|
||||||
|
|
||||||
type Actions =
|
type Actions =
|
||||||
@@ -43,6 +45,7 @@ type Actions =
|
|||||||
| PluginStatesAction
|
| PluginStatesAction
|
||||||
| NotificationsAction
|
| NotificationsAction
|
||||||
| PluginsAction
|
| PluginsAction
|
||||||
|
| UserAction
|
||||||
| {|type: 'INIT'|};
|
| {|type: 'INIT'|};
|
||||||
|
|
||||||
export type State = {|
|
export type State = {|
|
||||||
@@ -51,6 +54,7 @@ export type State = {|
|
|||||||
pluginStates: PluginStatesState,
|
pluginStates: PluginStatesState,
|
||||||
notifications: NotificationsState,
|
notifications: NotificationsState,
|
||||||
plugins: PluginsState,
|
plugins: PluginsState,
|
||||||
|
user: UserState,
|
||||||
|};
|
|};
|
||||||
|
|
||||||
export type Store = ReduxStore<State, Actions>;
|
export type Store = ReduxStore<State, Actions>;
|
||||||
@@ -79,4 +83,11 @@ export default combineReducers<_, Actions>({
|
|||||||
notifications,
|
notifications,
|
||||||
),
|
),
|
||||||
plugins,
|
plugins,
|
||||||
|
user: persistReducer(
|
||||||
|
{
|
||||||
|
key: 'user',
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
user,
|
||||||
|
),
|
||||||
});
|
});
|
||||||
|
|||||||
51
src/reducers/user.js
Normal file
51
src/reducers/user.js
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2018-present Facebook.
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
* @format
|
||||||
|
*/
|
||||||
|
|
||||||
|
export type User = {
|
||||||
|
name?: string,
|
||||||
|
profile_picture?: {
|
||||||
|
uri: string,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export type State = User;
|
||||||
|
|
||||||
|
export type Action =
|
||||||
|
| {
|
||||||
|
type: 'LOGIN',
|
||||||
|
payload: User,
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
type: 'LOGOUT',
|
||||||
|
};
|
||||||
|
|
||||||
|
const INITIAL_STATE: State = {};
|
||||||
|
|
||||||
|
export default function reducer(
|
||||||
|
state: State = INITIAL_STATE,
|
||||||
|
action: Action,
|
||||||
|
): State {
|
||||||
|
if (action.type === 'LOGOUT') {
|
||||||
|
return {};
|
||||||
|
} else if (action.type === 'LOGIN') {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
...action.payload,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const login = (payload: User): Action => ({
|
||||||
|
type: 'LOGIN',
|
||||||
|
payload,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const logout = (): Action => ({
|
||||||
|
type: 'LOGOUT',
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user