68 lines
1 KiB
TypeScript
68 lines
1 KiB
TypeScript
|
import { LocalizerType } from '../../types/Util';
|
||
|
|
||
|
// State
|
||
|
|
||
|
export type UserStateType = {
|
||
|
ourNumber: string;
|
||
|
regionCode: string;
|
||
|
i18n: LocalizerType;
|
||
|
};
|
||
|
|
||
|
// Actions
|
||
|
|
||
|
type UserChangedActionType = {
|
||
|
type: 'USER_CHANGED';
|
||
|
payload: {
|
||
|
ourNumber: string;
|
||
|
regionCode: string;
|
||
|
};
|
||
|
};
|
||
|
|
||
|
export type UserActionType = UserChangedActionType;
|
||
|
|
||
|
// 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 {
|
||
|
ourNumber: 'missing',
|
||
|
regionCode: 'missing',
|
||
|
i18n: () => 'missing',
|
||
|
};
|
||
|
}
|
||
|
|
||
|
export function reducer(
|
||
|
state: UserStateType,
|
||
|
action: UserActionType
|
||
|
): UserStateType {
|
||
|
if (!state) {
|
||
|
return getEmptyState();
|
||
|
}
|
||
|
|
||
|
if (action.type === 'USER_CHANGED') {
|
||
|
const { payload } = action;
|
||
|
|
||
|
return {
|
||
|
...state,
|
||
|
...payload,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
return state;
|
||
|
}
|