Skip to content

Commit 231108a

Browse files
Add try/catch logic so UI resets "saving" flags when a network call fails. (#2)
Previously, if a network call failed, the system would think the UI was still in a "saving" state. Now it turns off the "saving" flag even when the call fails.
1 parent be76829 commit 231108a

File tree

5 files changed

+529
-325
lines changed

5 files changed

+529
-325
lines changed

CHANGELOG.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Changelog
2+
3+
## Unreleased
4+
5+
## [0.0.5](https://github.com/AndrewSouthpaw/crud-muffins/releases/tag/v0.0.5) - 2020-12-03
6+
7+
#### Added
8+
9+
- Add try/catch logic so UI resets "saving" flags when network call fails. [#2](https://github.com/AndrewSouthpaw/crud-muffins/pull/2)
10+
11+
## [0.0.1](https://github.com/AndrewSouthpaw/crud-muffins/releases/tag/v0.0.1) - 2020-08-10
12+
13+
#### Added
14+
15+
- Initial release

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ Because I got super tired of writing the basic state management of CRUD operatio
1414
1. Deleting an item
1515
1. ...and all the UI state associated with it (e.g. `isEditing`, `isSaving`, etc.)
1616

17+
## Setup
18+
19+
```sh
20+
$ yarn add crud-muffins
21+
```
22+
23+
**This package is not transpiled and written with modern JS syntax**. You can add this package to the list of `node_modules` you transpile manually.
24+
25+
Why? My current understanding is that it makes code bundles smaller overall when there's only *one* transpilation, as opposed to each module transpiling in its own context. Happy to be convinced otherwise!
26+
1727
## Usage
1828

1929
The API will likely change quite a lot over the next several iterations. It's designed to work with any UI library, e.g. React, you just need to pass in the controls for reading/setting state.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "crud-muffins",
3-
"version": "0.0.3",
3+
"version": "0.0.5",
44
"description": "A half-baked solution to UI CRUD operations.",
55
"main": "dist/index.js",
66
"scripts": {
@@ -24,7 +24,7 @@
2424
"devDependencies": {
2525
"@babel/cli": "^7.10.5",
2626
"@babel/core": "^7.10.5",
27-
"@babel/preset-env": "^7.10.4",
27+
"@babel/preset-env": "^7.12.1",
2828
"babel-plugin-ramda": "^2.0.0"
2929
},
3030
"dependencies": {

src/index.js

+19-15
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,36 @@ export const useCrudMuffinsUI = (ui, setUi) => {
1212
const setSavingNew = (value) => setSaving(value, '_new')
1313

1414
const update = async (id, cb) => {
15-
// TODO add try/catch
1615
setSaving(true, id)
1716

18-
const item = await cb()
19-
20-
setSaving(false, id)
21-
setEditing(false, id)
22-
return item
17+
try {
18+
const item = await cb()
19+
setEditing(false, id)
20+
return item
21+
} finally {
22+
setSaving(false, id)
23+
}
2324
}
2425

2526
const create = async (cb) => {
26-
// TODO add try/catch
2727
setSavingNew(true)
28-
const item = await cb()
29-
setSavingNew(false)
30-
return item
28+
try {
29+
const item = await cb()
30+
return item
31+
} finally {
32+
setSavingNew(false)
33+
}
3134
}
3235

3336
const destroy = async (id, cb) => {
34-
// TODO add try/catch
3537
setSaving(true, id)
3638

37-
await cb()
38-
39-
setSaving(false, id)
40-
setEditing(false, id)
39+
try {
40+
await cb()
41+
setEditing(false, id)
42+
} finally {
43+
setSaving(false, id)
44+
}
4145
}
4246

4347
return {

0 commit comments

Comments
 (0)