feat(username): Add Username updating

This commit is contained in:
Bouncey
2018-09-13 18:28:23 +01:00
committed by Stuart Taylor
parent 99e025699a
commit 4f54803674
14 changed files with 807 additions and 171 deletions

View File

@@ -0,0 +1,73 @@
import { createAction, handleActions } from 'redux-actions';
import { createTypes, createAsyncTypes } from '../../utils/createTypes';
import { createSettingsSagas } from './settings-sagas';
const ns = 'settings';
const defaultFetchState = {
pending: false,
complete: false,
errored: false,
error: null
};
const initialState = {
usernameValidation: {
isValidUsername: false,
fetchState: { ...defaultFetchState }
}
};
export const types = createTypes(
[
...createAsyncTypes('validateUsername'),
...createAsyncTypes('submitNewUsername')
],
ns
);
export const sagas = [...createSettingsSagas(types)];
export const submitNewUsername = createAction(types.submitNewUsername);
export const submitNewUsernameComplete = createAction(
types.submitNewUsernameComplete,
({ type, username }) => (type === 'success' ? username : null)
);
export const submitNewUsernameError = createAction(
types.submitNewUsernameError
);
export const validateUsername = createAction(types.validateUsername);
export const validateUsernameComplete = createAction(
types.validateUsernameComplete
);
export const validateUsernameError = createAction(types.validateUsernameError);
export const usernameValidationSelector = state => state[ns].usernameValidation;
export const reducer = handleActions(
{
[types.submitNewUsernameComplete]: state => ({
...state,
usernameValidation: { ...initialState.usernameValidation }
}),
[types.validateUsername]: state => ({
...state,
usernameValidation: {
...state.usernameValidation,
isValidUsername: false,
fetchState: { ...defaultFetchState, pending: true }
}
}),
[types.validateUsernameComplete]: (state, { payload }) => ({
...state,
usernameValidation: {
...state.usernameValidation,
isValidUsername: !payload,
fetchState: { ...defaultFetchState, pending: false, complete: true }
}
})
},
initialState
);