Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add try/catch logic so UI resets "saving" flags when a network call fails. #2

Merged
merged 8 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog

## Unreleased

## [0.0.5](https://github.com/AndrewSouthpaw/crud-muffins/releases/tag/v0.0.5) - 2020-12-03

#### Added

- Add try/catch logic so UI resets "saving" flags when network call fails. [#2](https://github.com/AndrewSouthpaw/crud-muffins/pull/2)

## [0.0.1](https://github.com/AndrewSouthpaw/crud-muffins/releases/tag/v0.0.1) - 2020-08-10

#### Added

- Initial release
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ Because I got super tired of writing the basic state management of CRUD operatio
1. Deleting an item
1. ...and all the UI state associated with it (e.g. `isEditing`, `isSaving`, etc.)

## Setup

```sh
$ yarn add crud-muffins
```

**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.

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!

## Usage

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.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "crud-muffins",
"version": "0.0.3",
"version": "0.0.5",
"description": "A half-baked solution to UI CRUD operations.",
"main": "dist/index.js",
"scripts": {
Expand All @@ -24,7 +24,7 @@
"devDependencies": {
"@babel/cli": "^7.10.5",
"@babel/core": "^7.10.5",
"@babel/preset-env": "^7.10.4",
"@babel/preset-env": "^7.12.1",
"babel-plugin-ramda": "^2.0.0"
},
"dependencies": {
Expand Down
34 changes: 19 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,36 @@ export const useCrudMuffinsUI = (ui, setUi) => {
const setSavingNew = (value) => setSaving(value, '_new')

const update = async (id, cb) => {
// TODO add try/catch
setSaving(true, id)

const item = await cb()

setSaving(false, id)
setEditing(false, id)
return item
try {
const item = await cb()
setEditing(false, id)
return item
} finally {
setSaving(false, id)
}
}

const create = async (cb) => {
// TODO add try/catch
setSavingNew(true)
const item = await cb()
setSavingNew(false)
return item
try {
const item = await cb()
return item
} finally {
setSavingNew(false)
}
}

const destroy = async (id, cb) => {
// TODO add try/catch
setSaving(true, id)

await cb()

setSaving(false, id)
setEditing(false, id)
try {
await cb()
setEditing(false, id)
} finally {
setSaving(false, id)
}
}

return {
Expand Down
Loading