Last active
April 25, 2020 02:44
-
-
Save rosschapman/23f96b7ed9e0768bf1aa787af29f9cf2 to your computer and use it in GitHub Desktop.
AppConductor with action manager and state machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
processBookCreate = async (payload) => { | |
// Update component status (sync) | |
this.statusMachine(this.statuses.waiting); | |
// Post request (async) | |
await this.apiAdapater.books.post(action.payload); | |
// Update component status (sync) | |
this.statusMachine(this.statuses.success); | |
}; | |
statusMachine = (nextStatus: Status) => { | |
switch (nextStatus) { | |
case this.statuses.waiting: | |
if ( | |
this.status === this.statuses.idle || | |
this.status === this.statuses.hasData || | |
this.status === this.statuses.hasError | |
) { | |
return this.setState({ status: nextStatus }); | |
} | |
case this.statuses.hasData: | |
if (this.status === this.statuses.success) { | |
return this.setState({ status: nextStatus }); | |
} | |
case this.statuses.success: | |
if (this.status === this.statuses.waiting) { | |
return this.setState({ status: nextStatus }); | |
} | |
default: | |
console.error("Logical fallacy achieved!"); | |
} | |
}; | |
actionDispatch = async (action) => { | |
switch (action.type) { | |
case "SUBMIT_FORM": | |
console.time("actionManager:SUBMIT_FORM"); | |
await this.processBookCreate(action.payload); | |
console.timeEnd("actionManager:SUBMIT_FORM"); | |
console.timeLog("actionManager:SUBMIT_FORM"); | |
break; | |
default: | |
console.error("It should be impossible to get here"); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment