First pass - realigning/simplifying/refactoring (#33)

Realigning and simplifying directories and naming.

- Combining “containers” into “components”.
- Flattening out “data” into “reducers” and “config” to consolidate configuration-like files in one place and to make reducers a peer of its teammates (components, actions, sagas, and services).
- Creating dev/prod-specific redux configurations.
- Converting “index.jsx” files into files named for their contents.
- Splitting up the top-level “index.jsx” file into an entry point and an “App” component.
- Renaming SCSS file to “index.scss” to keep it consistent with where it’s imported.
- Renaming/simplifying some variables.
This commit is contained in:
David Joy
2019-02-25 15:36:24 -05:00
committed by GitHub
parent 6e41a0a928
commit 0e1a3356aa
38 changed files with 346 additions and 421 deletions

View File

@@ -1,18 +0,0 @@
import { getAuthenticatedAPIClient } from '@edx/frontend-auth';
import { configuration } from '../config';
const apiClient = getAuthenticatedAPIClient({
appBaseUrl: configuration.BASE_URL,
authBaseUrl: process.env.LMS_BASE_URL,
loginUrl: configuration.LOGIN_URL,
logoutUrl: configuration.LOGOUT_URL,
csrfTokenApiPath: process.env.CSRF_TOKEN_API_PATH,
refreshAccessTokenEndpoint: configuration.REFRESH_ACCESS_TOKEN_ENDPOINT,
accessTokenCookieName: configuration.ACCESS_TOKEN_COOKIE_NAME,
csrfCookieName: configuration.CSRF_COOKIE_NAME,
});
export default apiClient;

View File

@@ -1,179 +0,0 @@
import defaultsDeep from 'lodash.defaultsdeep';
import {
SAVE_PROFILE,
SAVE_PROFILE_PHOTO,
DELETE_PROFILE_PHOTO,
EDITABLE_FIELD_CLOSE,
EDITABLE_FIELD_OPEN,
FETCH_PROFILE,
} from '../../actions/profile';
import {
FETCH_PREFERENCES,
SAVE_PREFERENCES,
} from '../../actions/preferences';
const initialState = {
error: null,
saveState: null,
savePhotoState: null,
savePreferencesState: null,
saveProfileState: null,
currentlyEditingField: null,
profile: {},
preferences: {},
};
// This function returns state based on priority:
// if any are pending > the state is pending
// then, if any are errors > the state is error
// then, if any are complete > the state is complete
// else null
const mergeSaveStates = (statesToMerge) => {
const statePriority = ['pending', 'error', 'complete', null];
statesToMerge.sort((a, b) => statePriority.indexOf(a) - statePriority.indexOf(b));
return statesToMerge[0];
};
const profilePage = (state = initialState, action) => {
switch (action.type) {
case EDITABLE_FIELD_OPEN:
return {
...state,
currentlyEditingField: action.fieldName,
};
case EDITABLE_FIELD_CLOSE:
// Only close if the field to close is undefined or matches the field that is currently open
if (action.fieldName === state.currentlyEditingField) {
return {
...state,
currentlyEditingField: null,
};
}
return state;
case FETCH_PREFERENCES.SUCCESS:
return {
...state,
preferences: defaultsDeep({}, action.preferences, state.preferences),
};
case SAVE_PREFERENCES.BEGIN:
return {
...state,
savePreferencesState: 'pending',
saveState: mergeSaveStates(['pending', state.saveProfileState]),
};
case SAVE_PREFERENCES.SUCCESS:
// defaults deep used because our preferences/state object is multi-dimensional
return {
...state,
savePreferencesState: 'complete',
saveState: mergeSaveStates(['complete', state.saveProfileState]),
};
case SAVE_PREFERENCES.FAILURE:
return {
...state,
savePreferencesState: 'error',
saveState: mergeSaveStates(['error', state.saveProfileState]),
};
case SAVE_PREFERENCES.RESET:
return {
...state,
savePreferencesState: null,
saveState: mergeSaveStates([null, state.saveProfileState]),
error: null,
};
case FETCH_PROFILE.SUCCESS:
return {
...state,
profile: action.payload.profile,
};
case SAVE_PROFILE.BEGIN:
return {
...state,
saveProfileState: 'pending',
saveState: mergeSaveStates(['pending', state.savePreferencesState]),
error: null,
};
case SAVE_PROFILE.SUCCESS:
return {
...state,
saveProfileState: 'complete',
saveState: mergeSaveStates(['complete', state.savePreferencesState]),
error: null,
};
case SAVE_PROFILE.FAILURE:
return {
...state,
saveProfileState: 'error',
saveState: mergeSaveStates(['error', state.savePreferencesState]),
error: action.payload.error,
};
case SAVE_PROFILE.RESET:
return {
...state,
saveProfileState: null,
saveState: mergeSaveStates([null, state.savePreferencesState]),
error: null,
};
case SAVE_PROFILE_PHOTO.BEGIN:
return {
...state,
savePhotoState: 'pending',
error: null,
};
case SAVE_PROFILE_PHOTO.SUCCESS:
return {
...state,
savePhotoState: 'complete',
error: null,
};
case SAVE_PROFILE_PHOTO.FAILURE:
return {
...state,
savePhotoState: 'error',
error: action.payload.error,
};
case SAVE_PROFILE_PHOTO.RESET:
return {
...state,
savePhotoState: null,
error: null,
};
case DELETE_PROFILE_PHOTO.BEGIN:
return {
...state,
savePhotoState: 'pending',
error: null,
};
case DELETE_PROFILE_PHOTO.SUCCESS:
return {
...state,
savePhotoState: 'complete',
error: null,
};
case DELETE_PROFILE_PHOTO.FAILURE:
return {
...state,
savePhotoState: 'error',
error: action.payload.error,
};
case DELETE_PROFILE_PHOTO.RESET:
return {
...state,
savePhotoState: null,
error: null,
};
default:
return state;
}
};
export default profilePage;

View File

@@ -1,18 +0,0 @@
import { combineReducers } from 'redux';
import { userAccount } from '@edx/frontend-auth';
import profilePage from './ProfilePageReducer';
const identityReducer = (state) => {
const newState = { ...state };
return newState;
};
const rootReducer = combineReducers({
// The authentication state is added as initialState when
// creating the store in data/store.js.
authentication: identityReducer,
userAccount,
profilePage,
});
export default rootReducer;

View File

@@ -1,22 +0,0 @@
import { applyMiddleware, createStore } from 'redux';
import createSagaMiddleware from 'redux-saga';
import thunkMiddleware from 'redux-thunk';
import { composeWithDevTools } from 'redux-devtools-extension/logOnlyInProduction';
import { createLogger } from 'redux-logger';
import apiClient from './apiClient';
import reducers from './reducers/RootReducer';
import rootSaga from '../sagas/RootSaga';
const loggerMiddleware = createLogger();
const sagaMiddleware = createSagaMiddleware();
const initialState = apiClient.getAuthenticationState();
const store = createStore(
reducers,
initialState,
composeWithDevTools(applyMiddleware(thunkMiddleware, sagaMiddleware, loggerMiddleware)),
);
sagaMiddleware.run(rootSaga);
export default store;