Skip to content

Commit

Permalink
Added a new toast${listKey}Saved hook for customizing Success toast…
Browse files Browse the repository at this point in the history
… notifications (#2817)

Co-authored-by: Mike <mike@madebymike.com.au>
Co-authored-by: Tim Leslie <timl@thinkmill.com.au>
  • Loading branch information
3 people committed Jun 1, 2020
1 parent cced67b commit 927150d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-candles-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/app-admin-ui': minor
---

Added a new `toast${listKey}Saved` hook for customizing Success toast notifications.
18 changes: 18 additions & 0 deletions packages/app-admin-ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ The following hooks are available. Each is a function that takes no arguments.
export default {
logo,
pages,
[toast${listKey}Saved],
};
```

Expand Down Expand Up @@ -124,6 +125,23 @@ export default {
};
```

#### `toast${listKey}Saved`

Allows customizing the content of the Success toast notification when an item is saved. These may be specified on a per-list basis. For example, to customize the Success toast for a list called `MyList`, use `toastMyListSaved`.

Unlike most hooks, this takes a single argument containing the newly-saved item data. It should return a React component.

```javascript
export default {
toastMyListSaved: itemData => (
<div>
<strong>My custom toast notification!</strong>
<span>{itemData._label_}</span>
</div>
),
};
```

### `isAccessAllowed`

This function takes the same arguments as a [shorthand imperative boolean](https://www.keystonejs.com/api/access-control#shorthand-imperative-boolean) access control. It must return either true or false.
Expand Down
4 changes: 3 additions & 1 deletion packages/app-admin-ui/client/pages/Item/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
import { ItemTitle } from './ItemTitle';
import { ItemProvider } from '../../providers/Item';
import { useList } from '../../providers/List';
import { useUIHooks } from '../../providers/Hooks';

const Render = ({ children }) => children();

Expand Down Expand Up @@ -69,6 +70,7 @@ const ItemDetails = ({ list, item: initialData, itemErrors, onUpdate }) => {

const history = useHistory();
const { addToast } = useToasts();
const { [`toast${list.key}Saved`]: customToast } = useUIHooks();

const [updateItem, { loading: updateInProgress }] = useMutation(list.updateMutation, {
errorPolicy: 'all',
Expand Down Expand Up @@ -214,7 +216,7 @@ const ItemDetails = ({ list, item: initialData, itemErrors, onUpdate }) => {
const savedItem = await onUpdate();

// Defer the toast to this point since it ensures up-to-date data, such as for _label_.
toastItemSuccess({ addToast }, savedItem, 'Saved successfully');
toastItemSuccess({ addToast, customToast }, savedItem, 'Saved successfully');

// No changes since we kicked off the item saving.
// Then reset the state to the current server value
Expand Down
6 changes: 4 additions & 2 deletions packages/app-admin-ui/client/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ export const deconstructErrorsToDataShape = ({ graphQLErrors = [] } = {}) => {
// Toast Formatters
// ==============================

export function toastItemSuccess({ addToast }, item, message = 'Success') {
const toastContent = (
export function toastItemSuccess({ addToast, customToast }, item, message = 'Success') {
const toastContent = customToast ? (
customToast(item)
) : (
<div>
{item && item._label_ ? <strong>{item._label_}</strong> : null}
<div>{message}</div>
Expand Down

0 comments on commit 927150d

Please sign in to comment.