Skip to content

Commit

Permalink
WIP: Optimistic status updates
Browse files Browse the repository at this point in the history
  • Loading branch information
borisyankov committed Feb 14, 2019
1 parent ed7c0c5 commit 32659b3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/actionConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,5 @@ export const DELETE_OUTBOX_MESSAGE: 'DELETE_OUTBOX_MESSAGE' = 'DELETE_OUTBOX_MES
export const DRAFT_UPDATE: 'DRAFT_UPDATE' = 'DRAFT_UPDATE';

export const CLEAR_TYPING: 'CLEAR_TYPING' = 'CLEAR_TYPING';
export const OPTIMISTIC_USER_STATUS_UPDATE: 'OPTIMISTIC_USER_STATUS_UPDATE' =
'OPTIMISTIC_USER_STATUS_UPDATE';
9 changes: 8 additions & 1 deletion src/user-status/userStatusActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
import type { Dispatch, GetState } from '../types';
/* eslint-disable import/no-named-as-default-member */
import api from '../api';
import { getAuth } from '../selectors';
import { getAuth, getSelfUserDetail } from '../selectors';
import { OPTIMISTIC_USER_STATUS_UPDATE } from '../actionConstants';

export const updateUserAwayStatus = (away: boolean) => async (
dispatch: Dispatch,
getState: GetState,
) => {
const auth = getAuth(getState());
api.updateUserStatus(auth, { away });
const selfUserDetail = getSelfUserDetail(getState());
dispatch({
type: OPTIMISTIC_USER_STATUS_UPDATE,
user_id: selfUserDetail.user_id,
away,
});
};

export const updateUserStatusText = (statusText: string) => async (
Expand Down
2 changes: 2 additions & 0 deletions src/user-status/userStatusReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ACCOUNT_SWITCH,
REALM_INIT,
EVENT_USER_STATUS_UPDATE,
OPTIMISTIC_USER_STATUS_UPDATE,
} from '../actionConstants';
import { NULL_OBJECT } from '../nullObjects';

Expand All @@ -21,6 +22,7 @@ export default (state: UserStatusState = initialState, action: Action): UserStat
case REALM_INIT:
return action.data.user_status || initialState;

case OPTIMISTIC_USER_STATUS_UPDATE:
case EVENT_USER_STATUS_UPDATE: {
const newUserStatus = { ...state[action.user_id] };
if (action.away !== undefined) {
Expand Down

0 comments on commit 32659b3

Please sign in to comment.