|
1 |
| -import React from 'react'; |
| 1 | +import React, {PureComponent} from 'react'; |
2 | 2 | import PropTypes from 'prop-types';
|
3 | 3 | import {Button, Dialog} from '@neos-project/react-ui-components';
|
4 | 4 |
|
5 |
| -const ConfirmationDialog = ({isOpen, translate, close, send}) => { |
6 |
| - return ( |
7 |
| - <Dialog |
8 |
| - isOpen={isOpen} |
9 |
| - title={translate('Psmb.Newsletter:Main:js.testConfirmationTitle')} |
10 |
| - onRequestClose={close} |
11 |
| - actions={[ |
12 |
| - <Button onClick={close} style="clean">{translate('Neos.Neos:Main:cancel')}</Button>, |
13 |
| - <Button onClick={send} style="brand">{translate('Psmb.Newsletter:Main:js.send')}</Button> |
14 |
| - ]} |
15 |
| - > |
16 |
| - <div style={{padding: '16px'}}>{translate('Psmb.Newsletter:Main:js.confirmationDescription')}</div> |
17 |
| - </Dialog> |
18 |
| - ); |
19 |
| -}; |
20 |
| -ConfirmationDialog.propTypes = { |
21 |
| - isOpen: PropTypes.bool, |
22 |
| - translate: PropTypes.func.isRequired, |
23 |
| - close: PropTypes.func.isRequired, |
24 |
| - send: PropTypes.func.isRequired |
| 5 | +class ConfirmationDialog extends PureComponent { |
| 6 | + static propTypes = { |
| 7 | + isOpen: PropTypes.bool, |
| 8 | + translate: PropTypes.func.isRequired, |
| 9 | + close: PropTypes.func.isRequired, |
| 10 | + send: PropTypes.func.isRequired, |
| 11 | + subscription: PropTypes.string.isRequired, |
| 12 | + dataSourceAdditionalArguments: PropTypes.object |
| 13 | + }; |
| 14 | + |
| 15 | + state = { |
| 16 | + isLoading: false, |
| 17 | + subscribers: [] |
| 18 | + }; |
| 19 | + |
| 20 | + componentDidUpdate(prevProps) { |
| 21 | + if (this.props.subscription !== prevProps.subscription || (prevProps.isOpen === false && this.props.isOpen === true)) { |
| 22 | + this.fetchPreview(); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + fetchPreview() { |
| 27 | + if (this.props.subscription && this.props.isOpen) { |
| 28 | + this.setState({isLoading: true, subscribers: []}); |
| 29 | + const dataSourceAdditionalArguments = this.props.dataSourceAdditionalArguments; |
| 30 | + const data = new URLSearchParams(); |
| 31 | + if (dataSourceAdditionalArguments) { |
| 32 | + Object.keys(dataSourceAdditionalArguments).forEach(option => { |
| 33 | + data.set('dataSourceAdditionalArguments[' + option + ']', dataSourceAdditionalArguments[option]); |
| 34 | + }); |
| 35 | + } |
| 36 | + fetch(`/newsletter/preview?subscription=${this.props.subscription}&${data.toString()}`, { |
| 37 | + credentials: 'include' |
| 38 | + }) |
| 39 | + .then(response => response.json()) |
| 40 | + .then(subscribers => { |
| 41 | + this.setState({subscribers, isLoading: false}); |
| 42 | + }); |
| 43 | + } |
| 44 | + } |
| 45 | + render() { |
| 46 | + const {isOpen, translate, close, send} = this.props; |
| 47 | + |
| 48 | + const keys = this.state.subscribers[0] ? Object.keys(this.state.subscribers[0]) : []; |
| 49 | + return ( |
| 50 | + <Dialog |
| 51 | + isOpen={isOpen} |
| 52 | + title={translate('Psmb.Newsletter:Main:js.confirmationTitle')} |
| 53 | + onRequestClose={close} |
| 54 | + actions={[ |
| 55 | + <Button onClick={close} style="clean">{translate('Neos.Neos:Main:cancel')}</Button>, |
| 56 | + <Button onClick={send} style="brand">{translate('Psmb.Newsletter:Main:js.send')}</Button> |
| 57 | + ]} |
| 58 | + > |
| 59 | + <div style={{padding: '16px'}}> |
| 60 | + <div>{translate('Psmb.Newsletter:Main:js.confirmationDescription')}</div> |
| 61 | + {this.state.isLoading ? translate('Psmb.Newsletter:Main:js.loading') : ( |
| 62 | + <div> |
| 63 | + <div style={{padding: '16px 0'}}>{translate('Psmb.Newsletter:Main:js.recepients')}: <strong>{this.state.subscribers.length}</strong></div> |
| 64 | + <table> |
| 65 | + <tr>{keys.map(key => <th>{key}</th>)}</tr> |
| 66 | + {this.state.subscribers.map(subscriber => { |
| 67 | + return <tr>{keys.map(key => <td>{subscriber[key]}</td>)}</tr>; |
| 68 | + })} |
| 69 | + </table> |
| 70 | + </div> |
| 71 | + )} |
| 72 | + </div> |
| 73 | + </Dialog> |
| 74 | + ); |
| 75 | + } |
25 | 76 | };
|
26 | 77 |
|
27 | 78 | export default ConfirmationDialog;
|
0 commit comments