@@ -4,7 +4,9 @@ import { connect } from 'react-redux';
44import { getStatus } from 'redux-resource' ;
55import _ from 'lodash' ;
66import './Gist.css' ;
7- import { readGist , updateGist , deleteGist } from '../state/gists/action-creators' ;
7+ import {
8+ readGist , updateGist , deleteGist , clearUpdateGist
9+ } from '../state/gists/action-creators' ;
810
911class Gist extends Component {
1012 render ( ) {
@@ -44,6 +46,7 @@ class Gist extends Component {
4446 Delete Gist
4547 </ button >
4648 { updateGistStatus . pending && 'Saving gist...' }
49+ { updateGistStatus . succeeded && 'Saved!' }
4750 { deleteGistStatus . pending && 'Deleting gist...' }
4851 </ div >
4952 < div className = "Gist-description" >
@@ -106,10 +109,12 @@ class Gist extends Component {
106109 }
107110
108111 componentDidUpdate ( prevProps ) {
109- const { deleteGistStatus, readGistStatus, history, gist } = this . props ;
112+ const {
113+ deleteGistStatus, readGistStatus, updateGistStatus, clearUpdateGist, history, gist
114+ } = this . props ;
115+ const { gists, gistId } = prevProps ;
110116
111117 if ( deleteGistStatus . succeeded ) {
112- const { gists, gistId } = prevProps ;
113118 const prevGistDeleteStatus = getStatus ( { gists } , `gists.meta.${ gistId } .deleteStatus` ) ;
114119
115120 // When we transition from pending to succeeded, then we know that the deletion was
@@ -124,7 +129,6 @@ class Gist extends Component {
124129 // on the resource. For more on plugins, refer to the documentation:
125130 // https://redux-resource.js.org/docs/guides/plugins.html
126131 if ( readGistStatus . succeeded ) {
127- const { gists, gistId } = prevProps ;
128132 const prevGistReadStatus = getStatus ( { gists } , `gists.meta.${ gistId } .readStatus` ) ;
129133 if ( prevGistReadStatus . pending ) {
130134 const newState = {
@@ -138,6 +142,15 @@ class Gist extends Component {
138142 this . setState ( newState ) ;
139143 }
140144 }
145+
146+ // If the request just succeeded, then we set a timer to reset the request back to a NULL
147+ // state. That way, our success message disappears after a set amount of time.
148+ if ( updateGistStatus . succeeded ) {
149+ const prevGistUpdateStatus = getStatus ( { gists } , `gists.meta.${ gistId } .updateStatus` ) ;
150+ if ( prevGistUpdateStatus . pending ) {
151+ setTimeout ( ( ) => clearUpdateGist ( gistId ) , 1500 ) ;
152+ }
153+ }
141154 }
142155
143156 readGist = ( ) => {
@@ -243,7 +256,8 @@ function mapDispatchToProps(dispatch) {
243256 return bindActionCreators ( {
244257 readGist,
245258 updateGist,
246- deleteGist
259+ deleteGist,
260+ clearUpdateGist
247261 } , dispatch ) ;
248262}
249263
0 commit comments