Skip to content

Commit 654d924

Browse files
authored
Merge pull request #15 from jmeas/404
Add Not Found page for Gists
2 parents 9cdeaff + bd0c073 commit 654d924

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

package-lock.json

Lines changed: 17 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"react-scripts": "1.0.10",
1414
"redux": "^3.7.1",
1515
"redux-resource": "^2.2.0",
16+
"redux-resource-plugins": "^2.0.0",
1617
"redux-resource-xhr": "^2.0.0",
1718
"redux-thunk": "^2.2.0",
1819
"xhr": "^2.4.0"

src/components/Gist.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { readGist, updateGist, deleteGist } from '../state/gists/action-creators
99
class Gist extends Component {
1010
render() {
1111
const {
12-
readGistStatus, deleteGistStatus, updateGistStatus
12+
readGistStatus, deleteGistStatus, updateGistStatus, gistNotFound
1313
} = this.props;
1414
const { description, files } = this.state;
1515

@@ -25,7 +25,8 @@ class Gist extends Component {
2525
return (
2626
<div className="Gist">
2727
{readGistStatus.pending && ('Loading gist...')}
28-
{readGistStatus.failed && ('There was an error while retrieving this gist')}
28+
{readGistStatus.failed && !gistNotFound && ('There was an error while retrieving this gist')}
29+
{readGistStatus.failed && gistNotFound && ('This gist could not be found.')}
2930
{readGistStatus.succeeded && (
3031
<form>
3132
<div>
@@ -213,6 +214,13 @@ function mapStateToProps(state, props) {
213214
// documentation:
214215
// https://redux-resource.js.org/docs/api-reference/get-status.html#tips
215216
const readGistStatus = getStatus(state, `gists.meta.${gistId}.readStatus`, true);
217+
218+
// We're using the HTTP Status Code plugin to determine if the error is a 404. Typically,
219+
// if you're using standard HTTP requests in your application, you'll want to include the
220+
// HTTP Status Codes plugin. You can see how this is set up by referring to the gists
221+
// reducer file. For more on the HTTP Status Codes plugin, see the docs at:
222+
// https://redux-resource.js.org/docs/extras/http-status-codes-plugin.html
223+
const gistNotFound = _.get(state, `gists.meta.${gistId}.readStatusCode`) === 404;
216224

217225
// These requests are initiated by a user's action, so we do not pass `treatNullAsPending`
218226
// as `true`. Otherwise, the interface would always display a loading indicator to the user.
@@ -224,6 +232,7 @@ function mapStateToProps(state, props) {
224232
gists,
225233
gistId,
226234
gist,
235+
gistNotFound,
227236
readGistStatus,
228237
deleteGistStatus,
229238
updateGistStatus

src/state/gists/reducer.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
import { resourceReducer } from 'redux-resource';
2+
import { httpStatusCodes } from 'redux-resource-plugins';
23

3-
export default resourceReducer('gists');
4+
export default resourceReducer('gists', {
5+
plugins: [
6+
// This plugin gives us access to the HTTP Status Codes from our requests. The primary
7+
// use case for this is differentiating different failed requests. For instance,
8+
// did the request fail because the user was unauthorized, or because the resource
9+
// was not found? For more on the HTTP Status Codes plugin, see the documentation at:
10+
// https://redux-resource.js.org/docs/extras/http-status-codes-plugin.html
11+
// If you're using, say, gRPC, then you would want to write a similar plugin that
12+
// handles the gRPC status codes.
13+
httpStatusCodes
14+
]
15+
});

0 commit comments

Comments
 (0)