@panneau/core 0.6.70-alpha.0 → 0.6.73

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.
Files changed (77) hide show
  1. package/es/Panneau.js +33 -35
  2. package/es/components/App.js +5 -5
  3. package/es/components/Layout.js +4 -3
  4. package/es/components/Panneau.js +9 -11
  5. package/es/components/pages/Account.js +2 -1
  6. package/es/components/pages/ResourceCreate.js +2 -2
  7. package/es/components/pages/ResourceForm.js +39 -39
  8. package/es/components/pages/ResourceIndex.js +102 -64
  9. package/es/lib/ComponentsCollection.js +9 -11
  10. package/es/lib/ResourceApi.js +4 -4
  11. package/es/lib/messageWithValues.js +3 -3
  12. package/es/lib/parseDefinition.js +11 -10
  13. package/es/lib/react-container/Container.js +7 -6
  14. package/es/lib/react-container/IntlContainer.js +7 -6
  15. package/es/lib/react-container/RouterContainer.js +2 -1
  16. package/es/lib/react-container/StoreContainer.js +5 -5
  17. package/es/lib/react-container/UrlGenerator.js +6 -5
  18. package/es/lib/react-container/UrlGeneratorContainer.js +25 -14
  19. package/es/lib/react-container/configureStore.dev.js +2 -2
  20. package/es/lib/react-container/withUrlGenerator.js +5 -5
  21. package/es/lib/requests.js +8 -6
  22. package/es/lib/withComponentsCollection.js +6 -6
  23. package/es/lib/withDefinition.js +5 -5
  24. package/es/lib/withFieldsCollection.js +2 -2
  25. package/es/lib/withFormsCollection.js +2 -2
  26. package/es/lib/withLayoutsCollection.js +2 -2
  27. package/es/lib/withListsCollection.js +2 -2
  28. package/es/lib/withModalsCollection.js +2 -2
  29. package/es/lib/withPreviewsCollection.js +2 -2
  30. package/es/lib/withResourceApi.js +11 -13
  31. package/es/reducers/AuthReducer.js +3 -3
  32. package/es/reducers/PanneauReducer.js +4 -4
  33. package/lib/Panneau.js +35 -36
  34. package/lib/actions/PanneauActions.js +1 -1
  35. package/lib/components/App.js +11 -7
  36. package/lib/components/Layout.js +11 -5
  37. package/lib/components/Panneau.js +15 -13
  38. package/lib/components/index.js +4 -4
  39. package/lib/components/pages/Account.js +9 -4
  40. package/lib/components/pages/ResourceCreate.js +8 -4
  41. package/lib/components/pages/ResourceForm.js +47 -42
  42. package/lib/components/pages/ResourceIndex.js +110 -67
  43. package/lib/index.js +47 -47
  44. package/lib/lib/ComponentsCollection.js +9 -11
  45. package/lib/lib/PropTypes.js +1 -1
  46. package/lib/lib/ResourceApi.js +4 -4
  47. package/lib/lib/messageWithValues.js +3 -3
  48. package/lib/lib/parseDefinition.js +12 -10
  49. package/lib/lib/react-container/Container.js +14 -8
  50. package/lib/lib/react-container/IntlContainer.js +14 -8
  51. package/lib/lib/react-container/RouterContainer.js +2 -1
  52. package/lib/lib/react-container/StoreContainer.js +11 -7
  53. package/lib/lib/react-container/UrlGenerator.js +7 -5
  54. package/lib/lib/react-container/UrlGeneratorContainer.js +25 -14
  55. package/lib/lib/react-container/configureStore.dev.js +2 -2
  56. package/lib/lib/react-container/index.js +5 -5
  57. package/lib/lib/react-container/withUrlGenerator.js +11 -7
  58. package/lib/lib/requests.js +10 -7
  59. package/lib/lib/withComponentsCollection.js +12 -8
  60. package/lib/lib/withDefinition.js +11 -7
  61. package/lib/lib/withFieldsCollection.js +8 -4
  62. package/lib/lib/withFormsCollection.js +8 -4
  63. package/lib/lib/withLayoutsCollection.js +8 -4
  64. package/lib/lib/withListsCollection.js +8 -4
  65. package/lib/lib/withModalsCollection.js +8 -4
  66. package/lib/lib/withPreviewsCollection.js +8 -4
  67. package/lib/lib/withResourceApi.js +17 -15
  68. package/lib/reducers/AuthReducer.js +3 -3
  69. package/lib/reducers/PanneauReducer.js +4 -4
  70. package/package.json +3 -3
  71. package/src/components/Panneau.jsx +0 -1
  72. package/src/components/pages/ResourceIndex.jsx +62 -13
  73. package/src/lib/react-container/UrlGeneratorContainer.jsx +17 -10
  74. package/src/styles/pages/resource-index.scss +16 -1
  75. package/es/components/pages/_ResourceIndexOld.js +0 -459
  76. package/lib/components/pages/_ResourceIndexOld.js +0 -488
  77. package/src/components/pages/_ResourceIndexOld.jsx +0 -428
