signal-desktop/ts/state/ducks/user.ts

73 lines
1.2 KiB
TypeScript
Raw Normal View History

import { AnyAction } from 'redux';
2019-01-14 21:49:58 +00:00
import { LocalizerType } from '../../types/Util';
// State
export type UserStateType = {
attachmentsPath: string;
stickersPath: string;
2019-01-14 21:49:58 +00:00
ourNumber: string;
regionCode: string;
i18n: LocalizerType;
};
// Actions
type UserChangedActionType = {
type: 'USER_CHANGED';
payload: {
ourNumber: string;
regionCode: string;
};
};
export type UserActionType = AnyAction | UserChangedActionType;
2019-01-14 21:49:58 +00:00
// Action Creators
export const actions = {
userChanged,
};
function userChanged(attributes: {
ourNumber: string;
regionCode: string;
}): UserChangedActionType {
return {
type: 'USER_CHANGED',
payload: attributes,
};
}
// Reducer
function getEmptyState(): UserStateType {
return {
attachmentsPath: 'missing',
stickersPath: 'missing',
2019-01-14 21:49:58 +00:00
ourNumber: 'missing',
regionCode: 'missing',
i18n: () => 'missing',
};
}
export function reducer(
state: UserStateType = getEmptyState(),
2019-01-14 21:49:58 +00:00
action: UserActionType
): UserStateType {
if (!state) {
return getEmptyState();
}
if (action.type === 'USER_CHANGED') {
const { payload } = action;
return {
...state,
...payload,
};
}
return state;
}