@panneau/action-duplicate 3.0.160

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
package/es/index.js ADDED
@@ -0,0 +1,170 @@
1
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
2
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
3
+ import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
4
+ import { postJSON, getCSRFHeaders } from '@folklore/fetch';
5
+ import classNames from 'classnames';
6
+ import PropTypes from 'prop-types';
7
+ import React, { useState, useMemo, useCallback } from 'react';
8
+ import { FormattedMessage } from 'react-intl';
9
+ import Button from '@panneau/element-button';
10
+ import Confirm from '@panneau/modal-confirm';
11
+
12
+ var styles = {};
13
+
14
+ var _excluded = ["title", "description", "endpoint", "label", "icon", "value", "theme", "disabled", "onChange", "setSelectedItems", "className"];
15
+ var propTypes = {
16
+ title: PropTypes.node,
17
+ description: PropTypes.node,
18
+ endpoint: PropTypes.string,
19
+ label: PropTypes.string,
20
+ value: PropTypes.bool,
21
+ icon: PropTypes.string,
22
+ theme: PropTypes.string,
23
+ disabled: PropTypes.bool,
24
+ onChange: PropTypes.func.isRequired,
25
+ setSelectedItems: PropTypes.func,
26
+ className: PropTypes.string
27
+ };
28
+ var defaultProps = {
29
+ title: null,
30
+ description: null,
31
+ endpoint: '/duplicate',
32
+ label: /*#__PURE__*/React.createElement(FormattedMessage, {
33
+ id: "HzCxmr",
34
+ defaultMessage: [{
35
+ "type": 0,
36
+ "value": "Duplicate"
37
+ }]
38
+ }),
39
+ icon: 'trash',
40
+ value: null,
41
+ theme: 'primary',
42
+ disabled: false,
43
+ setSelectedItems: null,
44
+ className: null
45
+ };
46
+ var DuplicateAction = function DuplicateAction(_ref) {
47
+ var title = _ref.title,
48
+ description = _ref.description,
49
+ endpoint = _ref.endpoint,
50
+ label = _ref.label,
51
+ icon = _ref.icon,
52
+ value = _ref.value,
53
+ theme = _ref.theme,
54
+ disabled = _ref.disabled,
55
+ onChange = _ref.onChange,
56
+ setSelectedItems = _ref.setSelectedItems,
57
+ className = _ref.className,
58
+ props = _objectWithoutProperties(_ref, _excluded);
59
+ var _useState = useState(false),
60
+ _useState2 = _slicedToArray(_useState, 2),
61
+ showModal = _useState2[0],
62
+ setShowModal = _useState2[1];
63
+ var _useState3 = useState(null),
64
+ _useState4 = _slicedToArray(_useState3, 2),
65
+ error = _useState4[0],
66
+ setError = _useState4[1];
67
+ var ids = useMemo(function () {
68
+ return (value || []).map(function () {
69
+ var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
70
+ _ref2$id = _ref2.id,
71
+ id = _ref2$id === void 0 ? null : _ref2$id;
72
+ return id;
73
+ }).filter(function (id) {
74
+ return id !== null;
75
+ });
76
+ }, [value]);
77
+ var idLabels = useMemo(function () {
78
+ return (ids || []).map(function (id) {
79
+ return "#".concat(id);
80
+ }).join(', ');
81
+ }, [ids]);
82
+ var onOpen = useCallback(function () {
83
+ setShowModal(true);
84
+ }, [setShowModal]);
85
+ var onConfirm = useCallback(function () {
86
+ postJSON(endpoint, {
87
+ ids: ids
88
+ }, {
89
+ credentials: 'include',
90
+ headers: getCSRFHeaders(),
91
+ _method: 'POST'
92
+ }).then(function (response) {
93
+ setShowModal(false);
94
+ if (setSelectedItems !== null) {
95
+ setSelectedItems([]);
96
+ }
97
+ if (onChange !== null) {
98
+ onChange(response);
99
+ }
100
+ })["catch"](function (err) {
101
+ setError(err);
102
+ });
103
+ }, [ids, endpoint, onChange, setShowModal, setError]);
104
+ var onClose = useCallback(function () {
105
+ setShowModal(false);
106
+ }, [setShowModal]);
107
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, Object.assign({}, props, {
108
+ className: classNames([styles.container, _defineProperty({}, className, className !== null)]),
109
+ label: label,
110
+ icon: icon,
111
+ onClick: onOpen,
112
+ disabled: disabled,
113
+ theme: disabled ? 'secondary' : theme
114
+ })), showModal ? /*#__PURE__*/React.createElement(Confirm, {
115
+ title: title || /*#__PURE__*/React.createElement(FormattedMessage, {
116
+ id: "8T+JE7",
117
+ defaultMessage: [{
118
+ "type": 0,
119
+ "value": "Duplicate"
120
+ }]
121
+ }),
122
+ onConfirm: onConfirm,
123
+ onClose: onClose,
124
+ confirmButton: {
125
+ label: /*#__PURE__*/React.createElement(FormattedMessage, {
126
+ id: "UaVu03",
127
+ defaultMessage: [{
128
+ "type": 0,
129
+ "value": "Confirm"
130
+ }]
131
+ }),
132
+ theme: 'warning'
133
+ },
134
+ cancelButton: {
135
+ label: /*#__PURE__*/React.createElement(FormattedMessage, {
136
+ id: "PyxZY2",
137
+ defaultMessage: [{
138
+ "type": 0,
139
+ "value": "Cancel"
140
+ }]
141
+ })
142
+ }
143
+ }, description !== null ? description : /*#__PURE__*/React.createElement("p", null, /*#__PURE__*/React.createElement(FormattedMessage, {
144
+ id: "XmY7D8",
145
+ defaultMessage: [{
146
+ "type": 0,
147
+ "value": "The following items will be duplicated: "
148
+ }, {
149
+ "type": 1,
150
+ "value": "ids"
151
+ }, {
152
+ "type": 0,
153
+ "value": ". Are you sure you want to continue?"
154
+ }],
155
+ values: {
156
+ ids: idLabels
157
+ }
158
+ })), error !== null ? /*#__PURE__*/React.createElement(FormattedMessage, {
159
+ id: "cutyQH",
160
+ defaultMessage: [{
161
+ "type": 0,
162
+ "value": "An error has occured."
163
+ }]
164
+ }) : null) : null);
165
+ };
166
+ DuplicateAction.propTypes = propTypes;
167
+ DuplicateAction.defaultProps = defaultProps;
168
+ var DuplicateAction$1 = DuplicateAction;
169
+
170
+ export { DuplicateAction$1 as default };
package/lib/index.js ADDED
@@ -0,0 +1,174 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _defineProperty = require('@babel/runtime/helpers/defineProperty');
6
+ var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
7
+ var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
8
+ var fetch = require('@folklore/fetch');
9
+ var classNames = require('classnames');
10
+ var PropTypes = require('prop-types');
11
+ var React = require('react');
12
+ var reactIntl = require('react-intl');
13
+ var Button = require('@panneau/element-button');
14
+ var Confirm = require('@panneau/modal-confirm');
15
+
16
+ var styles = {};
17
+
18
+ var _excluded = ["title", "description", "endpoint", "label", "icon", "value", "theme", "disabled", "onChange", "setSelectedItems", "className"];
19
+ var propTypes = {
20
+ title: PropTypes.node,
21
+ description: PropTypes.node,
22
+ endpoint: PropTypes.string,
23
+ label: PropTypes.string,
24
+ value: PropTypes.bool,
25
+ icon: PropTypes.string,
26
+ theme: PropTypes.string,
27
+ disabled: PropTypes.bool,
28
+ onChange: PropTypes.func.isRequired,
29
+ setSelectedItems: PropTypes.func,
30
+ className: PropTypes.string
31
+ };
32
+ var defaultProps = {
33
+ title: null,
34
+ description: null,
35
+ endpoint: '/duplicate',
36
+ label: /*#__PURE__*/React.createElement(reactIntl.FormattedMessage, {
37
+ id: "HzCxmr",
38
+ defaultMessage: [{
39
+ "type": 0,
40
+ "value": "Duplicate"
41
+ }]
42
+ }),
43
+ icon: 'trash',
44
+ value: null,
45
+ theme: 'primary',
46
+ disabled: false,
47
+ setSelectedItems: null,
48
+ className: null
49
+ };
50
+ var DuplicateAction = function DuplicateAction(_ref) {
51
+ var title = _ref.title,
52
+ description = _ref.description,
53
+ endpoint = _ref.endpoint,
54
+ label = _ref.label,
55
+ icon = _ref.icon,
56
+ value = _ref.value,
57
+ theme = _ref.theme,
58
+ disabled = _ref.disabled,
59
+ onChange = _ref.onChange,
60
+ setSelectedItems = _ref.setSelectedItems,
61
+ className = _ref.className,
62
+ props = _objectWithoutProperties(_ref, _excluded);
63
+ var _useState = React.useState(false),
64
+ _useState2 = _slicedToArray(_useState, 2),
65
+ showModal = _useState2[0],
66
+ setShowModal = _useState2[1];
67
+ var _useState3 = React.useState(null),
68
+ _useState4 = _slicedToArray(_useState3, 2),
69
+ error = _useState4[0],
70
+ setError = _useState4[1];
71
+ var ids = React.useMemo(function () {
72
+ return (value || []).map(function () {
73
+ var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
74
+ _ref2$id = _ref2.id,
75
+ id = _ref2$id === void 0 ? null : _ref2$id;
76
+ return id;
77
+ }).filter(function (id) {
78
+ return id !== null;
79
+ });
80
+ }, [value]);
81
+ var idLabels = React.useMemo(function () {
82
+ return (ids || []).map(function (id) {
83
+ return "#".concat(id);
84
+ }).join(', ');
85
+ }, [ids]);
86
+ var onOpen = React.useCallback(function () {
87
+ setShowModal(true);
88
+ }, [setShowModal]);
89
+ var onConfirm = React.useCallback(function () {
90
+ fetch.postJSON(endpoint, {
91
+ ids: ids
92
+ }, {
93
+ credentials: 'include',
94
+ headers: fetch.getCSRFHeaders(),
95
+ _method: 'POST'
96
+ }).then(function (response) {
97
+ setShowModal(false);
98
+ if (setSelectedItems !== null) {
99
+ setSelectedItems([]);
100
+ }
101
+ if (onChange !== null) {
102
+ onChange(response);
103
+ }
104
+ })["catch"](function (err) {
105
+ setError(err);
106
+ });
107
+ }, [ids, endpoint, onChange, setShowModal, setError]);
108
+ var onClose = React.useCallback(function () {
109
+ setShowModal(false);
110
+ }, [setShowModal]);
111
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, Object.assign({}, props, {
112
+ className: classNames([styles.container, _defineProperty({}, className, className !== null)]),
113
+ label: label,
114
+ icon: icon,
115
+ onClick: onOpen,
116
+ disabled: disabled,
117
+ theme: disabled ? 'secondary' : theme
118
+ })), showModal ? /*#__PURE__*/React.createElement(Confirm, {
119
+ title: title || /*#__PURE__*/React.createElement(reactIntl.FormattedMessage, {
120
+ id: "8T+JE7",
121
+ defaultMessage: [{
122
+ "type": 0,
123
+ "value": "Duplicate"
124
+ }]
125
+ }),
126
+ onConfirm: onConfirm,
127
+ onClose: onClose,
128
+ confirmButton: {
129
+ label: /*#__PURE__*/React.createElement(reactIntl.FormattedMessage, {
130
+ id: "UaVu03",
131
+ defaultMessage: [{
132
+ "type": 0,
133
+ "value": "Confirm"
134
+ }]
135
+ }),
136
+ theme: 'warning'
137
+ },
138
+ cancelButton: {
139
+ label: /*#__PURE__*/React.createElement(reactIntl.FormattedMessage, {
140
+ id: "PyxZY2",
141
+ defaultMessage: [{
142
+ "type": 0,
143
+ "value": "Cancel"
144
+ }]
145
+ })
146
+ }
147
+ }, description !== null ? description : /*#__PURE__*/React.createElement("p", null, /*#__PURE__*/React.createElement(reactIntl.FormattedMessage, {
148
+ id: "XmY7D8",
149
+ defaultMessage: [{
150
+ "type": 0,
151
+ "value": "The following items will be duplicated: "
152
+ }, {
153
+ "type": 1,
154
+ "value": "ids"
155
+ }, {
156
+ "type": 0,
157
+ "value": ". Are you sure you want to continue?"
158
+ }],
159
+ values: {
160
+ ids: idLabels
161
+ }
162
+ })), error !== null ? /*#__PURE__*/React.createElement(reactIntl.FormattedMessage, {
163
+ id: "cutyQH",
164
+ defaultMessage: [{
165
+ "type": 0,
166
+ "value": "An error has occured."
167
+ }]
168
+ }) : null) : null);
169
+ };
170
+ DuplicateAction.propTypes = propTypes;
171
+ DuplicateAction.defaultProps = defaultProps;
172
+ var DuplicateAction$1 = DuplicateAction;
173
+
174
+ exports.default = DuplicateAction$1;
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@panneau/action-duplicate",
3
+ "version": "3.0.160",
4
+ "description": "Duplicate action",
5
+ "keywords": [
6
+ "javascript"
7
+ ],
8
+ "homepage": "https://github.com/folkloreinc/panneau-js",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/folkloreinc/panneau-js.git"
12
+ },
13
+ "author": {
14
+ "name": "Folklore",
15
+ "email": "info@folklore.email"
16
+ },
17
+ "contributors": [
18
+ {
19
+ "name": "David Mongeau-Petitpas",
20
+ "email": "dmp@folklore.email"
21
+ },
22
+ {
23
+ "name": "Nicolas Roy-Bourdages",
24
+ "email": "nrb@folklore.email"
25
+ }
26
+ ],
27
+ "license": "ISC",
28
+ "main": "lib/index.js",
29
+ "module": "es/index.js",
30
+ "files": [
31
+ "lib",
32
+ "es",
33
+ "assets"
34
+ ],
35
+ "scripts": {
36
+ "prepublishOnly": "npm run build",
37
+ "build": "../../scripts/prepare-package.sh"
38
+ },
39
+ "devDependencies": {
40
+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
41
+ "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
42
+ },
43
+ "peerDependencies": {
44
+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
45
+ "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
46
+ },
47
+ "dependencies": {
48
+ "@babel/runtime": "^7.12.5",
49
+ "@folklore/fetch": "^0.1.17",
50
+ "@panneau/core": "^3.0.160",
51
+ "@panneau/element-button": "^3.0.160",
52
+ "@panneau/modal-confirm": "^3.0.160",
53
+ "@panneau/themes": "^3.0.160",
54
+ "classnames": "^2.5.1",
55
+ "prop-types": "^15.7.2",
56
+ "react-intl": "^5.15.8||^6.0.0"
57
+ },
58
+ "publishConfig": {
59
+ "access": "public"
60
+ },
61
+ "gitHead": "71a2a64cd2e0ed3cc921b0ec3b0a69449a4a978c"
62
+ }