@capillarytech/creatives-library 2.0.44 → 2.0.45

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 (87) hide show
  1. package/components/CmsTemplatesComponent/index.js +1 -0
  2. package/components/FormBuilder/_formBuilder.scss +2 -14
  3. package/components/FormBuilder/index.js +106 -93
  4. package/components/Header/index.js +41 -0
  5. package/components/Header/messages.js +13 -0
  6. package/components/Header/tests/index.test.js +10 -0
  7. package/components/PreviewSideBar/index.js +7 -3
  8. package/components/TemplatePreview/index.js +6 -7
  9. package/config/app.js +2 -2
  10. package/containers/Cap/index.js +1 -1
  11. package/containers/Cap/selectors.js +11 -1
  12. package/containers/CreativesContainer/SlideBoxContent.js +5 -1
  13. package/containers/CreativesContainer/SlideBoxFooter.js +5 -9
  14. package/containers/CreativesContainer/index.js +56 -12
  15. package/containers/Email/actions.js +2 -3
  16. package/containers/Email/index.js +32 -26
  17. package/containers/Email/sagas.js +1 -0
  18. package/containers/EmailWrapper/index.js +43 -13
  19. package/containers/EmailWrapper/messages.js +8 -0
  20. package/containers/MobilePush/Create/index.js +1 -1
  21. package/containers/Sms/Create/actions.js +4 -4
  22. package/containers/Sms/Create/index.js +28 -17
  23. package/containers/Sms/Create/sagas.js +1 -2
  24. package/containers/Sms/Edit/actions.js +4 -5
  25. package/containers/Sms/Edit/index.js +31 -5
  26. package/containers/Sms/Edit/sagas.js +1 -3
  27. package/containers/Templates/actions.js +3 -1
  28. package/containers/Templates/index.js +219 -84
  29. package/containers/Templates/messages.js +8 -0
  30. package/containers/Templates/sagas.js +16 -4
  31. package/containers/Templates/selectors.js +2 -0
  32. package/containers/TemplatesV2/index.js +9 -4
  33. package/containers/WeChat/MapTemplates/_mapTemplates.scss +8 -0
  34. package/containers/WeChat/MapTemplates/actions.js +52 -0
  35. package/containers/WeChat/MapTemplates/constants.js +28 -0
  36. package/containers/WeChat/MapTemplates/index.js +1401 -0
  37. package/containers/WeChat/MapTemplates/messages.js +73 -0
  38. package/containers/WeChat/MapTemplates/reducer.js +74 -0
  39. package/containers/WeChat/MapTemplates/sagas.js +86 -0
  40. package/containers/WeChat/MapTemplates/selectors.js +25 -0
  41. package/containers/WeChat/MapTemplates/tests/actions.test.js +18 -0
  42. package/containers/WeChat/MapTemplates/tests/index.test.js +10 -0
  43. package/containers/WeChat/MapTemplates/tests/reducer.test.js +9 -0
  44. package/containers/WeChat/MapTemplates/tests/sagas.test.js +15 -0
  45. package/containers/WeChat/MapTemplates/tests/selectors.test.js +10 -0
  46. package/containers/WeChat/RichmediaTemplates/Create/_createRichmedia.scss +13 -0
  47. package/containers/WeChat/RichmediaTemplates/Create/actions.js +35 -0
  48. package/containers/WeChat/RichmediaTemplates/Create/constants.js +14 -0
  49. package/containers/WeChat/RichmediaTemplates/Create/index.js +679 -0
  50. package/containers/WeChat/RichmediaTemplates/Create/messages.js +13 -0
  51. package/containers/WeChat/RichmediaTemplates/Create/reducer.js +49 -0
  52. package/containers/WeChat/RichmediaTemplates/Create/sagas.js +44 -0
  53. package/containers/WeChat/RichmediaTemplates/Create/selectors.js +32 -0
  54. package/containers/WeChat/RichmediaTemplates/Create/tests/actions.test.js +18 -0
  55. package/containers/WeChat/RichmediaTemplates/Create/tests/index.test.js +10 -0
  56. package/containers/WeChat/RichmediaTemplates/Create/tests/reducer.test.js +9 -0
  57. package/containers/WeChat/RichmediaTemplates/Create/tests/sagas.test.js +15 -0
  58. package/containers/WeChat/RichmediaTemplates/Create/tests/selectors.test.js +10 -0
  59. package/containers/WeChat/RichmediaTemplates/Create/testschema.js +379 -0
  60. package/containers/WeChat/RichmediaTemplates/Edit/actions.js +20 -0
  61. package/containers/WeChat/RichmediaTemplates/Edit/constants.js +10 -0
  62. package/containers/WeChat/RichmediaTemplates/Edit/index.js +98 -0
  63. package/containers/WeChat/RichmediaTemplates/Edit/messages.js +13 -0
  64. package/containers/WeChat/RichmediaTemplates/Edit/reducer.js +28 -0
  65. package/containers/WeChat/RichmediaTemplates/Edit/sagas.js +32 -0
  66. package/containers/WeChat/RichmediaTemplates/Edit/selectors.js +25 -0
  67. package/containers/WeChat/RichmediaTemplates/Edit/tests/actions.test.js +18 -0
  68. package/containers/WeChat/RichmediaTemplates/Edit/tests/index.test.js +10 -0
  69. package/containers/WeChat/RichmediaTemplates/Edit/tests/reducer.test.js +9 -0
  70. package/containers/WeChat/RichmediaTemplates/Edit/tests/sagas.test.js +15 -0
  71. package/containers/WeChat/RichmediaTemplates/Edit/tests/selectors.test.js +10 -0
  72. package/containers/WeChat/RichmediaTemplates/View/actions.js +15 -0
  73. package/containers/WeChat/RichmediaTemplates/View/constants.js +7 -0
  74. package/containers/WeChat/RichmediaTemplates/View/index.js +45 -0
  75. package/containers/WeChat/RichmediaTemplates/View/messages.js +13 -0
  76. package/containers/WeChat/RichmediaTemplates/View/reducer.js +23 -0
  77. package/containers/WeChat/RichmediaTemplates/View/sagas.js +11 -0
  78. package/containers/WeChat/RichmediaTemplates/View/selectors.js +25 -0
  79. package/containers/WeChat/RichmediaTemplates/View/tests/actions.test.js +18 -0
  80. package/containers/WeChat/RichmediaTemplates/View/tests/index.test.js +10 -0
  81. package/containers/WeChat/RichmediaTemplates/View/tests/reducer.test.js +9 -0
  82. package/containers/WeChat/RichmediaTemplates/View/tests/sagas.test.js +15 -0
  83. package/containers/WeChat/RichmediaTemplates/View/tests/selectors.test.js +10 -0
  84. package/index.js +3 -3
  85. package/package.json +1 -1
  86. package/routes.js +53 -13
  87. package/services/api.js +6 -1
