2018-08-23 16:29:26 +01:00
|
|
|
import { createAction, handleActions } from 'redux-actions';
|
|
|
|
|
|
|
|
import { createTypes, createAsyncTypes } from '../utils/createTypes';
|
|
|
|
import { createFetchUserSaga } from './fetch-user-saga';
|
2018-08-25 00:24:19 +01:00
|
|
|
import { createAcceptTermsSaga } from './accept-terms-saga';
|
2018-08-30 15:27:53 +01:00
|
|
|
import { createAppMountSaga } from './app-mount-saga';
|
2018-09-07 13:32:38 +01:00
|
|
|
import { createReportUserSaga } from './report-user-saga';
|
2018-09-04 14:54:23 +01:00
|
|
|
import { createShowCertSaga } from './show-cert-saga';
|
2018-08-30 15:27:53 +01:00
|
|
|
import { createUpdateMyEmailSaga } from './update-email-saga';
|
2018-09-14 14:48:16 +01:00
|
|
|
import { createNightModeSaga } from './night-mode-saga';
|
2018-08-23 16:29:26 +01:00
|
|
|
|
2018-09-13 18:28:23 +01:00
|
|
|
import { types as settingsTypes } from './settings';
|
|
|
|
|
2018-08-23 16:29:26 +01:00
|
|
|
const ns = 'app';
|
|
|
|
|
2018-09-04 14:54:23 +01:00
|
|
|
const defaultFetchState = {
|
|
|
|
pending: true,
|
|
|
|
complete: false,
|
|
|
|
errored: false,
|
|
|
|
error: null
|
|
|
|
};
|
|
|
|
|
2018-08-23 16:29:26 +01:00
|
|
|
const initialState = {
|
|
|
|
appUsername: '',
|
2018-09-04 14:54:23 +01:00
|
|
|
showCert: {},
|
|
|
|
showCertFetchState: {
|
|
|
|
...defaultFetchState
|
2018-08-23 16:29:26 +01:00
|
|
|
},
|
2018-09-04 14:54:23 +01:00
|
|
|
user: {},
|
|
|
|
userFetchState: {
|
|
|
|
...defaultFetchState
|
|
|
|
}
|
2018-08-23 16:29:26 +01:00
|
|
|
};
|
|
|
|
|
2018-08-25 00:24:19 +01:00
|
|
|
const types = createTypes(
|
2018-08-30 15:27:53 +01:00
|
|
|
[
|
|
|
|
'appMount',
|
|
|
|
...createAsyncTypes('fetchUser'),
|
|
|
|
...createAsyncTypes('acceptTerms'),
|
2018-09-04 14:54:23 +01:00
|
|
|
...createAsyncTypes('showCert'),
|
2018-09-07 13:32:38 +01:00
|
|
|
...createAsyncTypes('updateMyEmail'),
|
|
|
|
...createAsyncTypes('reportUser')
|
2018-08-30 15:27:53 +01:00
|
|
|
],
|
2018-08-25 00:24:19 +01:00
|
|
|
ns
|
|
|
|
);
|
2018-08-23 16:29:26 +01:00
|
|
|
|
2018-08-25 00:24:19 +01:00
|
|
|
export const sagas = [
|
2018-08-30 15:27:53 +01:00
|
|
|
...createAcceptTermsSaga(types),
|
|
|
|
...createAppMountSaga(types),
|
2018-08-25 00:24:19 +01:00
|
|
|
...createFetchUserSaga(types),
|
2018-09-04 14:54:23 +01:00
|
|
|
...createUpdateMyEmailSaga(types),
|
2018-09-07 13:32:38 +01:00
|
|
|
...createShowCertSaga(types),
|
2018-09-14 14:48:16 +01:00
|
|
|
...createReportUserSaga(types),
|
|
|
|
...createNightModeSaga({ ...types, ...settingsTypes })
|
2018-08-25 00:24:19 +01:00
|
|
|
];
|
2018-08-23 16:29:26 +01:00
|
|
|
|
2018-08-30 15:27:53 +01:00
|
|
|
export const appMount = createAction(types.appMount);
|
2018-08-23 16:29:26 +01:00
|
|
|
|
2018-08-25 00:24:19 +01:00
|
|
|
export const acceptTerms = createAction(types.acceptTerms);
|
|
|
|
export const acceptTermsComplete = createAction(types.acceptTermsComplete);
|
|
|
|
export const acceptTermsError = createAction(types.acceptTermsError);
|
|
|
|
|
2018-08-30 15:27:53 +01:00
|
|
|
export const fetchUser = createAction(types.fetchUser);
|
|
|
|
export const fetchUserComplete = createAction(types.fetchUserComplete);
|
|
|
|
export const fetchUserError = createAction(types.fetchUserError);
|
|
|
|
|
2018-09-07 13:32:38 +01:00
|
|
|
export const reportUser = createAction(types.reportUser);
|
|
|
|
export const reportUserComplete = createAction(types.reportUserComplete);
|
|
|
|
export const reportUserError = createAction(types.reportUserError);
|
|
|
|
|
2018-09-04 14:54:23 +01:00
|
|
|
export const showCert = createAction(types.showCert);
|
|
|
|
export const showCertComplete = createAction(types.showCertComplete);
|
|
|
|
export const showCertError = createAction(types.showCertError);
|
|
|
|
|
2018-08-30 15:27:53 +01:00
|
|
|
export const updateMyEmail = createAction(types.updateMyEmail);
|
|
|
|
export const updateMyEmailComplete = createAction(types.updateMyEmailComplete);
|
|
|
|
export const updateMyEmailError = createAction(types.updateMyEmailError);
|
|
|
|
|
2018-08-23 16:29:26 +01:00
|
|
|
export const isSignedInSelector = state => !!Object.keys(state[ns].user).length;
|
2018-09-04 14:54:23 +01:00
|
|
|
|
2018-09-13 18:28:23 +01:00
|
|
|
export const signInLoadingSelector = state =>
|
|
|
|
userFetchStateSelector(state).pending;
|
|
|
|
|
2018-09-04 14:54:23 +01:00
|
|
|
export const showCertSelector = state => state[ns].showCert;
|
|
|
|
export const showCertFetchStateSelector = state => state[ns].showCertFetchState;
|
|
|
|
|
|
|
|
export const userByNameSelector = username => state => {
|
|
|
|
const { user } = state[ns];
|
|
|
|
return username in user ? user[username] : {};
|
|
|
|
};
|
|
|
|
export const userFetchStateSelector = state => state[ns].userFetchState;
|
2018-08-23 16:29:26 +01:00
|
|
|
export const usernameSelector = state => state[ns].appUsername;
|
2018-09-13 18:28:23 +01:00
|
|
|
export const userSelector = state => {
|
|
|
|
const username = usernameSelector(state);
|
|
|
|
|
|
|
|
return state[ns].user[username] || {};
|
|
|
|
};
|
2018-08-23 16:29:26 +01:00
|
|
|
|
|
|
|
export const reducer = handleActions(
|
|
|
|
{
|
|
|
|
[types.fetchUser]: state => ({
|
|
|
|
...state,
|
2018-09-04 14:54:23 +01:00
|
|
|
userFetchState: { ...defaultFetchState }
|
2018-08-23 16:29:26 +01:00
|
|
|
}),
|
|
|
|
[types.fetchUserComplete]: (state, { payload: { user, username } }) => ({
|
|
|
|
...state,
|
2018-09-13 18:28:23 +01:00
|
|
|
user: {
|
|
|
|
...state.user,
|
|
|
|
[username]: user
|
|
|
|
},
|
2018-08-23 16:29:26 +01:00
|
|
|
appUsername: username,
|
2018-09-04 14:54:23 +01:00
|
|
|
userFetchState: {
|
2018-08-23 16:29:26 +01:00
|
|
|
pending: false,
|
|
|
|
complete: true,
|
|
|
|
errored: false,
|
|
|
|
error: null
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
[types.fetchUserError]: (state, { payload }) => ({
|
|
|
|
...state,
|
2018-09-04 14:54:23 +01:00
|
|
|
userFetchState: {
|
|
|
|
pending: false,
|
|
|
|
complete: false,
|
|
|
|
errored: true,
|
|
|
|
error: payload
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
[types.showCert]: state => ({
|
|
|
|
...state,
|
|
|
|
showCert: {},
|
|
|
|
showCertFetchState: { ...defaultFetchState }
|
|
|
|
}),
|
|
|
|
[types.showCertComplete]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
showCert: payload,
|
|
|
|
showCertFetchState: {
|
|
|
|
pending: false,
|
|
|
|
complete: true,
|
|
|
|
errored: false,
|
|
|
|
error: null
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
[types.showCertError]: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
showCert: {},
|
|
|
|
showCertFetchState: {
|
2018-08-23 16:29:26 +01:00
|
|
|
pending: false,
|
|
|
|
complete: false,
|
|
|
|
errored: true,
|
|
|
|
error: payload
|
|
|
|
}
|
2018-09-13 18:28:23 +01:00
|
|
|
}),
|
|
|
|
[settingsTypes.submitNewUsernameComplete]: (state, { payload }) =>
|
|
|
|
payload
|
|
|
|
? {
|
|
|
|
...state,
|
|
|
|
user: {
|
|
|
|
...state.user,
|
|
|
|
[state.appUsername]: {
|
|
|
|
...state.user[state.appUsername],
|
|
|
|
username: payload
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-14 13:18:31 +01:00
|
|
|
: state,
|
2018-09-14 14:00:43 +01:00
|
|
|
[settingsTypes.submitNewAboutComplete]: (state, { payload }) =>
|
|
|
|
payload
|
|
|
|
? {
|
|
|
|
...state,
|
|
|
|
user: {
|
|
|
|
...state.user,
|
|
|
|
[state.appUsername]: {
|
|
|
|
...state.user[state.appUsername],
|
|
|
|
...payload
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
: state,
|
|
|
|
[settingsTypes.updateUserFlagComplete]: (state, { payload }) =>
|
|
|
|
payload
|
|
|
|
? {
|
|
|
|
...state,
|
|
|
|
user: {
|
|
|
|
...state.user,
|
|
|
|
[state.appUsername]: {
|
|
|
|
...state.user[state.appUsername],
|
|
|
|
...payload
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
: state
|
2018-08-23 16:29:26 +01:00
|
|
|
},
|
|
|
|
initialState
|
|
|
|
);
|