Skip to content

Commit

Permalink
Refactor errors => system messages
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunsaker committed Apr 30, 2018
1 parent 42f4565 commit 369db9f
Show file tree
Hide file tree
Showing 34 changed files with 135 additions and 95 deletions.
4 changes: 4 additions & 0 deletions SETUP_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,14 @@ sudo rm ./App.js && sudo rm ./src/.gitignore && sudo rm ./src/package-lock.json

## 9. Setup ESLint and Prettier

1. Install dependencies:

```
yarn add --dev eslint babel-eslint eslint-config-airbnb eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react eslint-plugin-react-native
```

2. Move config files:

```
sudo mv ./src/.eslintrc.json ./.eslintrc.json && sudo mv ./src/.prettierrc ./prettierrc
```
Expand Down
6 changes: 3 additions & 3 deletions reducers/appData/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export default function appDataReducer(state = initialState, action = {}) {
switch (action.type) {
case 'SET_DATA':
newState = utils.cloneObject(state);
newState.appData[action.payload.node] = action.payload.data;
newState.appState.loading = false;
newState.appState.refreshing = false;
newState[action.payload.node] = action.payload.data;
newState.loading = false;
newState.refreshing = false;
return newState;

default:
Expand Down
32 changes: 12 additions & 20 deletions reducers/appState/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,44 @@ export default function appStateReducer(state = initialState, action = {}) {
switch (action.type) {
case 'SET_APP_START':
newState = utils.cloneObject(state);
newState.appState.appStart = !newState.appState.appStart;
newState.appStart = !newState.appStart;
return newState;

case 'TOGGLE_LOADING':
newState = utils.cloneObject(state);
newState.appState.loading = !newState.appState.loading;
newState.loading = !newState.loading;
return newState;

case 'SET_DEVICE_LOCATION':
newState = utils.cloneObject(state);
newState.appState.deviceLocation = action.payload;
newState.deviceLocation = action.payload;
return newState;

case 'SET_TEMPORARY_IMAGE':
newState = utils.cloneObject(state);
newState.appState.temporaryImage = action.payload;
newState.temporaryImage = action.payload;
return newState;

case 'CLEAR_TEMPORARY_IMAGE':
newState = utils.cloneObject(state);
newState.appState.temporaryImage = null;
return newState;

case 'SET_USER_PHOTO':
newState = utils.cloneObject(state);
newState.userData.profile.userPhotoURL = action.payload;
newState.temporaryImage = null;
return newState;

case 'TOGGLE_FEEDBACK_POSTED':
newState = utils.cloneObject(state);
newState.appState.feedbackPosted = !newState.appState.feedbackPosted;
newState.appState.loading = false;
newState.feedbackPosted = !newState.feedbackPosted;
newState.loading = false;
return newState;

// TODO: Refactor
case 'SET_MESSAGE':
case 'SET_SYSTEM_MESSAGE':
newState = utils.cloneObject(state);
newState.appState.error = {
...action.payload,
};
newState.systemMessage.message = action.payload.message; // instanceof Error
newState.systemMessage.error = action.error;
return newState;

// TODO: Refactor
case 'RESET_MESSAGE':
case 'RESET_SYSTEM_MESSAGE':
newState = utils.cloneObject(state);
newState.appState.error = initialState.appState.error;
newState.systemMessage = initialState.message;
return newState;

default:
Expand Down
13 changes: 2 additions & 11 deletions reducers/appState/initialState.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
const initialState = {
appStart: false,
loading: true,
// TODO: refactor this error object
error: {
errorType: null,
systemMessage: {
message: null,
iconName: null,
success: null,
autoHide: null,
action: {
type: null,
text: null,
data: null,
},
error: null,
},
deviceLocation: null,
temporaryImage: null,
Expand Down
15 changes: 10 additions & 5 deletions reducers/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,30 @@ export default function userReducer(state = initialState, action = {}) {
switch (action.type) {
case 'UPDATE_USER_EMAIL':
newState = utils.cloneObject(state);
newState.user.userEmail = action.payload;
newState.userEmail = action.payload;
return newState;

case 'UPDATE_USER_PASSWORD':
newState = utils.cloneObject(state);
newState.user.userPassword = action.payload;
newState.userPassword = action.payload;
return newState;

case 'SIGN_IN_USER':
newState = utils.cloneObject(state);
newState.user = {
newState = {
...action.payload,
};
newState.user.authenticated = true;
newState.authenticated = true;
return newState;

case 'SIGN_OUT_USER':
newState = utils.cloneObject(state);
newState.user = initialState.user;
newState = initialState;
return newState;

case 'SET_USER_PHOTO':
newState = utils.cloneObject(state);
newState.userPhotoURL = action.payload;
return newState;

default:
Expand Down
6 changes: 4 additions & 2 deletions sagas/analytics/logEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export default function* logEvent(action) {
});
}
} catch (error) {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
6 changes: 4 additions & 2 deletions sagas/auth/getUserAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ export default function* getUserAuth(action) {
});
}
} catch (error) {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
2 changes: 1 addition & 1 deletion sagas/auth/getUserCredentialFromEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function* getUserCredentialFromEmail(action) {
}
} catch (error) {
yield put({
type: 'SET_MESSAGE',
type: 'SET_SYSTEM_MESSAGE',
payload: new Error(error),
error: true,
});
Expand Down
6 changes: 4 additions & 2 deletions sagas/auth/getUserCredentialFromFacebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export default function* getUserCredentialFromFacebook(action) {
});
}
} catch (error) {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
6 changes: 4 additions & 2 deletions sagas/auth/getUserCredentialFromGoogle.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export default function* getUserCredentialFromGoogle(action) {
});
}
} catch (error) {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
12 changes: 8 additions & 4 deletions sagas/auth/linkUserWithCredential.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ export default function* linkUserWithCredential(action) {
payload: response,
});
} catch (error) {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand All @@ -45,9 +47,11 @@ export default function* linkUserWithCredential(action) {
payload: action.payload.credential,
});
} else {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
6 changes: 4 additions & 2 deletions sagas/auth/sendPasswordResetEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ export default function* sendPasswordResetEmail(action) {
}
}
} catch (error) {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
6 changes: 4 additions & 2 deletions sagas/auth/signInUserAnonymously.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export default function* signInUserAnonymously(action) {
});
}
} catch (error) {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
8 changes: 5 additions & 3 deletions sagas/auth/signInUserWithCredential.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ export default function* signInUserWithCredential(action) {
} catch (error) {
if (error.message.code === 'auth/account-exists-with-different-credential') {
yield put({
type: 'SET_MESSAGE',
type: 'SET_SYSTEM_MESSAGE',
payload: new Error(
"Hello! You've already signed in with someone else. Please try another option.",
),
error: true,
});
} else {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
6 changes: 4 additions & 2 deletions sagas/auth/signOutUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export default function* signOutUser(action) {
});
}
} catch (error) {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
6 changes: 4 additions & 2 deletions sagas/database/getData.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export default function* getData(action) {
});
}
} catch (error) {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
6 changes: 4 additions & 2 deletions sagas/database/pushData.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export default function* pushData(action) {
});
}
} catch (error) {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
6 changes: 4 additions & 2 deletions sagas/database/setData.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export default function* setData(action) {
});
}
} catch (error) {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
6 changes: 4 additions & 2 deletions sagas/database/updateData.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export default function* updateData(action) {
});
}
} catch (error) {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
6 changes: 4 additions & 2 deletions sagas/http/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export default function* get(action) {
});
}
} catch (error) {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
6 changes: 4 additions & 2 deletions sagas/http/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export default function* get(action) {
});
}
} catch (error) {
const payload = error instanceof Error ? error : new Error(error);

yield put({
type: 'SET_MESSAGE',
payload: new Error(error),
type: 'SET_SYSTEM_MESSAGE',
payload,
error: true,
});
}
Expand Down
Loading

0 comments on commit 369db9f

Please sign in to comment.