@@ -1,428 +0,0 @@
1
- /* eslint-disable jsx-a11y/anchor-is-valid */
2
- import React, { Component } from 'react';
3
- import PropTypes from 'prop-types';
4
- import { Link } from 'react-router-dom';
5
- import get from 'lodash/get';
6
- import isString from 'lodash/isString';
7
- import isObject from 'lodash/isObject';
8
- import classNames from 'classnames';
9
- import { defineMessages, FormattedMessage, injectIntl } from 'react-intl';
10
- import Loading from '../partials/Loading';
11
-
12
- import * as PanneauPropTypes from '../../lib/PropTypes';
13
- import withResourceApi from '../../lib/withResourceApi';
14
- import withListsCollection from '../../lib/withListsCollection';
15
-
16
- import styles from '../../styles/pages/resource-index.scss';
17
-
18
- const messages = defineMessages({
19
- add: {
20
- id: 'core.buttons.resources.add',
21
- description: 'The label of the "add" index button',
22
- defaultMessage: 'Add {name}',
23
- },
24
- title: {
25
- id: 'core.titles.resources.index',
26
- description: 'The title of the resource index page',
27
- defaultMessage: '{name}',
28
- },
29
- confirmDelete: {
30
- id: 'core.resources.index.confirm_delete',
31
- description: 'The confirm message when deleting on the resource index page',
32
- defaultMessage: 'Are you sure you want to delete this item from {name}?',
33
- },
34
- });
35
-
36
- const propTypes = {
37
- intl: PanneauPropTypes.intl.isRequired,
38
- listsCollection: PanneauPropTypes.componentsCollection.isRequired,
39
- resource: PanneauPropTypes.resource.isRequired,
40
- resourceApi: PanneauPropTypes.resourceApi.isRequired,
41
- query: PropTypes.shape({
42
- page: PropTypes.string,
43
- }),
44
- items: PropTypes.arrayOf(PropTypes.object),
45
- title: PanneauPropTypes.message,
46
- errors: PropTypes.arrayOf(PropTypes.string),
47
- showAddButton: PropTypes.bool,
48
- addButtonLabel: PanneauPropTypes.message,
49
- confirmDeleteMessage: PanneauPropTypes.message,
50
- getResourceActionUrl: PropTypes.func.isRequired,
51
- gotoResourceAction: PropTypes.func.isRequired,
52
- };
53
-
54
- const defaultProps = {
55
- query: null,
56
- items: null,
57
- title: messages.title,
58
- errors: null,
59
- showAddButton: true,
60
- addButtonLabel: messages.add,
61
- confirmDeleteMessage: messages.confirmDelete,
62
- };
63
-
64
- class ResourceIndexOld extends Component {
65
- static getDerivedStateFromProps({ query: nextQuery, errors: nextErrors }, { query, errors }) {
66
- const queryChanged = nextQuery !== query;
67
- const errorsChanged = nextErrors !== errors;
68
- if (queryChanged || errorsChanged) {
69
- return {
70
- query: queryChanged ? nextQuery : query,
71
- errors: errorsChanged ? nextErrors : errors,
72
- isLoading: queryChanged,
73
- };
74
- }
75
- return null;
76
- }
77
-
78
- constructor(props) {
79
- super(props);
80
-
81
- this.onItemsLoaded = this.onItemsLoaded.bind(this);
82
- this.onItemsLoadError = this.onItemsLoadError.bind(this);
83
- this.onClickAction = this.onClickAction.bind(this);
84
- this.onItemDeleted = this.onItemDeleted.bind(this);
85
-
86
- this.state = {
87
- isLoading: props.items === null,
88
- query: null, // eslint-disable-line react/no-unused-state
89
- items: props.items,
90
- pagination: null,
91
- errors: props.errors,
92
- };
93
- }
94
-
95
- componentDidMount() {
96
- const { items } = this.state;
97
- if (items === null) {
98
- this.loadItems();
99
- }
100
- }
101
-
102
- componentDidUpdate({ query: prevQuery }, { items: prevItems }) {
103
- const { query } = this.props;
104
- const { items } = this.state;
105
- const itemsChanged = prevItems !== items;
106
- const queryChanged = prevQuery !== query;
107
- if ((itemsChanged && items === null) || queryChanged) {
108
- this.loadItems();
109
- }
110
- }
111
-
112
- onItemsLoaded(data) {
113
- if (this.isPaginated()) {
114
- const { data: items, ...pagination } = data;
115
- this.setState({
116
- items,
117
- pagination,
118
- isLoading: false,
119
- });
120
- return;
121
- }
122
- this.setState({
123
- items: data,
124
- isLoading: false,
125
- });
126
- }
127
-
128
- onItemsLoadError(errors) {
129
- this.setState({
130
- errors,
131
- isLoading: false,
132
- });
133
- }
134
-
135
- onClickAction(e, action, it) {
136
- const { getResourceActionUrl, gotoResourceAction } = this.props;
137
-
138
- const useRouter = get(action, 'useRouter', true);
139
-
140
- switch (action.id) {
141
- case 'edit':
142
- case 'show':
143
- if (useRouter) {
144
- gotoResourceAction(action.id, it.id);
145
- } else {
146
- window.location.href = getResourceActionUrl(action.id);
147
- }
148
- break;
149
- case 'delete': {
150
- const hasPage = get(action, 'hasPage', false);
151
- if (!hasPage) {
152
- this.deleteItem(it.id);
153
- } else if (useRouter) {
154
- gotoResourceAction(action.id, it.id);
155
- } else {
156
- window.location.href = getResourceActionUrl(action.id);
157
- }
158
- break;
159
- }
160
- default:
161
- break;
162
- }
163
- }
164
-
165
- onItemDeleted({ id }) {
166
- this.setState(({ items }) => ({
167
- items: items.filter(it => it.id !== id),
168
- }));
169
- }
170
-
171
- isPaginated() {
172
- const { resource } = this.props;
173
- return get(resource, 'lists.index.pagination', get(resource, 'lists.pagination', false));
174
- }
175
-
176
- loadItems() {
177
- const { resourceApi, query } = this.props;
178
- const params = {};
179
- if (this.isPaginated()) {
180
- const page = query.page || null;
181
- if (page !== null) {
182
- params.page = page;
183
- }
184
- }
185
- resourceApi
186
- .index(params)
187
- .then(this.onItemsLoaded)
188
- .catch(this.onItemsLoadError);
189
- }
190
-
191
- deleteItem(id) {
192
- const {
193
- resource, resourceApi, intl, confirmDeleteMessage,
194
- } = this.props;
195
- const { name } = resource;
196
- const confirmMessage = get(resource, 'messages.confirm_delete', confirmDeleteMessage);
197
- const message = isObject(confirmMessage) && typeof confirmMessage.id !== 'undefined'
198
- ? intl.formatMessage(confirmMessage, {
199
- name,
200
- id,
201
- })
202
- : confirmMessage;
203
- // eslint-disable-next-line no-alert
204
- if (window.confirm(message)) {
205
- resourceApi.destroy(id).then(this.onItemDeleted);
206
- }
207
- }
208
-
209
- renderAddButton() {
210
- const { addButtonLabel, resource, getResourceActionUrl } = this.props;
211
- const resourceType = get(resource, 'type', 'default');
212
- const isTyped = resourceType === 'typed';
213
- const types = isTyped ? get(resource, 'types', []) : null;
214
- const buttonMessage = get(resource, 'messages.buttons.resources.add', null);
215
- const name = get(
216
- resource,
217
- 'messages.names.a',
218
- get(resource, 'messages.name', resource.name),
219
- );
220
- const buttonLabel = isString(addButtonLabel) ? (
221
- addButtonLabel
222
- ) : (
223
- <FormattedMessage {...addButtonLabel} values={{ name }} />
224
- );
225
- return (
226
- <div
227
- className={classNames({
228
- 'btn-group': true,
229
- })}
230
- >
231
- {isTyped ? (
232
- <button
233
- type="button"
234
- className={classNames({
235
- btn: true,
236
- 'btn-primary': true,
237
- 'dropdown-toggle': isTyped,
238
- })}
239
- data-toggle="dropdown"
240
- aria-haspopup="true"
241
- aria-expanded="false"
242
- >
243
- {buttonMessage !== null ? buttonMessage : buttonLabel}
244
- {' '}
245
- <span className="caret" />
246
- </button>
247
- ) : (
248
- <Link
249
- to={getResourceActionUrl('create')}
250
- className={classNames({
251
- btn: true,
252
- 'btn-primary': true,
253
- })}
254
- >
255
- {buttonMessage !== null ? buttonMessage : buttonLabel}
256
- </Link>
257
- )}
258
- {isTyped ? (
259
- <div className="dropdown-menu">
260
- {types.map(({ id, label }) => (
261
- <Link
262
- key={`add-button-${id}`}
263
- to={`${getResourceActionUrl('create')}?type=${id}`}
264
- className="dropdown-item"
265
- >
266
- {label}
267
- </Link>
268
- ))}
269
- </div>
270
- ) : null}
271
- </div>
272
- );
273
- }
274
-
275
- renderHeader() {
276
- const { resource, title, showAddButton } = this.props;
277
-
278
- const message = get(resource, 'messages.titles.resources.index', null);
279
- const name = get(
280
- resource,
281
- 'messages.names.plural',
282
- get(resource, 'messages.name', resource.name),
283
- );
284
- const defaultTitle = isString(title) ? (
285
- title
286
- ) : (
287
- <FormattedMessage {...title} values={{ name }} />
288
- );
289
-
290
- return (
291
- <div
292
- className={classNames({
293
- 'py-4': true,
294
- [styles.header]: true,
295
- })}
296
- >
297
- <div className={styles.cols}>
298
- <div className={styles.col}>
299
- <h1 className={classNames([styles.title, 'mb-0', 'mt-0'])}>
300
- {message !== null ? message : defaultTitle}
301
- </h1>
302
- </div>
303
- <div
304
- className={classNames({
305
- [styles.col]: true,
306
- 'text-right': true,
307
- })}
308
- >
309
- {showAddButton ? this.renderAddButton() : null}
310
- </div>
311
- </div>
312
- {this.renderErrors()}
313
- </div>
314
- );
315
- }
316
-
317
- renderErrors() {
318
- const { errors } = this.state;
319
-
320
- const errorsClassNames = classNames({
321
- [styles.errors]: true,
322
- });
323
-
324
- /* eslint-disable react/no-array-index-key */
325
- return errors !== null && errors.length > 1 ? (
326
- <div className={errorsClassNames}>
327
- <ul>
328
- {errors.map((error, index) => (
329
- <li key={`error-${index}`}>{error}</li>
330
- ))}
331
- </ul>
332
- </div>
333
- ) : null;
334
- /* eslint-enable react/no-array-index-key */
335
- }
336
-
337
- renderList() {
338
- const { resource, listsCollection } = this.props;
339
- const { items } = this.state;
340
- const list = get(resource, 'lists.index', get(resource, 'lists', {}));
341
- const { type, ...listProps } = list;
342
- const ListComponent = listsCollection.getComponent(type || 'table');
343
-
344
- const listClassNames = classNames({
345
- [styles.list]: true,
346
- });
347
-
348
- return ListComponent !== null ? (
349
- <div className={listClassNames}>
350
- <ListComponent
351
- items={items || []}
352
- onClickAction={this.onClickAction}
353
- {...listProps}
354
- />
355
- </div>
356
- ) : null;
357
- }
358
-
359
- // eslint-disable-next-line class-methods-use-this
360
- renderPagination() {
361
- const { getResourceActionUrl, listsCollection } = this.props;
362
- const { pagination } = this.state;
363
- const Pagination = listsCollection.getComponent('pagination');
364
- return (
365
- <Pagination
366
- total={pagination.total}
367
- perPage={pagination.per_page}
368
- currentPage={pagination.current_page}
369
- lastPage={pagination.last_page}
370
- url={getResourceActionUrl('index')}
371
- />
372
- );
373
- }
374
-
375
- // eslint-disable-next-line class-methods-use-this
376
- renderLoading() {
377
- const { items } = this.state;
378
- return (
379
- <div
380
- className={classNames({
381
- [styles.loading]: true,
382
- [styles.alone]: items === null,
383
- })}
384
- >
385
- <div className={styles.inner}>
386
- <div className={styles.middle}>
387
- <Loading loading />
388
- </div>
389
- </div>
390
- </div>
391
- );
392
- }
393
-
394
- render() {
395
- const { items, pagination, isLoading } = this.state;
396
- const containerClassNames = classNames({
397
- [styles.container]: true,
398
- });
399
- return (
400
- <div className={containerClassNames}>
401
- <div className="container">
402
- <div className="row justify-content-md-center">
403
- <div className="col-lg-8">
404
- {this.renderHeader()}
405
- <div className={styles.listContainer}>
406
- <div className={styles.list}>
407
- {items !== null ? this.renderList() : null}
408
- </div>
409
- {isLoading ? this.renderLoading() : null}
410
- </div>
411
- {pagination !== null && pagination.last_page > 1
412
- ? this.renderPagination()
413
- : null}
414
- </div>
415
- </div>
416
- </div>
417
- </div>
418
- );
419
- }
420
- }
421
-
422
- ResourceIndexOld.propTypes = propTypes;
423
- ResourceIndexOld.defaultProps = defaultProps;
424
-
425
- const WithResourceApi = withResourceApi()(ResourceIndexOld);
426
- const WithListsCollectionContainer = withListsCollection()(WithResourceApi);
427
- const WithIntlContainer = injectIntl(WithListsCollectionContainer);
428
- export default WithIntlContainer;