|
1 | 1 | import * as React from 'react'; |
2 | | -import { Tooltip, Button, Popconfirm } from 'antd'; |
3 | | -import './ResourceActions.less'; |
4 | | -import { |
5 | | - isFile, |
6 | | - chainPredicates, |
7 | | - not, |
8 | | - isDefaultElasticView, |
9 | | - isDeprecated, |
10 | | - isView, |
11 | | -} from '../../utils/nexusMaybe'; |
| 2 | +import { Tooltip, Button, Popconfirm, notification } from 'antd'; |
12 | 3 | import { Resource } from '@bbp/nexus-sdk'; |
13 | 4 |
|
14 | | -const actionTypes = [ |
15 | | - { |
16 | | - name: 'deprecateResource', |
17 | | - predicate: chainPredicates([isDefaultElasticView, not(isDeprecated)]), |
18 | | - title: 'Deprecate this resource', |
19 | | - shortTitle: 'Dangerously Deprecate', |
20 | | - message: ( |
21 | | - <div> |
22 | | - <h3>Warning!</h3> |
23 | | - <p> |
24 | | - Deprecating this resource <em>WILL ABSOLUTELY</em> break this |
25 | | - application for this project. Are you sure you want to deprecate it? |
26 | | - </p> |
27 | | - </div> |
28 | | - ), |
29 | | - icon: 'delete', |
30 | | - danger: true, |
31 | | - }, |
32 | | - { |
33 | | - name: 'deprecateResource', |
34 | | - predicate: chainPredicates([not(isDeprecated), not(isDefaultElasticView)]), |
35 | | - title: 'Deprecate this resource', |
36 | | - message: "Are you sure you'd like to deprecate this resource?", |
37 | | - shortTitle: 'Deprecate', |
38 | | - icon: 'delete', |
39 | | - danger: true, |
40 | | - }, |
41 | | - { |
42 | | - name: 'goToView', |
43 | | - predicate: isView, |
44 | | - title: 'Query this view', |
45 | | - shortTitle: 'Query', |
46 | | - icon: 'search', |
47 | | - }, |
48 | | - { |
49 | | - name: 'downloadFile', |
50 | | - predicate: isFile, |
51 | | - title: 'Download this file', |
52 | | - shortTitle: 'Download', |
53 | | - icon: 'download', |
54 | | - }, |
55 | | -]; |
| 5 | +import './ResourceActions.less'; |
| 6 | + |
| 7 | +export type ActionType = { |
| 8 | + name: string; // A unique name for your action type |
| 9 | + // predicate: This function will be called with the resource passed |
| 10 | + // to test if we want to display this Action Button |
| 11 | + predicate: (resource: Resource) => Promise<boolean>; |
| 12 | + title: string; // A long title displayed on the confirm popup or tooltip |
| 13 | + shortTitle: string; // Displayed on Button |
| 14 | + // message: a longer message to be displayed on on the confirmation popup |
| 15 | + message?: React.ReactElement | string; |
| 16 | + icon: string; // An icon for the button |
| 17 | + danger?: boolean; // should we use a confirmation popup and color the button red? |
| 18 | +}; |
56 | 19 |
|
57 | 20 | const makeButton = ({ |
58 | 21 | title, |
@@ -94,30 +57,50 @@ const makeButton = ({ |
94 | 57 | </div> |
95 | 58 | ); |
96 | 59 |
|
97 | | -const makeActions = ( |
| 60 | +const makeActionButtons = async ( |
98 | 61 | resource: Resource, |
99 | 62 | actionDispatchers: { |
100 | 63 | [key: string]: () => void; |
101 | | - } |
102 | | -) => |
103 | | - actionTypes |
104 | | - .filter(action => action.predicate(resource)) |
| 64 | + }, |
| 65 | + actionTypes: ActionType[] |
| 66 | +) => { |
| 67 | + const appliedActions = await Promise.all( |
| 68 | + actionTypes.map(async action => { |
| 69 | + return await action.predicate(resource); |
| 70 | + }) |
| 71 | + ); |
| 72 | + return actionTypes |
| 73 | + .filter((action, index) => appliedActions[index]) |
105 | 74 | .map(action => |
106 | 75 | makeButton(action)(resource, actionDispatchers[action.name]) |
107 | 76 | ); |
| 77 | +}; |
108 | 78 |
|
109 | 79 | const ResourceActions: React.FunctionComponent<{ |
110 | 80 | resource: Resource; |
111 | 81 | actions: { |
112 | 82 | [key: string]: () => void; |
113 | 83 | }; |
| 84 | + actionTypes: ActionType[]; |
114 | 85 | }> = props => { |
115 | | - const { resource, actions } = props; |
116 | | - return ( |
117 | | - <section className="resource-actions"> |
118 | | - {makeActions(resource, actions)} |
119 | | - </section> |
120 | | - ); |
| 86 | + const { resource, actions, actionTypes } = props; |
| 87 | + const [actionButtons, setActionButtons] = React.useState< |
| 88 | + React.ReactElement[] |
| 89 | + >([]); |
| 90 | + |
| 91 | + React.useEffect(() => { |
| 92 | + makeActionButtons(resource, actions, actionTypes) |
| 93 | + .then(setActionButtons) |
| 94 | + .catch((error: Error) => { |
| 95 | + notification.error({ |
| 96 | + message: |
| 97 | + 'There was an error while fetching information about this resource', |
| 98 | + description: error.message, |
| 99 | + }); |
| 100 | + }); |
| 101 | + }, [resource._self, resource._rev]); |
| 102 | + |
| 103 | + return <section className="resource-actions">{actionButtons}</section>; |
121 | 104 | }; |
122 | 105 |
|
123 | 106 | export default ResourceActions; |
0 commit comments