@@ -0,0 +1,1401 @@
1
+ /*
2
+ *
3
+ * WeChat
4
+ *
5
+ */
6
+
7
+ import PropTypes from 'prop-types';
8
+
9
+ import React from 'react';
10
+ import { connect } from 'react-redux';
11
+ import { bindActionCreators } from 'redux';
12
+ import Helmet from 'react-helmet';
13
+ import { injectIntl, intlShape } from 'react-intl';
14
+ import { createStructuredSelector } from 'reselect';
15
+ import { Row, Col, Spin } from 'antd';
16
+ import _ from 'lodash';
17
+ import { makeSelectTemplates } from '../../Templates/selectors';
18
+ import { makeSelectMetaEntities, makeSelectFetchingSchema } from '../../Cap/selectors';
19
+ import makeSelectWeChat from './selectors';
20
+ import messages from './messages';
21
+ import * as actions from './actions';
22
+ import FormBuilder from '../../../components/FormBuilder';
23
+ import { UserIsAuthenticated } from '../../../utils/authWrapper';
24
+ import * as globalActions from '../../../containers/Cap/actions';
25
+ import {getMessageObject} from '../../../utils/messageUtils';
26
+ import * as templateActions from '../../Templates/actions';
27
+ // import SlideBox from '../../components/SlideBox';
28
+ // import PageHeader from '../../components/PageHeader';
29
+ import './_mapTemplates.scss';
30
+
31
+ import callNativeEvent from '../../../utils/callNativeEvent';
32
+
33
+ // const Option = Select.Option;
34
+
35
+ export class MapTemplates extends React.Component { // eslint-disable-line react/prefer-stateless-function
36
+ constructor(props) {
37
+ super(props);
38
+ this.state = {
39
+ schema: {},
40
+ baseSchema: {},
41
+ formData: {},
42
+ map: {
43
+ "discard-button": {
44
+ discardValues: this.discardValues,
45
+ },
46
+ "cancel-button": {
47
+ cancelTemplate: this.cancelTemplate,
48
+ },
49
+ "save-button": {
50
+ saveFormData: this.saveFormData,
51
+ },
52
+ "wechat-template": {
53
+ onSelect: this.selectWechatTemplate,
54
+ },
55
+ "wechat-accounts": {
56
+ onSelect: this.selectWechatAccount,
57
+ },
58
+ "template-link": {
59
+ onChange: this.setContent,
60
+ },
61
+ "is-internal-url": {
62
+ onChange: this.internalUrlChecked,
63
+ },
64
+ },
65
+ selectedTemplateTags: [],
66
+ isEdit: false,
67
+ editTemplateData: {},
68
+ isFormValid: true,
69
+ checkValidation: false,
70
+ injectedTags: {},
71
+ isLoading: true,
72
+ showModal: false,
73
+ modalContent: {
74
+ title: "Alert",
75
+ body: "Do you really want to go back? All your temporary changes will be lost!",
76
+ type: 'confirm',
77
+ id: 'template-back-confirm-modal',
78
+ },
79
+ };
80
+
81
+ this.saveFormData = this.saveFormData.bind(this);
82
+ this.onFormDataChange = this.onFormDataChange.bind(this);
83
+ this.setTemplateOptions = this.setTemplateOptions.bind(this);
84
+ this.cancelTemplate = this.cancelTemplate.bind(this);
85
+ this.setSelectedTemplateData = this.setSelectedTemplateData.bind(this);
86
+ this.discardValues = this.discardValues.bind(this);
87
+ this.internalUrlChecked = this.internalUrlChecked.bind(this);
88
+ this.getTagsFromContent = this.getTagsFromContent.bind(this);
89
+ this.onTagSelect = this.onTagSelect.bind(this);
90
+ this.insertAtCursor = this.insertAtCursor.bind(this);
91
+ this.setFormValidity = this.setFormValidity.bind(this);
92
+ this.setContent = this.setContent.bind(this);
93
+ this.prepareWeChatPreviewData = this.prepareWeChatPreviewData.bind(this);
94
+ this.setEditData = this.setEditData.bind(this);
95
+ this.maskTemplateUrl = this.maskTemplateUrl.bind(this);
96
+ this.unMaskTemplateUrl = this.unMaskTemplateUrl.bind(this);
97
+ this.resetState = this.resetState.bind(this);
98
+ this.resetSchema = this.resetSchema.bind(this);
99
+ this.handleFrameTasks = this.handleFrameTasks.bind(this);
100
+ this.getCurrentWindow = this.getCurrentWindow.bind(this);
101
+ this.startLoading = this.startLoading.bind(this);
102
+ this.startTemplateCreation = this.startTemplateCreation.bind(this);
103
+ this.selectWechatTemplate = this.selectWechatTemplate.bind(this);
104
+ this.removeActionButtons = this.removeActionButtons.bind(this);
105
+ this.setWechatAccountOptions = this.setWechatAccountOptions.bind(this);
106
+ this.selectWechatAccount = this.selectWechatAccount.bind(this);
107
+ this.moveToTemplates = this.moveToTemplates.bind(this);
108
+ this.injectMessages = this.injectMessages.bind(this);
109
+ this.hideSchemaElementsByModule = this.hideSchemaElementsByModule.bind(this);
110
+
111
+ this.injectEvents = this.injectEvents.bind(this);
112
+ this.injectSections = this.injectSections.bind(this);
113
+ this.injectContainer = this.injectContainer.bind(this);
114
+ this.injectParentSection = this.injectParentSection.bind(this);
115
+ this.injectColLabelSection = this.injectColLabelSection.bind(this);
116
+ this.injectMultiColSection = this.injectMultiColSection.bind(this);
117
+ this.getMappedEvent = this.getMappedEvent.bind(this);
118
+ this.showNext = this.showNext.bind(this);
119
+ this.updateSchema = false;
120
+ this.fetchedWeCrmAccount = false;
121
+ this.isAccountChange = false;
122
+ }
123
+
124
+ componentWillMount() {
125
+ const query = {
126
+ layout: 'WECHAT',
127
+ type: 'LAYOUT',
128
+ context: this.props.location.query.type === 'embedded' ? this.props.location.query.module : 'default',
129
+ };
130
+ this.props.globalActions.fetchSchemaForEntity(query);
131
+ this.props.actions.getWeCrmAccounts();
132
+ if (this.props.params.id) {
133
+ this.props.actions.getTemplateDetails(this.props.params.id);
134
+ this.setState({isEdit: true});
135
+ }
136
+ console.log("Mount Params ", this.props.Templates.selectedWeChatAccount, this.props.location.query.type);
137
+ if (this.props.Templates.selectedWeChatAccount || (this.props.location.query.type === 'embedded' && this.props.location.query.module === 'loyalty')) {
138
+ if (this.props.Templates.selectedWeChatAccount) {
139
+ const params = {
140
+ name: '',
141
+ sortBy: 'Most Recent',
142
+ wecrmId: this.props.Templates.selectedWeChatAccount.configs.wecrm_app_id,
143
+ wecrmToken: this.props.Templates.selectedWeChatAccount.configs.wecrm_token,
144
+ originalId: this.props.Templates.selectedWeChatAccount.sourceAccountIdentifier,
145
+ };
146
+ this.props.actions.getDefaultWeChatTemplates('wechat', params);
147
+ } else {
148
+ this.setWechatAccountOptions(this.props.WeChat.weCrmAccounts);
149
+ }
150
+ window.addEventListener("message", this.handleFrameTasks);
151
+ } else if (this.props.location.query.type === 'embedded' && this.props.location.query.module === 'outbound') {
152
+ console.log('Inside embedded Outbound');
153
+ } else {
154
+ const module = this.props.location.query.module ? this.props.location.query.module : 'default';
155
+ const type = this.props.location.query.type;
156
+ this.props.router.push({
157
+ pathname: `/wechat`,
158
+ query: type === 'embedded' ? {type: 'embedded', module} : {module},
159
+ });
160
+ }
161
+ }
162
+
163
+ componentDidMount() {
164
+ console.log('added listener create,', this.props);
165
+ const query = {
166
+ layout: 'WECHAT',
167
+ type: 'LAYOUT',
168
+ context: this.props.location.query.type === 'embedded' ? this.props.location.query.module : 'default',
169
+ embedded: this.props.location.query.type === 'embedded' ? this.props.location.query.type : 'full',
170
+ };
171
+ this.props.globalActions.fetchSchemaForEntity(query);
172
+
173
+ window.addEventListener("message", this.handleFrameTasks);
174
+ }
175
+
176
+ componentWillReceiveProps(nextProps) {
177
+ if (nextProps.isGetFormData) {
178
+ this.getFormData();
179
+ }
180
+ console.log('MetaEntity nextprops ', nextProps.metaEntities);
181
+ if (nextProps.metaEntities && nextProps.metaEntities.layouts && nextProps.metaEntities.layouts.length > 0 && _.isEmpty(this.state.schema) && !nextProps.WeChat.getTemplateDetailsInProgress) {
182
+ this.injectEvents(nextProps.metaEntities.layouts[0].definition);
183
+ const query = {
184
+ layout: 'WECHAT',
185
+ type: 'TAG',
186
+ context: this.props.location.query.type === 'embedded' ? this.props.location.query.module : 'default',
187
+ embedded: this.props.location.query.type === 'embedded' ? this.props.location.query.type : 'full',
188
+ };
189
+ if (this.props.getDefaultTags) { // handling dynamic tags in library mode, but handling static and synamic tags in email and sms.
190
+ query.context = this.props.getDefaultTags;
191
+ }
192
+ this.props.globalActions.fetchSchemaForEntity(query);
193
+ }
194
+ if (nextProps.metaEntities && nextProps.metaEntities.layouts && nextProps.metaEntities.layouts.length > 0 && !_.isEmpty(this.state.schema)) {
195
+ if (nextProps.WeChat.weChatDefaultTemplate && !_.isEqual(nextProps.WeChat.weChatDefaultTemplate, this.props.WeChat.weChatDefaultTemplate)) {
196
+ this.injectEvents(this.state.schema);
197
+ if (nextProps.location.query.type === 'embedded' && nextProps.location.query.module === 'loyalty') {
198
+ this.setTemplateOptions(nextProps.WeChat.weChatTemplates);
199
+ } else {
200
+ this.setTemplateOptions(nextProps.WeChat.weChatDefaultTemplate);
201
+ }
202
+ }
203
+ if ((this.props.location.query.type === 'embedded' && (this.props.location.query.module === 'loyalty' || this.props.location.query.module === 'outbound')) && !_.isEmpty(nextProps.WeChat.templateDetails) && !_.isEqual(this.props.WeChat.templateDetails, this.state.editTemplateData) && this.isAccountChange === false) {
204
+ this.setState({editTemplateData: nextProps.WeChat.templateDetails}, () => {
205
+ if ( !_.isEmpty(this.state.baseSchema)) {
206
+ this.setEditData();
207
+ } else {
208
+ this.setState({baseSchema: _.cloneDeep(this.state.schema)}, () => {
209
+ this.setEditData();
210
+ });
211
+ }
212
+ });
213
+ }
214
+ // has to be un commented when wechat edit is implemented in embedded mode.
215
+ // if (nextProps.location.query.type === "embedded" && nextProps.location.query.module === "library" && nextProps.templateData && nextProps.templateData.versions) { // Edit of already added message in library mode
216
+ // this.setState({editTemplateData: nextProps.templateData}, () => {
217
+ // this.setEditData();
218
+ // });
219
+ // }
220
+ if (nextProps.params.id && !_.isEqual(nextProps.params.id, this.props.params.id)) {
221
+ this.setState({isEdit: true});
222
+ }
223
+ if (this.state.isEdit && _.isEmpty(this.state.editTemplateData) && !_.isEmpty(nextProps.WeChat.weChatTemplates)) {
224
+ const templateIndex = _.findIndex(nextProps.WeChat.weChatTemplates, {_id: nextProps.params.id});
225
+ if (templateIndex > -1) {
226
+ this.setState({editTemplateData: nextProps.WeChat.weChatTemplates[templateIndex]}, () => {
227
+ if (_.isEmpty(this.state.baseSchema)) {
228
+ this.setState({ baseSchema: _.cloneDeep(this.state.schema)}, () => {
229
+ this.setEditData();
230
+ });
231
+ } else {
232
+ this.setEditData();
233
+ }
234
+ });
235
+ }
236
+ }
237
+ if (nextProps.location.query.type === 'embedded') {
238
+ if (this.props.location.query.module !== 'loyalty') {
239
+ this.removeActionButtons();
240
+ }
241
+ }
242
+
243
+ if (nextProps.WeChat.createResponse && nextProps.WeChat.createResponse.templateId) {
244
+ // this.resetSchema();
245
+ let message;
246
+ if (this.state.isEdit) {
247
+ message = getMessageObject('success', this.props.intl.formatMessage(messages.wechatEditSuccess), true);
248
+ } else {
249
+ message = getMessageObject('success', this.props.intl.formatMessage(messages.wechatCreateSuccess), true);
250
+ }
251
+ this.props.globalActions.addMessageToQueue(message);
252
+ this.props.actions.clearCreateResponse();
253
+ const module = this.props.location.query.module ? this.props.location.query.module : 'default';
254
+ const type = this.props.location.query.type;
255
+ this.props.router.push({
256
+ pathname: `/wechat`,
257
+ query: type === 'embedded' ? {type: 'embedded', module} : {module},
258
+ });
259
+ }
260
+ if (nextProps.WeChat.createTemplateError && !_.isEqual(nextProps.WeChat.createTemplateError, this.props.WeChat.createTemplateError)) {
261
+ const message = getMessageObject('error', this.props.intl.formatMessage(messages.somethingWentWrong), true);
262
+ this.props.globalActions.addMessageToQueue(message);
263
+ }
264
+
265
+ // console.log('All Dont Hail ', query);
266
+ // this.injectEvents(nextProps.metaEntities.layouts[0].definition);
267
+ // const query = {
268
+ // layout: 'WECHAT',
269
+ // type: 'TAG',
270
+ // context: this.props.location.query.type === 'embedded' ? this.props.location.query.module : 'default',
271
+ // };
272
+ // this.props.globalActions.fetchSchemaForEntity(query);
273
+ if (this.props.location.query.type === 'embedded') {
274
+ this.showNext();
275
+ }
276
+
277
+ if (!this.fetchedWeCrmAccount && !_.isEmpty(this.props.WeChat.weCrmAccounts) && (this.props.location.query.type === 'embedded' && this.props.location.query.module === 'loyalty')) {
278
+ this.injectEvents(this.state.schema);
279
+ this.setWechatAccountOptions(nextProps.WeChat.weCrmAccounts);
280
+ this.fetchedWeCrmAccount = true;
281
+ }
282
+ if (!_.isEmpty(nextProps.WeChat.selectedWeChatAccount) && !_.isEqual(this.props.WeChat.selectedWeChatAccount, nextProps.WeChat.selectedWeChatAccount) && (this.props.location.query.type === 'embedded')) {
283
+ const params = {
284
+ name: '',
285
+ sortBy: 'Most Recent',
286
+ wecrmId: nextProps.WeChat.selectedWeChatAccount.configs.wecrm_app_id,
287
+ wecrmToken: nextProps.WeChat.selectedWeChatAccount.configs.wecrm_token,
288
+ originalId: nextProps.WeChat.selectedWeChatAccount.sourceAccountIdentifier,
289
+ };
290
+ this.props.actions.getDefaultWeChatTemplates('wechat', params);
291
+ }
292
+
293
+ if (_.isEmpty(nextProps.WeChat.selectedWeChatAccount) && (this.props.location.query.type === 'embedded')) {
294
+ let selectedAccount = {};
295
+ _.forEach(nextProps.WeChat.weCrmAccounts, (account) => {
296
+ if (account.configs && account.configs.is_wecrm_enabled) {
297
+ if (account.sourceAccountIdentifier === this.props.location.query.source_account_id) {
298
+ selectedAccount = account;
299
+ }
300
+ }
301
+ });
302
+ if (!_.isEmpty(selectedAccount)) {
303
+ this.props.actions.setWeChatAccount(selectedAccount);
304
+ }
305
+ }
306
+
307
+ if (nextProps.metaEntities && nextProps.metaEntities.tags && nextProps.metaEntities.tags.custom && this.props.metaEntities.tags !== nextProps.metaEntities.tags) {
308
+ let injectedTags = _.cloneDeep(this.state.injectedTags);
309
+ delete injectedTags["Registration custom fields"];
310
+ delete injectedTags["Store custom fields"];
311
+ delete injectedTags["Transaction custom fields"];
312
+ injectedTags = _.merge({}, injectedTags, nextProps.metaEntities.tags.custom);
313
+ console.log('new injected tags', nextProps.metaEntities.tags.custom);
314
+ if (!_.has(nextProps.metaEntities.tags.custom, "Registration custom fields")) {
315
+ delete injectedTags["Registration custom fields"];
316
+ }
317
+ if (!_.has(nextProps.metaEntities.tags.custom, "Store custom fields")) {
318
+ delete injectedTags["Store custom fields"];
319
+ }
320
+ if (!_.has(nextProps.metaEntities.tags.custom, "Transaction custom fields")) {
321
+ delete injectedTags["Transaction custom fields"];
322
+ }
323
+ this.setState({injectedTags});
324
+ }
325
+
326
+ // if (this.props.location.query.type.type === 'embedded' && this.props.location.query.module === 'loyalty' && this.state.formData['wechat-template'] !== '') {
327
+ // }
328
+ if (!nextProps.WeChat.fetchingDefaultTemplates && nextProps.location.query.module === 'outbound' && nextProps.params.id !== undefined) {
329
+ this.setState({ isLoading: false});
330
+ }
331
+ if (((!nextProps.WeChat.fetchingDefaultTemplates && !_.isEqual(nextProps.WeChat.fetchingDefaultTemplates, this.props.WeChat.fetchingDefaultTemplates) && nextProps.location.query.module !== 'loyalty') || (nextProps.location.query.module === 'loyalty'))) {
332
+ this.setState({ isLoading: false});
333
+ }
334
+
335
+ if (nextProps.WeChat.fetchingDefaultTemplates && nextProps.location.query.module === 'loyalty' && nextProps.params.id === undefined) {
336
+ this.setState({ isLoading: true});
337
+ }
338
+ }
339
+ }
340
+
341
+ componentWillUnmount() {
342
+ console.log('removed listener create');
343
+ window.removeEventListener("message", this.handleFrameTasks);
344
+ this.props.templateActions.resetTemplate();
345
+ this.resetState();
346
+ }
347
+
348
+ onFormDataChange(formData) {
349
+ let selectedWechatTemplateIndex = -1;
350
+ let selectedTemplate = {};
351
+ let selectedTemplateData = {};
352
+ if (this.props.location.query.type === 'embedded' && (this.props.location.query.module === 'loyalty')) {
353
+ if (this.props.params.id && _.isEmpty(this.props.WeChat.weChatTemplates) && _.isEqual(formData['wechat-template'], this.state.formData['wechat-template'])) {
354
+ console.log('In If', formData['wechat-template']);
355
+ selectedTemplate = {
356
+ template_id: this.props.WeChat.templateDetails.versions.base.template_id,
357
+ title: this.props.WeChat.templateDetails.versions.base.Title,
358
+ content: this.props.WeChat.templateDetails.versions.base.content,
359
+ };
360
+ selectedTemplateData = this.props.WeChat.templateDetails;
361
+ } else {
362
+ console.log('In Else', formData['wechat-template']);
363
+ _.forEach(this.props.WeChat.weChatTemplates, (template) => {
364
+ if (template.versions.base.template_id === formData['wechat-template']) {
365
+ selectedTemplate = {
366
+ template_id: template.versions.base.template_id,
367
+ title: template.versions.base.Title,
368
+ content: template.versions.base.content,
369
+ };
370
+ this.isAccountChange = false;
371
+ selectedTemplateData = template;
372
+ }
373
+ });
374
+ }
375
+ } else if (this.state.isEdit && this.state.editTemplateData.versions) {
376
+ console.log('this.state.editTemplateData', this.state.editTemplateData);
377
+ selectedTemplateData = _.cloneDeep(this.state.editTemplateData);
378
+ selectedTemplate = {
379
+ template_id: selectedTemplateData.versions.base.template_id,
380
+ title: selectedTemplateData.versions.base.Title,
381
+ content: selectedTemplateData.versions.base.content,
382
+ };
383
+ } else if (!this.state.isEdit) {
384
+ selectedWechatTemplateIndex = _.findIndex(this.props.WeChat.weChatDefaultTemplate, {template_id: formData['wechat-template']});
385
+ selectedTemplate = this.props.WeChat.weChatDefaultTemplate[selectedWechatTemplateIndex];
386
+ }
387
+
388
+ if (!_.isEqual(formData['wechat-template'], this.state.formData['wechat-template'])) {
389
+ this.updateSchema = true;
390
+ }
391
+ if (this.isAccountChange) {
392
+ selectedTemplateData = {};
393
+ }
394
+ this.setState({formData, editTemplateData: selectedTemplateData}, () => {
395
+ if (!_.isEmpty(this.state.editTemplateData) && this.props.location.query.type === 'embedded' && this.props.location.query.module === 'loyalty') {
396
+ this.setEditData();
397
+ }
398
+ if (this.props.WeChat.selectedWeChatAccount && !_.isEqual(this.props.WeChat.selectedWeChatAccount.name, formData['wechat-accounts']) && this.props.location.query.type === 'embedded' && this.props.location.query.module === 'loyalty') {
399
+ let selectedAccount = {};
400
+ _.forEach(this.props.WeChat.weCrmAccounts, (account) => {
401
+ if (account.configs && account.configs.is_wecrm_enabled) {
402
+ if (account.name === formData['wechat-accounts']) {
403
+ selectedAccount = account;
404
+ }
405
+ }
406
+ });
407
+ this.props.actions.setWeChatAccount(selectedAccount);
408
+ this.setSelectedTemplateData({});
409
+ }
410
+ if ((selectedWechatTemplateIndex !== -1 || this.state.isEdit) && !_.isEmpty(selectedTemplate)) {
411
+ let templateData = {};
412
+ if (this.state.isEdit && !_.isEmpty(this.state.editTemplateData)) {
413
+ templateData = {
414
+ template_id: this.state.editTemplateData.versions.base.template_id,
415
+ title: this.state.editTemplateData.versions.base.Title,
416
+ content: this.state.editTemplateData.versions.base.content,
417
+ };
418
+ if (this.props.location.query.type === 'embedded' && this.props.location.query.module === 'loyalty') {
419
+ if (_.isEqual(this.props.WeChat.selectedWeChatAccount.name, formData['wechat-accounts'])) {
420
+ this.setSelectedTemplateData(selectedTemplate);
421
+ }
422
+ } else {
423
+ const templateTags = this.getTagsFromContent(templateData.content);
424
+ this.prepareWeChatPreviewData(templateData, templateTags);
425
+ }
426
+ } else if (!this.updateSchema) {
427
+ const templateTags = this.getTagsFromContent(selectedTemplate.content);
428
+ this.prepareWeChatPreviewData(selectedTemplate, templateTags);
429
+ } else {
430
+ this.updateSchema = false;
431
+ this.setSelectedTemplateData(selectedTemplate);
432
+ }
433
+ } else {
434
+ let selectedAccount = {};
435
+ _.forEach(this.props.WeChat.weCrmAccounts, (account) => {
436
+ if (account.configs && account.configs.is_wecrm_enabled) {
437
+ if (account.name === formData['wechat-accounts']) {
438
+ selectedAccount = account;
439
+ }
440
+ }
441
+ });
442
+ this.props.actions.setWeChatAccount(selectedAccount);
443
+ }
444
+ });
445
+ }
446
+
447
+ onTagSelect(data, currentTab, value) {
448
+ const editorId = (value.id).replace('tagList', 'template');
449
+ this.insertAtCursor(document.getElementById(editorId), `{{${data}}}`);
450
+ document.getElementById(editorId).focus();
451
+ }
452
+
453
+ getFormData(e) {
454
+ console.log('posting final result', this.state.formData, this.state.editTemplateData);
455
+ const selectedWeChatAccount = (!_.isEmpty(this.props.Templates.selectedWeChatAccount) ? this.props.Templates.selectedWeChatAccount : this.props.WeChat.selectedWeChatAccount);
456
+ const data = {
457
+ template_id: this.state.editTemplateData.versions.base.template_id,
458
+ Title: this.state.editTemplateData.versions.base.Title,
459
+ Tag: [],
460
+ url: this.state.formData['is-internal-url'] ? this.maskTemplateUrl(this.state.formData['template-link']) : this.state.formData['template-link'],
461
+ TopColor: "#000000",
462
+ data: {},
463
+ preview: '',
464
+ BrandId: (selectedWeChatAccount.configs.brand_id ? selectedWeChatAccount.configs.brand_id : ''),
465
+ OriginalId: selectedWeChatAccount.sourceAccountIdentifier,
466
+ content: this.state.editTemplateData.versions.base.content,
467
+ isInternalUrl: this.state.formData['is-internal-url'],
468
+ creative_template_id: this.state.editTemplateData._id,
469
+ touser: "{{wechat_open_id}}",
470
+ };
471
+ _.forEach(this.state.selectedTemplateTags, (templateTag) => {
472
+ const tagName = templateTag.substr(0, templateTag.indexOf('.'));
473
+ let newTagname = tagName;
474
+ data.Tag.push(tagName);
475
+ if (tagName.match(/first/i)) {
476
+ newTagname = "Header";
477
+ }
478
+ if (tagName.match(/remark/i)) {
479
+ newTagname = "Footer";
480
+ }
481
+ data.data[tagName] = {};
482
+ data.data[tagName] = {
483
+ value: this.state.formData[`template-${newTagname}`],
484
+ color: "#00000",
485
+ };
486
+ });
487
+ const response = {
488
+ action: "getFormData",
489
+ value: data,
490
+ validity: this.state.isFormValid,
491
+ };
492
+ console.log('response ', response);
493
+ this.setState({checkValidation: true});
494
+ if (this.props.location.query.module === 'library') {
495
+ this.props.getLibraryFormData(response);
496
+ } else {
497
+ e.source.postMessage(JSON.stringify(response), e.origin);
498
+ }
499
+ }
500
+
501
+ getCurrentWindow(e) {
502
+ console.log('in sms create', e);
503
+ const response = {
504
+ action: e.action,
505
+ value: 'edit',
506
+ direction: e.value,
507
+ };
508
+ parent.postMessage(JSON.stringify(response), '*');
509
+ }
510
+
511
+ getTagsFromContent(string) {
512
+ const foundTags = [];
513
+ const rxp = /{{([^}]+)}}/g;
514
+ let curMatch;
515
+ /* eslint-disable */
516
+ while ( curMatch = rxp.exec( string ) ) {
517
+ foundTags.push( curMatch[1] );
518
+ }
519
+ return foundTags;
520
+ }
521
+
522
+ startLoading(ifEdit) {
523
+ console.log('startLoading', ifEdit);
524
+ if (ifEdit) {
525
+ this.setState({loading: true});
526
+ }
527
+ }
528
+
529
+ handleFrameTasks = (e) => {
530
+ // console.log('data listened to is', e.data);
531
+ const type = e.data;
532
+ // console.log('data type', typeof type);
533
+ if (typeof type === 'object') {
534
+ // console.log('received something', type);
535
+ const action = type.action;
536
+ switch (action) {
537
+ case "startTemplateCreation":
538
+ this.startTemplateCreation(type.value);
539
+ break;
540
+ case "startLoading":
541
+ this.startLoading(type.edit);
542
+ break;
543
+ case "getCurrentWindow":
544
+ this.getCurrentWindow(type);
545
+ break;
546
+ default:
547
+ break;
548
+ }
549
+ } else {
550
+ switch (type) {
551
+ case "getFormData":
552
+ this.getFormData(e);
553
+ break;
554
+ case "startTemplateCreation":
555
+ console.log('Starting to create template');
556
+ //this.getFormData(e);
557
+ break;
558
+ case "moveToTemplates":
559
+ console.log('moving to templates');
560
+ this.moveToTemplates();
561
+ break;
562
+ case "validateContent":
563
+ console.log('validating Content');
564
+ this.validateContent(e);
565
+ break;
566
+ default:
567
+ break;
568
+ }
569
+ }
570
+ }
571
+
572
+ moveToTemplates() {
573
+ console.log('in move to Templates');
574
+ const modalContent = {
575
+ title: "Alert",
576
+ body: "Do you really want to go back? All your temporary changes will be lost!",
577
+ type: 'confirm',
578
+ id: 'template-back-confirm-modal',
579
+ show: true,
580
+ };
581
+ this.setState({modalContent, showModal: true});
582
+ }
583
+
584
+ injectMessages(elem) {
585
+ const temp = elem;
586
+ if (temp.value && messages[temp.value]) {
587
+ temp.value = this.props.intl.formatMessage(messages[temp.value]);
588
+ }
589
+ if (temp.label && messages[temp.label]) {
590
+ temp.label = this.props.intl.formatMessage(messages[temp.label]);
591
+ }
592
+ if (temp.errorMessage && messages[temp.errorMessage]) {
593
+ temp.errorMessage = this.props.intl.formatMessage(messages[temp.errorMessage]);
594
+ }
595
+ if (temp.placeholder && messages[temp.placeholder]) {
596
+ temp.placeholder = this.props.intl.formatMessage(messages[temp.placeholder]);
597
+ }
598
+ return temp;
599
+ }
600
+
601
+ setSelectedTemplateData(weChatData) {
602
+ let schema = _.cloneDeep(this.state.baseSchema);
603
+ if (_.isEmpty(weChatData)) {
604
+ schema.standalone.sections[0].childSections[1].childSections[0].childSections[1].inputFields = [];
605
+ const formData=_.cloneDeep(this.state.formData);
606
+ formData['wechat-template']="";
607
+ this.setState({schema,formData});
608
+ this.isAccountChange = true;
609
+ return;
610
+ }
611
+ console.log('Setting Data ', weChatData, this.state.baseSchema);
612
+ const {map} = _.cloneDeep(this.state);
613
+ const templateTags = this.getTagsFromContent(weChatData.content);
614
+ const formData = _.cloneDeep(this.state.formData);
615
+ _.forEach(templateTags, (tag) => {
616
+ const tagTitle = tag.substr(0, tag.indexOf('.'));
617
+ let title = tagTitle;
618
+
619
+ if (tagTitle.match(/first/i)) {
620
+ title = "Header";
621
+ }
622
+ if (tagTitle.match(/remark/i)) {
623
+ title = "Footer";
624
+ }
625
+
626
+ const colsData = {
627
+ cols: [
628
+ {
629
+ id: `template-label-${title}`,
630
+ metaType: "label",
631
+ value: `${title}`,
632
+ primitive: true,
633
+ type: "div",
634
+ width: 4,
635
+ offset: 0,
636
+ onlyDisplay: true,
637
+ colStyle: {
638
+ flex: 1,
639
+ textAlign: 'left',
640
+ color: '#333333',
641
+ lineHeight: '24px',
642
+ fontSize: '14px',
643
+ fontWeight: '400',
644
+ fontFamily: 'open-sans',
645
+ fontStyle: 'normal',
646
+ textTransform: 'capitalize',
647
+ padding: '1em .833em',
648
+ },
649
+ },
650
+ {
651
+ id: `template-${title}`,
652
+ placeholder: `${title}`,
653
+ type: "input", //Resembles component to be used
654
+ metaType: "text",
655
+ datatype: "string",
656
+ errorMessage: `${title} has invalid content.`,
657
+ required: true,
658
+ fluid: true,
659
+ disabled: (this.props.location.query.type === 'embedded'),
660
+ style: {
661
+ fontSize: '14px',
662
+ fontFamily: 'open-sans',
663
+ fontStyle: 'normal',
664
+ // fontWeight: 600,
665
+ marginBottom: '16px',
666
+ },
667
+ styling: "semantic",
668
+ order: 1,
669
+ width: 15,
670
+ // offSet: 2,
671
+ customComponent: false,
672
+ standalone: true,
673
+ supportedEvents: [
674
+ 'onChange',
675
+ ],
676
+ },
677
+ ],
678
+ };
679
+
680
+ if(this.props.location.query.type !== 'embedded') {
681
+ colsData.cols.push({
682
+ offset: 20,
683
+ id: `tagList-${title}`,
684
+ label: "Tags",
685
+ type: "tag-list", //Resembles component to be used
686
+ metaType: "List",
687
+ datatype: "select",
688
+ required: true,
689
+ fluid: true,
690
+ onlyDisplay: true,
691
+ styling: "semantic",
692
+ order: 1,
693
+ customComponent: true,
694
+ supportedEvents: [
695
+ 'onTagSelect',
696
+ ],
697
+ },);
698
+ }
699
+
700
+ map[`tagList-${title}`] = {
701
+ onTagSelect: this.onTagSelect,
702
+ };
703
+
704
+ // if(!tagTitle.match(/first/i) && !tagTitle.match(/remark/i) && this.props.location.query.type !== 'embedded') {
705
+ // colsData.cols.push({
706
+ // offset: 20,
707
+ // id: `tagList-${title}`,
708
+ // // label: "Tags",
709
+ // type: "tag-list", //Resembles component to be used
710
+ // metaType: "List",
711
+ // datatype: "select",
712
+ // required: true,
713
+ // fluid: true,
714
+ // onlyDisplay: true,
715
+ // styling: "semantic",
716
+ // order: 1,
717
+ // customComponent: true,
718
+ // supportedEvents: [
719
+ // 'onTagSelect',
720
+ // ],
721
+ // },);
722
+
723
+ // map[`tagList-${title}`] = {
724
+ // onTagSelect: this.onTagSelect,
725
+ // };
726
+ // }
727
+ map[`template-${title}`] = {
728
+ onChange: this.setContent,
729
+ };
730
+
731
+ schema.standalone.sections[0].childSections[1].childSections[0].childSections[1].inputFields.push(colsData);
732
+ });
733
+
734
+ schema.standalone.sections[0].childSections[1].childSections[0].childSections[1].inputFields.push(
735
+ {
736
+ cols: [
737
+ {
738
+ id: "template-label-link",
739
+ metaType: "label",
740
+ value: 'Link',
741
+ primitive: true,
742
+ type: "div",
743
+ width: 4,
744
+ offset: 0,
745
+ onlyDisplay: true,
746
+ colStyle: {
747
+ flex: 1,
748
+ textAlign: 'left',
749
+ color: '#333333',
750
+ lineHeight: '24px',
751
+ fontSize: '14px',
752
+ fontWeight: '400',
753
+ fontFamily: 'open-sans',
754
+ fontStyle: 'normal',
755
+ textTransform: 'capitalize',
756
+ padding: '.5em .833em',
757
+ },
758
+ },
759
+ {
760
+ id: 'template-link',
761
+ placeholder: this.props.intl.formatMessage(messages.Link),
762
+ type: "input", //Resembles component to be used
763
+ metaType: "text",
764
+ datatype: "string",
765
+ errorMessage: `Link cannot be empty.`,
766
+ required: true,
767
+ fluid: true,
768
+ disabled: (this.props.location.query.type === 'embedded'),
769
+ style: {
770
+ fontSize: '14px',
771
+ fontFamily: 'open-sans',
772
+ fontStyle: 'normal',
773
+ // fontWeight: 600,
774
+ // marginBottom: '16px',
775
+ },
776
+ styling: "semantic",
777
+ order: 1,
778
+ width: 15,
779
+ // offSet: 6,
780
+ customComponent: false,
781
+ standalone: true,
782
+ supportedEvents: [
783
+ 'onChange',
784
+ ],
785
+ },
786
+ ],
787
+ },
788
+ {
789
+ cols: [
790
+ {
791
+ id: "is-internal-url",
792
+ label: 'Is this internal url',
793
+ standalone: true,
794
+ labelStyle: {
795
+ backgroundColor: "white",
796
+ },
797
+ colStyle: {
798
+ marginBottom: '24px',
799
+ },
800
+ style: {
801
+ textAlign: 'left',
802
+ color: '#262626',
803
+ fontFamily: 'Open-Sans',
804
+ lineHeight: '24px',
805
+ fontSize: '14px',
806
+ padding: '.5833em .833em',
807
+ marginBottom: '24px',
808
+ paddingLeft: '0',
809
+ },
810
+ type: "checkbox",
811
+ disabled: (this.props.location.query.type === 'embedded'),
812
+ metaType: "text",
813
+ datatype: "boolean",
814
+ required: true,
815
+ fluid: true,
816
+ styling: "semantic",
817
+ order: 1,
818
+ width: 10,
819
+ offset: 4,
820
+ onlyDisplay: false,
821
+ customComponent: false,
822
+ supportedEvents: [
823
+ 'onChange',
824
+ ],
825
+ },
826
+ ],
827
+ }
828
+ );
829
+ if (this.state.isEdit) {
830
+ _.forEach(this.state.editTemplateData.versions.base.Tag, (tag) => {
831
+ let tagName = tag;
832
+ if (tag.match(/first/i)) {
833
+ tagName = "Header";
834
+ }
835
+ if (tag.match(/remark/i)) {
836
+ tagName = "Footer";
837
+ }
838
+ formData[`template-${tagName}`] = this.state.editTemplateData.versions.base.data[tag].value;
839
+ });
840
+ if (this.state.editTemplateData.versions.base.url) {
841
+ formData[`template-link`] = this.state.editTemplateData.versions.base.isInternalUrl ? this.unMaskTemplateUrl(this.state.editTemplateData.versions.base.url) : this.state.editTemplateData.versions.base.url;
842
+ }
843
+ if (this.state.editTemplateData.versions.base.isInternalUrl) {
844
+ formData['is-internal-url'] = this.state.editTemplateData.versions.base.isInternalUrl;
845
+ }
846
+ // schema.standalone.sections[0].childSections[1].childSections[1].inputFields[0].cols[0].content = weChatData.content;
847
+ } else {
848
+ // schema.standalone.sections[0].childSections[1].childSections[1].inputFields[0].cols[0].content = weChatData.content;
849
+ }
850
+ // schema.standalone.sections[0].childSections[1].childSections[1].inputFields[0].cols[0].content = weChatData.content;
851
+
852
+ this.setState({schema, map, selectedTemplateTags: templateTags, formData}, () => {
853
+ console.log('Is internal URL ', this.state.schema, formData);
854
+ this.prepareWeChatPreviewData(weChatData, templateTags);
855
+ });
856
+ }
857
+
858
+ resetState() {
859
+ this.setState({
860
+ formData: {},
861
+ schema: {},
862
+ baseSchema: {},
863
+ selectedTemplateTags: [],
864
+ isEdit: false,
865
+ editTemplateData: {},
866
+ isFormValid: true,
867
+ checkValidation: false,
868
+ injectedTags: {},
869
+ isLoading: true,
870
+ });
871
+ }
872
+
873
+ resetSchema() {
874
+ const formData = _.cloneDeep(this.state.formData);
875
+ if (this.state.isEdit) {
876
+ const templateData = {
877
+ template_id: this.state.editTemplateData.versions.base.template_id,
878
+ title: this.state.editTemplateData.versions.base.Title,
879
+ content: this.state.editTemplateData.versions.base.content,
880
+ };
881
+ this.setSelectedTemplateData(templateData);
882
+ } else {
883
+ this.setState({ schema: this.state.baseSchema, formData: {'wechat-template': "" }});
884
+ // this.setState({ schema: this.props.metaEntities.layouts[0].definition });
885
+ }
886
+ }
887
+
888
+ setWechatAccountOptions(weCrmAccounts) {
889
+ const schema = _.cloneDeep(this.state.schema);
890
+ const formData = _.cloneDeep(this.state.formData);
891
+ if (!_.isEmpty(weCrmAccounts)) {
892
+ _.forEach(weCrmAccounts, (account) => {
893
+ if (account.configs && account.configs.is_wecrm_enabled) {
894
+ schema.standalone.sections[0].childSections[1].childSections[0].childSections[0].inputFields[0].options.push({
895
+ key: account.uuid,
896
+ label: account.name,
897
+ value: account.name,
898
+ });
899
+ }
900
+
901
+ if (this.props.WeChat.templateDetails && this.props.WeChat.templateDetails.versions.base.OriginalId === account.sourceAccountIdentifier) {
902
+ formData['wechat-accounts'] = account.name;
903
+ this.props.actions.setWeChatAccount(account);
904
+ }
905
+ });
906
+ this.setState({schema, formData, baseSchema: schema});
907
+ }
908
+ }
909
+
910
+ selectWechatAccount(value, option) {
911
+ // console.log('Test ', value, option);
912
+ // templateActions.setWeChatAccount();
913
+ }
914
+
915
+ setTemplateOptions(weChatData) {
916
+ console.log('Set Templates ', weChatData);
917
+ //const {schema, baseSchema} = this.state;
918
+ const schema = _.cloneDeep(this.state.schema);
919
+ const baseSchema = _.cloneDeep(this.state.schema);
920
+ const baseSchemaTemplateOptionEmpty = (!_.isEmpty(baseSchema) && _.isEmpty(baseSchema.standalone.sections[0].childSections[1].childSections[0].childSections[0].inputFields[1].options));
921
+ schema.standalone.sections[0].childSections[1].childSections[0].childSections[0].inputFields[1].options = [];
922
+ if (!_.isEmpty(baseSchema)) {
923
+ baseSchema.standalone.sections[0].childSections[1].childSections[0].childSections[0].inputFields[1].options = [];
924
+
925
+ }
926
+ _.forEach(weChatData, (template) => {
927
+ schema.standalone.sections[0].childSections[1].childSections[0].childSections[0].inputFields[1].options.push({
928
+ key: (template.versions) ? template.versions.base.template_id : template.template_id,
929
+ label: (template.versions) ? template.name : `${template.title} : ${template.template_id}`,
930
+ value: (template.versions) ? template.versions.base.template_id : template.template_id,
931
+ });
932
+ if ((!_.isEmpty(baseSchema) && this.props.location.query.module === 'loyalty') || (baseSchemaTemplateOptionEmpty)) {
933
+ baseSchema.standalone.sections[0].childSections[1].childSections[0].childSections[0].inputFields[1].options.push({
934
+ key: (template.versions) ? template.versions.base.template_id : template.template_id,
935
+ label: (template.versions) ? template.name : `${template.title} : ${template.template_id}`,
936
+ value: (template.versions) ? template.versions.base.template_id : template.template_id,
937
+ });
938
+ }
939
+ });
940
+ this.setState({schema, baseSchema: _.cloneDeep(baseSchema)}, () => {
941
+ console.log('setTemplateOptions', this.state.baseSchema, schema);
942
+ if (_.isEmpty(this.state.baseSchema)) {
943
+ this.setState({baseSchema: _.cloneDeep(schema)});
944
+ }
945
+ });
946
+ }
947
+
948
+ setFormValidity(isFormValid) {
949
+ this.setState({isFormValid});
950
+ }
951
+
952
+ setContent(content) {
953
+ console.log('Template content changed to ', content);
954
+ }
955
+
956
+ setEditData() {
957
+ console.log('Inside Edit Data ', this.state.editTemplateData);
958
+ const formData = _.cloneDeep(this.state.formData);
959
+ const baseSchema = _.cloneDeep(this.state.baseSchema);
960
+ if (_.isEmpty(baseSchema)) {
961
+ return;
962
+ }
963
+ formData['wechat-template'] = this.state.editTemplateData.versions.base.template_id;
964
+ if (this.props.location.query.module !== 'loyalty' && !_.isEmpty(baseSchema)) {
965
+ baseSchema.standalone.sections[0].childSections[1].childSections[0].childSections[0].inputFields[1].options = [];
966
+ baseSchema.standalone.sections[0].childSections[1].childSections[0].childSections[0].inputFields[1].options.push({
967
+ key: this.state.editTemplateData.versions.base.template_id,
968
+ label: this.state.editTemplateData.name,
969
+ value: this.state.editTemplateData.versions.base.template_id,
970
+ });
971
+ }
972
+ const templateData = {
973
+ 'template_id': this.state.editTemplateData.versions.base.template_id,
974
+ 'title': this.state.editTemplateData.versions.base.Title,
975
+ 'content': this.state.editTemplateData.versions.base.content,
976
+ };
977
+
978
+ this.setState({baseSchema, formData, isEdit: true}, () => {
979
+ this.setSelectedTemplateData(templateData);
980
+ })
981
+ }
982
+
983
+ maskTemplateUrl(url) {
984
+ const selectedWeChatAccount = (!_.isEmpty(this.props.Templates.selectedWeChatAccount) ? this.props.Templates.selectedWeChatAccount : this.props.WeChat.selectedWeChatAccount);
985
+ console.log('wecrm url', selectedWeChatAccount.configs.wecrm_app_id);
986
+
987
+ const appid = selectedWeChatAccount.configs.wecrm_app_id;
988
+ const domain = selectedWeChatAccount.configs.domain;
989
+ const maskedUrl = `${domain}/web?appid=${appid}&redirect_uri=${escape(url)}&response_type=code&scope=snsapi_base&state=STATE`;
990
+ console.log('wecrm masked url', maskedUrl);
991
+ return maskedUrl;
992
+ }
993
+
994
+ unMaskTemplateUrl(url) {
995
+ let startIndex, endIndex;
996
+ startIndex = url.indexOf('&redirect_uri=') + 14;
997
+ endIndex = url.indexOf('&response_type');
998
+ return decodeURIComponent(url.substring(startIndex, endIndex));
999
+ }
1000
+
1001
+ getMappedEvent(id, event) {
1002
+ return this.state.map[id][event];
1003
+ }
1004
+
1005
+ handleOnTagsContextChange = (data) => {
1006
+ console.log('parent tags context', data);
1007
+ const query = {
1008
+ layout: 'WECHAT',
1009
+ type: 'TAG',
1010
+ context: data.toLowerCase() === 'all' ? 'default' : data.toLowerCase(),
1011
+ embedded: this.props.location.query.type === 'embedded' ? this.props.location.query.type : 'full',
1012
+ };
1013
+ this.props.globalActions.fetchSchemaForEntity(query);
1014
+ }
1015
+
1016
+ startTemplateCreation(data) {
1017
+ console.log('startTemplateCreation');
1018
+ }
1019
+
1020
+ selectWechatTemplate() {
1021
+ console.log('Updated data');
1022
+ }
1023
+
1024
+ removeActionButtons() {
1025
+ const schema = _.cloneDeep(this.state.schema);
1026
+ const headerCols = schema.standalone.sections[0].childSections[0].inputFields[0].cols;
1027
+ _.forEach(headerCols, (col) => {
1028
+ col.colStyle.display = 'none';
1029
+ });
1030
+ this.setState({schema});
1031
+ }
1032
+
1033
+ showNext() {
1034
+ const response = {
1035
+ action: "showNext",
1036
+ value: true,
1037
+ };
1038
+ parent.postMessage(JSON.stringify(response), '*');
1039
+ }
1040
+
1041
+ insertAtCursor(field, myValue) {
1042
+ //IE support
1043
+ const myField = field;
1044
+ if (document.selection) {
1045
+ myField.focus();
1046
+ const sel = document.selection.createRange();
1047
+ sel.text = myValue;
1048
+ } else if (myField.selectionStart || myField.selectionStart === '0') { //MOZILLA and others
1049
+ const startPos = myField.selectionStart;
1050
+ const endPos = myField.selectionEnd;
1051
+ myField.value = myField.value.substring(0, startPos)
1052
+ + myValue
1053
+ + myField.value.substring(endPos, myField.value.length);
1054
+ myField.selectionStart = startPos + myValue.length;
1055
+ myField.selectionEnd = startPos + myValue.length;
1056
+ } else {
1057
+ myField.value += myValue;
1058
+ }
1059
+ const event = new Event('input', { bubbles: true });
1060
+ callNativeEvent({field, value: myField.value, event});
1061
+ }
1062
+
1063
+ internalUrlChecked() {
1064
+
1065
+ }
1066
+
1067
+ discardValues() {
1068
+ // this.setState({ schema: this.state.baseSchema, formData: {}});
1069
+ this.resetSchema();
1070
+ // this.resetState();
1071
+ }
1072
+
1073
+ cancelTemplate() {
1074
+ const type = this.props.location.query.type;
1075
+ this.props.router.push({
1076
+ pathname: `/wechat`,
1077
+ query: type === 'embedded' ? {type: 'embedded'} : {},
1078
+ });
1079
+ }
1080
+
1081
+ saveFormData(formData) {
1082
+ const obj = {};
1083
+ let selectedWechatData = {};
1084
+ if (this.state.isEdit) {
1085
+ obj['_id'] = this.state.editTemplateData._id;
1086
+ selectedWechatData = {
1087
+ 'template_id': this.state.editTemplateData.versions.base.template_id,
1088
+ 'title': this.state.editTemplateData.versions.base.Title,
1089
+ 'content': this.state.editTemplateData.versions.base.content,
1090
+ };
1091
+ } else {
1092
+ const selectedWechatTemplateIndex = _.findIndex(this.props.WeChat.weChatDefaultTemplate, {template_id: formData['wechat-template']});
1093
+ selectedWechatData = this.props.WeChat.weChatDefaultTemplate[selectedWechatTemplateIndex];
1094
+ }
1095
+ obj.name = `${selectedWechatData.title}: ${selectedWechatData.template_id}`;
1096
+ obj.type = 'WECHAT';
1097
+ obj.versions = {
1098
+ base: {},
1099
+ history: [],
1100
+ };
1101
+ obj.versions.base = {
1102
+ "template_id": selectedWechatData.template_id,
1103
+ "Title": selectedWechatData.title,
1104
+ Tag: [],
1105
+ url: this.state.formData['is-internal-url'] ? this.maskTemplateUrl(this.state.formData['template-link']) : this.state.formData['template-link'],
1106
+ TopColor: "#000000",
1107
+ data: {},
1108
+ preview: '',
1109
+ BrandId: (this.props.Templates.selectedWeChatAccount.configs.brand_id ? this.props.Templates.selectedWeChatAccount.configs.brand_id : ''),
1110
+ OriginalId: this.props.Templates.selectedWeChatAccount.sourceAccountIdentifier,
1111
+ content: selectedWechatData.content,
1112
+ isInternalUrl: this.state.formData['is-internal-url'],
1113
+ }
1114
+ _.forEach(this.state.selectedTemplateTags, (templateTag) => {
1115
+ const tagName = templateTag.substr(0, templateTag.indexOf('.'));
1116
+ let newTagname = tagName;
1117
+ obj.versions.base.Tag.push(tagName);
1118
+ if (tagName.match(/first/i)) {
1119
+ newTagname = "Header";
1120
+ }
1121
+ if (tagName.match(/remark/i)) {
1122
+ newTagname = "Footer";
1123
+ }
1124
+ obj.versions.base.data[tagName] = {};
1125
+ obj.versions.base.data[tagName] = {
1126
+ value: this.state.formData[`template-${newTagname}`],
1127
+ color: "#00000",
1128
+ }
1129
+ });
1130
+ console.log("Final Object ", obj);
1131
+ this.props.actions.createTemplate(obj);
1132
+ }
1133
+
1134
+ prepareWeChatPreviewData(wechatData, templateTags) {
1135
+ let content = _.cloneDeep(wechatData.content);
1136
+ const {formData} = _.cloneDeep(this.state);
1137
+ _.forEach(templateTags, (tag) => {
1138
+ let tagName = tag.substr(0, tag.indexOf('.'));
1139
+ if (tagName.match(/first/i)) {
1140
+ tagName = "Header";
1141
+ }
1142
+ if (tagName.match(/remark/i)) {
1143
+ tagName = "Footer";
1144
+ }
1145
+ if(this.state.formData[`template-${tagName}`] !== undefined && this.state.formData[`template-${tagName}`] !== '') {
1146
+ content = content.replace(`{{${tag}}}`, this.state.formData[`template-${tagName}`]);
1147
+ }
1148
+ })
1149
+ formData['sms-preview'] = content;
1150
+ // schema.standalone.sections[0].childSections[1].childSections[1].inputFields[0].cols[0].content = content;
1151
+ this.setState({formData}, () => {
1152
+ this.injectEvents(this.state.schema);
1153
+ });
1154
+ }
1155
+
1156
+ injectEvents(schema) {
1157
+ console.log('injecting events', schema);
1158
+ if (_.isEmpty(schema)) {
1159
+ return false;
1160
+ }
1161
+ const temp = schema;
1162
+ let newSchema = _.cloneDeep(schema);
1163
+ temp.standalone.sections = this.injectSections(temp.standalone.sections);
1164
+ _.forEach(temp.containers, (container) => {
1165
+ let tempContainer = container;
1166
+ tempContainer = this.injectContainer(tempContainer);
1167
+ return tempContainer;
1168
+ });
1169
+ newSchema = this.hideSchemaElementsByModule(schema);
1170
+ this.setState({schema: newSchema});
1171
+ return schema;
1172
+ }
1173
+
1174
+ hideSchemaElementsByModule(schema) {
1175
+ schema.standalone.sections[0].childSections[0].inputFields[0].cols[0].colStyle.display = (this.props.location.query.type === 'embedded' ? 'none' : 'block');
1176
+ schema.standalone.sections[0].childSections[0].inputFields[0].cols[1].colStyle.display = (this.props.location.query.type === 'embedded' ? 'none' : 'block');
1177
+ schema.standalone.sections[0].childSections[0].inputFields[0].cols[2].colStyle.display = (this.props.location.query.type === 'embedded' ? 'none' : 'block');
1178
+ schema.standalone.sections[0].childSections[0].inputFields[0].cols[3].colStyle.display = (this.props.location.query.type === 'embedded' ? 'none' : 'block');
1179
+
1180
+ schema.standalone.sections[0].childSections[1].childSections[0].childSections[0].inputFields[0].labelStyle.display = (this.props.location.query.module === 'loyalty' ? 'block' : 'none');
1181
+ schema.standalone.sections[0].childSections[1].childSections[0].childSections[0].inputFields[0].style.display = (this.props.location.query.module === 'loyalty' ? 'block' : 'none');
1182
+
1183
+ schema.standalone.sections[0].childSections[1].childSections[0].childSections[0].inputFields[1].disabled = (this.props.params.id && this.props.location.query.module !== 'loyalty');
1184
+ return schema;
1185
+ }
1186
+
1187
+ injectContainer(container) {
1188
+ const temp = container;
1189
+ if (temp.type === 'tabs') {
1190
+ temp.tabContent.sections = this.injectSections(temp.tabContent.sections);
1191
+ temp.tabBarExtraContent.sections = this.injectSections(temp.tabBarExtraContent.sections);
1192
+ temp.tabContent.sections = this.injectSections(temp.tabContent.sections);
1193
+ temp.injectedEvents = {};
1194
+ _.forEach(temp.supportedEvents, (event) => {
1195
+ temp.injectedEvents[event] = this.getMappedEvent(temp.id, event);
1196
+ });
1197
+ _.forEach(temp.panes, (pane) => {
1198
+ const tempPane = pane;
1199
+ tempPane.sections = this.injectSections(tempPane.sections);
1200
+ });
1201
+ }
1202
+ return temp;
1203
+ }
1204
+
1205
+ injectMultiColSection(section) {
1206
+ _.forEach(section.inputFields, (inputField) => {
1207
+ _.forEach(inputField.cols, (col) => {
1208
+ console.log('injecting for col', col);
1209
+ let temp = col;
1210
+ if (temp.type === 'popover') {
1211
+ temp.content.sections = this.injectSections(temp.content.sections);
1212
+ temp.value.sections = this.injectSections(temp.value.sections);
1213
+ return true;
1214
+ }
1215
+ temp.injectedEvents = {};
1216
+ _.forEach(col.supportedEvents, (event) => {
1217
+ temp.injectedEvents[event] = this.getMappedEvent(col.id, event);
1218
+ });
1219
+ temp = this.injectMessages(temp);
1220
+ return true;
1221
+ });
1222
+ });
1223
+ _.forEach(section.actionFields, (actionField) => {
1224
+ _.forEach(actionField.cols, (col) => {
1225
+ let temp = col;
1226
+ if (temp.type === 'popover') {
1227
+ temp.content.sections = this.injectSections(temp.content.sections);
1228
+ temp.value.sections = this.injectSections(temp.value.sections);
1229
+ return true;
1230
+ }
1231
+ temp.injectedEvents = {};
1232
+ _.forEach(col.supportedEvents, (event) => {
1233
+ temp.injectedEvents[event] = this.getMappedEvent(col.id, event);
1234
+ });
1235
+ temp = this.injectMessages(temp);
1236
+ return true;
1237
+ });
1238
+ });
1239
+ return section;
1240
+ }
1241
+
1242
+ injectColLabelSection(section) {
1243
+ _.forEach(section.inputFields, (inputField) => {
1244
+ let temp = inputField;
1245
+ if (temp.type === 'popover') {
1246
+ temp.content.sections = this.injectSections(temp.content.sections);
1247
+ temp.value.sections = this.injectSections(temp.value.sections);
1248
+ return true;
1249
+ }
1250
+ temp.injectedEvents = {};
1251
+ _.forEach(inputField.supportedEvents, (event) => {
1252
+ temp.injectedEvents[event] = this.getMappedEvent(inputField.id, event);
1253
+ });
1254
+ temp = this.injectMessages(temp);
1255
+ return true;
1256
+ });
1257
+ _.forEach(section.actionFields, (actionField) => {
1258
+ let temp = actionField;
1259
+ if (temp.type === 'popover') {
1260
+ temp.content.sections = this.injectSections(temp.content.sections);
1261
+ temp.value.sections = this.injectSections(temp.value.sections);
1262
+ return true;
1263
+ }
1264
+ temp.injectedEvents = {};
1265
+ _.forEach(actionField.supportedEvents, (event) => {
1266
+ temp.injectedEvents[event] = this.getMappedEvent(actionField.id, event);
1267
+ });
1268
+ temp = this.injectMessages(temp);
1269
+ return true;
1270
+ });
1271
+ return section;
1272
+ }
1273
+
1274
+ injectSections(sections) {
1275
+ _.forEach(sections, (section) => {
1276
+ let temp = section;
1277
+ if (temp.type === 'col-label') {
1278
+ temp = this.injectColLabelSection(temp);
1279
+ } else if (section.type === 'multicols') {
1280
+ temp = this.injectMultiColSection(temp);
1281
+ } else if (section.type === 'parent') {
1282
+ temp = this.injectSections(temp.childSections);
1283
+ }
1284
+ });
1285
+ return sections;
1286
+ }
1287
+
1288
+ handleCancelModal = () => {
1289
+ this.setState({showModal: false});
1290
+ }
1291
+
1292
+ injectParentSection(section) {
1293
+ _.forEach(section.childSections, (childSection) => {
1294
+ let temp = childSection;
1295
+ if (temp.type === 'col-label') {
1296
+ temp = this.injectColLabelSection(temp);
1297
+ } else if (section.type === 'multicols') {
1298
+ temp = this.injectMultiColSection(temp);
1299
+ } else if (section.type === 'parent') {
1300
+ temp = this.injectParentSection(temp);
1301
+ }
1302
+ });
1303
+ return section;
1304
+ }
1305
+
1306
+ render() {
1307
+ // const schema = this.state.schema;
1308
+ // const options = [
1309
+ // {
1310
+ // label: 'Test',
1311
+ // value: 'Test',
1312
+ // },
1313
+ // {
1314
+ // label: 'Test1',
1315
+ // value: 'Test1',
1316
+ // },
1317
+ // {
1318
+ // label: 'Test2',
1319
+ // value: 'Test2',
1320
+ // },
1321
+ // {
1322
+ // label: 'Test3',
1323
+ // value: 'Test3',
1324
+ // },
1325
+ // ];
1326
+ let tags = this.props.metaEntities && this.props.metaEntities.tags ? this.props.metaEntities.tags.standard : [];
1327
+ if (this.props.supportedTags) {
1328
+ tags = this.props.supportedTags;
1329
+ }
1330
+ return (
1331
+ <Spin
1332
+ tip={this.props.intl.formatMessage(messages.gettingTemplateData)}
1333
+ spinning={this.state.isLoading || this.props.fetchingSchema || (this.props.WeChat.getTemplateDetailsInProgress ? true : false)}>
1334
+ <div>
1335
+ <Helmet
1336
+ title="WeChat"
1337
+ />
1338
+ <Row className="wechat-container">
1339
+ <Col>
1340
+ <FormBuilder
1341
+ schema={this.state.schema}
1342
+ onSubmit={this.saveFormData}
1343
+ onChange={this.onFormDataChange}
1344
+ parent={this}
1345
+ modal={this.state.modalContent}
1346
+ showModal={this.state.showModal}
1347
+ iframeParent={parent}
1348
+ formData={_.cloneDeep(this.state.formData)}
1349
+ onFormValidityChange={this.setFormValidity}
1350
+ location= {this.props.location}
1351
+ tags={tags}
1352
+ tagModule={this.props.getDefaultTags}
1353
+ injectedTags={this.state.injectedTags ? this.state.injectedTags : {}}
1354
+ isEdit={this.state.isEdit}
1355
+ usingTabContainer={false}
1356
+ router={this.props.router}
1357
+ checkValidation={this.state.checkValidation}
1358
+ handleCancelModal={this.handleCancelModal}
1359
+ onContextChange={this.handleOnTagsContextChange}
1360
+ />
1361
+ </Col>
1362
+ </Row>
1363
+ </div>
1364
+ </Spin>
1365
+ );
1366
+ }
1367
+ }
1368
+
1369
+ MapTemplates.propTypes = {
1370
+ actions: PropTypes.object.isRequired,
1371
+ WeChat: PropTypes.object,
1372
+ globalActions: PropTypes.object,
1373
+ Templates: PropTypes.object,
1374
+ location: PropTypes.object,
1375
+ router: PropTypes.object,
1376
+ params: PropTypes.object,
1377
+ metaEntities: PropTypes.object,
1378
+ intl: intlShape.isRequired,
1379
+ isGetFormData: PropTypes.bool,
1380
+ getLibraryFormData: PropTypes.func,
1381
+ getDefaultTags: PropTypes.string,
1382
+ templateData: PropTypes.object,
1383
+ templateActions: PropTypes.object,
1384
+ };
1385
+
1386
+ const mapStateToProps = createStructuredSelector({
1387
+ WeChat: makeSelectWeChat(),
1388
+ Templates: makeSelectTemplates(),
1389
+ metaEntities: makeSelectMetaEntities(),
1390
+ fetchingSchema: makeSelectFetchingSchema(),
1391
+ });
1392
+
1393
+ function mapDispatchToProps(dispatch) {
1394
+ return {
1395
+ actions: bindActionCreators(actions, dispatch),
1396
+ globalActions: bindActionCreators(globalActions, dispatch),
1397
+ templateActions: bindActionCreators(templateActions, dispatch),
1398
+ };
1399
+ }
1400
+
1401
+ export default UserIsAuthenticated(connect(mapStateToProps, mapDispatchToProps)(injectIntl(MapTemplates)));