@bigbinary/neeto-message-templates-frontend 0.0.8 → 0.1.0

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.
package/dist/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import React, { useContext, useState, useRef, useEffect } from 'react';
2
- import { isNotEmpty, noop as noop$1, findBy } from '@bigbinary/neeto-commons-frontend/pure';
2
+ import { isNotEmpty, noop as noop$1, findBy, capitalize as capitalize$1, nullSafe } from '@bigbinary/neeto-commons-frontend/pure';
3
3
  import { buildFiltersFromURL, Bar } from '@bigbinary/neeto-filters-frontend';
4
4
  import Container from '@bigbinary/neeto-molecules/Container';
5
5
  import NeetoHeader from '@bigbinary/neeto-molecules/Header';
6
6
  import SubHeader from '@bigbinary/neeto-molecules/SubHeader';
7
- import { Dropdown, Typography, Button, Pane, Table, NoData, Alert, Select } from '@bigbinary/neetoui';
8
- import { prop, equals, includes, pick, omit, isEmpty, assoc, mergeAll } from 'ramda';
7
+ import { Dropdown, Typography, Button, Pane, Table, NoData, Alert, Select, Textarea } from '@bigbinary/neetoui';
8
+ import { prop, equals, includes, pick, omit, isEmpty, assoc, mergeAll, pluck } from 'ramda';
9
9
  import { QueryClient, QueryCache, QueryClientProvider, useQuery, useQueryClient, useMutation } from 'react-query';
10
10
  import { ReactQueryDevtools } from 'react-query/devtools';
11
11
  import { MenuHorizontal } from '@bigbinary/neeto-icons';
@@ -13,7 +13,7 @@ import axios from 'axios';
13
13
  import { withImmutableActions } from '@bigbinary/neeto-commons-frontend/react-utils';
14
14
  import { create as create$1 } from 'zustand';
15
15
  import { FormikEditor } from '@bigbinary/neeto-editor';
16
- import { Input, Form as Form$2 } from '@bigbinary/neetoui/formik';
16
+ import { Input, Form as Form$1, Select as Select$1, Button as Button$1 } from '@bigbinary/neetoui/formik';
17
17
  import { isPhoneNumberValid } from '@bigbinary/neeto-molecules/PhoneNumber';
18
18
  import * as yup from 'yup';
19
19
  import { useFormikContext } from 'formik';
@@ -5248,6 +5248,7 @@ var template = {
5248
5248
  search: "Search {{what}}",
5249
5249
  markAsActive: "Mark as active",
5250
5250
  markAsInactive: "Mark as inactive",
5251
+ close: "Close",
5251
5252
  "delete": "Delete",
5252
5253
  edit: "Edit",
5253
5254
  saveChanges: "Save changes",
@@ -5271,13 +5272,16 @@ var template = {
5271
5272
  sendTestEmail: "Send test email",
5272
5273
  sendTestSms: "Send test sms",
5273
5274
  filtersEmptyState: "There are no {{type}} to show for applied filters.",
5275
+ preview: "Template Preview",
5276
+ resolveVariable: "Resolve {{type}} variables",
5274
5277
  validation: {
5275
5278
  namePresence: "Name is required",
5276
5279
  subjectPresence: "Subject is required",
5277
5280
  bodyPresence: "Body is required",
5278
5281
  emailAddressPresence: "Email address is required",
5279
5282
  emailAddressFormatValidation: "Invalid email address",
5280
- validPhoneNumber: "Enter valid phone number"
5283
+ validPhoneNumber: "Enter valid phone number",
5284
+ variablePresence: "Variable should not be empty"
5281
5285
  }
5282
5286
  };
5283
5287
  var sendMessage = {
@@ -5369,10 +5373,6 @@ var MESSAGE_TEMPLATES = {
5369
5373
  }
5370
5374
  };
5371
5375
  var DEFAULT_EDITOR_ROW_COUNT = 15;
5372
- var TEMPLATE_VARIABLES = [{
5373
- key: "name",
5374
- label: t$1("template.name")
5375
- }];
5376
5376
 
5377
5377
  var MESSAGE_TEMPLATE_INITIAL_STATE = {
5378
5378
  isOpen: false,
@@ -6058,7 +6058,7 @@ var TestMessage = function TestMessage(_ref) {
6058
6058
 
6059
6059
  function ownKeys$2(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
6060
6060
  function _objectSpread$2(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys$2(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys$2(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6061
- var Form$1 = function Form(_ref) {
6061
+ var Form = function Form(_ref) {
6062
6062
  var onClose = _ref.onClose,
6063
6063
  isEdit = _ref.isEdit,
6064
6064
  _ref$template = _ref.template,
@@ -6067,7 +6067,8 @@ var Form$1 = function Form(_ref) {
6067
6067
  setInitialFocusField = _ref.setInitialFocusField,
6068
6068
  handleSubmitTestTemplate = _ref.handleSubmitTestTemplate,
6069
6069
  isTestMessageLoading = _ref.isTestMessageLoading,
6070
- shouldIncludeTestTemplate = _ref.shouldIncludeTestTemplate;
6070
+ shouldIncludeTestTemplate = _ref.shouldIncludeTestTemplate,
6071
+ templateVariables = _ref.templateVariables;
6071
6072
  var _useTranslation = useTranslation(),
6072
6073
  t = _useTranslation.t;
6073
6074
  var editorRef = useRef(null);
@@ -6095,7 +6096,7 @@ var Form$1 = function Form(_ref) {
6095
6096
  }
6096
6097
  });
6097
6098
  };
6098
- return /*#__PURE__*/React.createElement(Form$2, {
6099
+ return /*#__PURE__*/React.createElement(Form$1, {
6099
6100
  formProps: {
6100
6101
  noValidate: true,
6101
6102
  className: "flex w-full flex-col gap-y-4 pb-6"
@@ -6129,7 +6130,7 @@ var Form$1 = function Form(_ref) {
6129
6130
  name: "body",
6130
6131
  ref: editorRef,
6131
6132
  rows: DEFAULT_EDITOR_ROW_COUNT,
6132
- variables: TEMPLATE_VARIABLES
6133
+ variables: templateVariables
6133
6134
  }), isTestTemplateVisible && /*#__PURE__*/React.createElement(TestMessage, {
6134
6135
  handleSubmitTestTemplate: handleSubmitTestTemplate,
6135
6136
  isEmailTemplate: isEmailTemplate,
@@ -6158,7 +6159,8 @@ var AddEdit = function AddEdit(_ref) {
6158
6159
  handleSubmitTestTemplate = _ref.handleSubmitTestTemplate,
6159
6160
  shouldIncludeTestTemplate = _ref.shouldIncludeTestTemplate,
6160
6161
  addText = _ref.addText,
6161
- editText = _ref.editText;
6162
+ editText = _ref.editText,
6163
+ templateVariables = _ref.templateVariables;
6162
6164
  var initialFocusField = useRef();
6163
6165
  var setInitialFocusField = function setInitialFocusField(fieldRef) {
6164
6166
  return initialFocusField.current = fieldRef;
@@ -6174,7 +6176,7 @@ var AddEdit = function AddEdit(_ref) {
6174
6176
  }, /*#__PURE__*/React.createElement(Pane.Header, null, /*#__PURE__*/React.createElement(Typography, {
6175
6177
  style: "h2",
6176
6178
  weight: "semibold"
6177
- }, label)), /*#__PURE__*/React.createElement(Pane.Body, null, /*#__PURE__*/React.createElement(Form$1, {
6179
+ }, label)), /*#__PURE__*/React.createElement(Pane.Body, null, /*#__PURE__*/React.createElement(Form, {
6178
6180
  createUpdateTemplate: createUpdateTemplate,
6179
6181
  handleSubmitTestTemplate: handleSubmitTestTemplate,
6180
6182
  isEdit: isEdit,
@@ -6182,6 +6184,7 @@ var AddEdit = function AddEdit(_ref) {
6182
6184
  setInitialFocusField: setInitialFocusField,
6183
6185
  shouldIncludeTestTemplate: shouldIncludeTestTemplate,
6184
6186
  template: template,
6187
+ templateVariables: templateVariables,
6185
6188
  onClose: onClose
6186
6189
  })));
6187
6190
  };
@@ -6347,7 +6350,9 @@ var MessageTemplates = function MessageTemplates(_ref) {
6347
6350
  _ref$isTestMessageLoa = _ref.isTestMessageLoading,
6348
6351
  isTestMessageLoading = _ref$isTestMessageLoa === void 0 ? false : _ref$isTestMessageLoa,
6349
6352
  _ref$shouldIncludeTes = _ref.shouldIncludeTestTemplate,
6350
- shouldIncludeTestTemplate = _ref$shouldIncludeTes === void 0 ? true : _ref$shouldIncludeTes;
6353
+ shouldIncludeTestTemplate = _ref$shouldIncludeTes === void 0 ? true : _ref$shouldIncludeTes,
6354
+ _ref$templateVariable = _ref.templateVariables,
6355
+ templateVariables = _ref$templateVariable === void 0 ? {} : _ref$templateVariable;
6351
6356
  var _useState = useState(MESSAGE_TEMPLATE_INITIAL_STATE),
6352
6357
  _useState2 = _slicedToArray(_useState, 2),
6353
6358
  manageTemplatePane = _useState2[0],
@@ -6434,6 +6439,7 @@ var MessageTemplates = function MessageTemplates(_ref) {
6434
6439
  isTestMessageLoading: isTestMessageLoading,
6435
6440
  manageTemplatePane: manageTemplatePane,
6436
6441
  shouldIncludeTestTemplate: shouldIncludeTestTemplate,
6442
+ templateVariables: templateVariables,
6437
6443
  onClose: function onClose() {
6438
6444
  return setManageTemplatePane(assoc("isOpen", false));
6439
6445
  }
@@ -6441,13 +6447,13 @@ var MessageTemplates = function MessageTemplates(_ref) {
6441
6447
  };
6442
6448
  var index$1 = withReactQuery(MessageTemplates);
6443
6449
 
6450
+ var formatEditorContent = function formatEditorContent(value) {
6451
+ return value === null || value === void 0 ? void 0 : value.replaceAll(/<\/?(?!img)\w*\b[^>]*>/gi, "").trim();
6452
+ };
6444
6453
  var SEND_MESSAGE_INITIAL_VALUES = {
6445
6454
  subject: "",
6446
6455
  body: ""
6447
6456
  };
6448
- var formatEditorContent = function formatEditorContent(value) {
6449
- return value === null || value === void 0 ? void 0 : value.replaceAll(/<\/?(?!img)\w*\b[^>]*>/gi, "").trim();
6450
- };
6451
6457
  var EMAIL_MESSAGE_FORM_SCHEMA = yup.object().shape({
6452
6458
  subject: yup.string().trim().required(t$1("template.validation.subjectPresence")),
6453
6459
  body: yup.string().test("body", t$1("template.validation.bodyPresence"), function (value) {
@@ -6459,15 +6465,21 @@ var SMS_MESSAGE_FORM_VALIDATION_SCHEMA = yup.object().shape({
6459
6465
  return !!formatEditorContent(value);
6460
6466
  })
6461
6467
  });
6468
+ var WHATSAPP_MESSAGE_FORM_VALIDATIONS_SCHEMA = yup.object().shape({
6469
+ variableContents: yup.object().shape({
6470
+ body: yup.array().of(yup.string().required(t$1("template.validation.variablePresence")))
6471
+ })
6472
+ });
6462
6473
 
6463
6474
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
6464
6475
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6465
- var Form = function Form(_ref) {
6476
+ var EmailAndSms = function EmailAndSms(_ref) {
6466
6477
  var isEmailTemplate = _ref.isEmailTemplate,
6467
6478
  onClose = _ref.onClose,
6468
6479
  templates = _ref.templates,
6469
6480
  handleSubmit = _ref.handleSubmit,
6470
6481
  setInitialFocusField = _ref.setInitialFocusField,
6482
+ templateVariables = _ref.templateVariables,
6471
6483
  customFields = _ref.customFields,
6472
6484
  customFieldsInitialValues = _ref.customFieldsInitialValues,
6473
6485
  customFieldsValidationSchema = _ref.customFieldsValidationSchema;
@@ -6481,7 +6493,7 @@ var Form = function Form(_ref) {
6481
6493
  var updatedInitialValues = mergeAll([INITIAL_VALUES, customFieldsInitialValues]);
6482
6494
  var updatedValidationSchema = yup.object().shape(_objectSpread(_objectSpread({}, SEND_MESSAGE_FORM_SCHEMA === null || SEND_MESSAGE_FORM_SCHEMA === void 0 ? void 0 : SEND_MESSAGE_FORM_SCHEMA.fields), customFieldsValidationSchema === null || customFieldsValidationSchema === void 0 ? void 0 : customFieldsValidationSchema.fields));
6483
6495
  var initialValueKeys = Object.keys(INITIAL_VALUES);
6484
- return /*#__PURE__*/React.createElement(Form$2, {
6496
+ return /*#__PURE__*/React.createElement(Form$1, {
6485
6497
  formProps: {
6486
6498
  noValidate: true,
6487
6499
  className: "flex w-full flex-col gap-y-4 pb-6"
@@ -6533,7 +6545,7 @@ var Form = function Form(_ref) {
6533
6545
  name: "body",
6534
6546
  ref: editorRef,
6535
6547
  rows: DEFAULT_EDITOR_ROW_COUNT,
6536
- variables: TEMPLATE_VARIABLES
6548
+ variables: templateVariables
6537
6549
  }), /*#__PURE__*/React.createElement(Pane.Footer, {
6538
6550
  className: "absolute bottom-0 left-0 flex gap-x-2"
6539
6551
  }, /*#__PURE__*/React.createElement(Button, {
@@ -6550,6 +6562,125 @@ var Form = function Form(_ref) {
6550
6562
  });
6551
6563
  };
6552
6564
 
6565
+ var Whatsapp = function Whatsapp(_ref) {
6566
+ var _templates$, _templates$2, _templates$3, _templates$4;
6567
+ var templates = _ref.templates,
6568
+ onClose = _ref.onClose,
6569
+ handleSubmit = _ref.handleSubmit;
6570
+ var _useTranslation = useTranslation(),
6571
+ t = _useTranslation.t;
6572
+ var getInitialVariableComponents = function getInitialVariableComponents(variables) {
6573
+ var initialComponents = {};
6574
+ variables.forEach(function (_ref2) {
6575
+ var type = _ref2.type,
6576
+ count = _ref2.count;
6577
+ initialComponents[type] = Array(count).fill("");
6578
+ });
6579
+ return initialComponents;
6580
+ };
6581
+ var getTemplateMessage = function getTemplateMessage(components) {
6582
+ return nullSafe(pluck)("text", components).join("\n");
6583
+ };
6584
+ if (isEmpty(templates)) {
6585
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Typography, null, t("template.emptyState", {
6586
+ type: t("template.whatsappTemplates")
6587
+ })), /*#__PURE__*/React.createElement(Pane.Footer, {
6588
+ className: "absolute bottom-0 left-0"
6589
+ }, /*#__PURE__*/React.createElement(Button, {
6590
+ label: t("template.close"),
6591
+ onClick: onClose
6592
+ })));
6593
+ }
6594
+ var initialFormValues = {
6595
+ selectedTemplate: {
6596
+ label: (_templates$ = templates[0]) === null || _templates$ === void 0 ? void 0 : _templates$.name,
6597
+ value: (_templates$2 = templates[0]) === null || _templates$2 === void 0 ? void 0 : _templates$2.id
6598
+ },
6599
+ templateMessage: getTemplateMessage((_templates$3 = templates[0]) === null || _templates$3 === void 0 ? void 0 : _templates$3.components),
6600
+ variableContents: getInitialVariableComponents((_templates$4 = templates[0]) === null || _templates$4 === void 0 ? void 0 : _templates$4.variableCount)
6601
+ };
6602
+ return /*#__PURE__*/React.createElement(Form$1, {
6603
+ formProps: {
6604
+ noValidate: true,
6605
+ className: "flex w-full flex-col gap-y-4 pb-6"
6606
+ },
6607
+ formikProps: {
6608
+ initialValues: initialFormValues,
6609
+ validationSchema: WHATSAPP_MESSAGE_FORM_VALIDATIONS_SCHEMA,
6610
+ onSubmit: handleSubmit
6611
+ }
6612
+ }, function (_ref3) {
6613
+ var dirty = _ref3.dirty,
6614
+ isSubmitting = _ref3.isSubmitting,
6615
+ values = _ref3.values,
6616
+ setValues = _ref3.setValues;
6617
+ var handleTemplateChange = function handleTemplateChange(value) {
6618
+ if (!value) return;
6619
+ var template = findBy({
6620
+ id: value === null || value === void 0 ? void 0 : value.value
6621
+ }, templates);
6622
+ setValues({
6623
+ selectedTemplate: {
6624
+ label: template.name,
6625
+ value: template.id
6626
+ },
6627
+ templateMessage: getTemplateMessage(template.components),
6628
+ variableContents: getInitialVariableComponents(template.variableCount)
6629
+ });
6630
+ };
6631
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Select$1, {
6632
+ required: true,
6633
+ defaultValue: initialFormValues.selectedTemplate,
6634
+ label: t("template.title"),
6635
+ name: "selectedTemplate",
6636
+ options: templates.map(function (_ref4) {
6637
+ var name = _ref4.name,
6638
+ id = _ref4.id;
6639
+ return {
6640
+ label: name,
6641
+ value: id
6642
+ };
6643
+ }),
6644
+ onChange: handleTemplateChange
6645
+ }), /*#__PURE__*/React.createElement(Textarea, {
6646
+ disabled: true,
6647
+ nakedTextarea: true,
6648
+ label: t("template.preview"),
6649
+ value: values.templateMessage
6650
+ }), isNotEmpty(values.variableContents) && /*#__PURE__*/React.createElement(React.Fragment, null, Object.keys(values.variableContents).map(function (group) {
6651
+ return /*#__PURE__*/React.createElement("div", {
6652
+ className: "flex flex-col gap-y-2",
6653
+ key: group
6654
+ }, /*#__PURE__*/React.createElement(Typography, {
6655
+ style: "h4"
6656
+ }, t("template.resolveVariable", {
6657
+ type: capitalize$1(group)
6658
+ })), values.variableContents[group].map(function (_, index) {
6659
+ return /*#__PURE__*/React.createElement("div", {
6660
+ key: "".concat(group, "-").concat(index)
6661
+ }, /*#__PURE__*/React.createElement(Input, {
6662
+ required: true,
6663
+ key: "".concat(group, "-").concat(index),
6664
+ label: "{{".concat(index + 1, "}}"),
6665
+ name: "variableContents.".concat(group, ".").concat(index)
6666
+ }));
6667
+ }));
6668
+ })), /*#__PURE__*/React.createElement(Pane.Footer, {
6669
+ className: "absolute bottom-0 left-0 flex gap-x-2"
6670
+ }, /*#__PURE__*/React.createElement(Button$1, {
6671
+ disabled: !dirty || isSubmitting,
6672
+ label: t("template.saveChanges"),
6673
+ loading: isSubmitting,
6674
+ type: "submit"
6675
+ }), /*#__PURE__*/React.createElement(Button$1, {
6676
+ label: t("template.cancel"),
6677
+ style: "text",
6678
+ type: "reset",
6679
+ onClick: onClose
6680
+ })));
6681
+ });
6682
+ };
6683
+
6553
6684
  var SendMessagePane = function SendMessagePane(_ref) {
6554
6685
  var _ref$isOpen = _ref.isOpen,
6555
6686
  isOpen = _ref$isOpen === void 0 ? false : _ref$isOpen,
@@ -6564,7 +6695,9 @@ var SendMessagePane = function SendMessagePane(_ref) {
6564
6695
  _ref$customFieldsInit = _ref.customFieldsInitialValues,
6565
6696
  customFieldsInitialValues = _ref$customFieldsInit === void 0 ? {} : _ref$customFieldsInit,
6566
6697
  _ref$customFieldsVali = _ref.customFieldsValidationSchema,
6567
- customFieldsValidationSchema = _ref$customFieldsVali === void 0 ? {} : _ref$customFieldsVali;
6698
+ customFieldsValidationSchema = _ref$customFieldsVali === void 0 ? {} : _ref$customFieldsVali,
6699
+ _ref$templateVariable = _ref.templateVariables,
6700
+ templateVariables = _ref$templateVariable === void 0 ? {} : _ref$templateVariable;
6568
6701
  var _useTranslation = useTranslation(),
6569
6702
  t = _useTranslation.t;
6570
6703
  var initialFocusField = useRef();
@@ -6582,6 +6715,7 @@ var SendMessagePane = function SendMessagePane(_ref) {
6582
6715
  _useFetchTemplates$da3 = _useFetchTemplates$da2.templates,
6583
6716
  templates = _useFetchTemplates$da3 === void 0 ? [] : _useFetchTemplates$da3;
6584
6717
  var isEmailTemplate = type === "email";
6718
+ var isWhatsappTemplate = type === "whatsapp";
6585
6719
  return /*#__PURE__*/React.createElement(Pane, {
6586
6720
  initialFocusRef: initialFocusField,
6587
6721
  isOpen: isOpen,
@@ -6589,13 +6723,18 @@ var SendMessagePane = function SendMessagePane(_ref) {
6589
6723
  }, /*#__PURE__*/React.createElement(Pane.Header, null, /*#__PURE__*/React.createElement(Typography, {
6590
6724
  style: "h2",
6591
6725
  weight: "semibold"
6592
- }, t("common.send"), " ", type)), /*#__PURE__*/React.createElement(Pane.Body, null, /*#__PURE__*/React.createElement(Form, {
6726
+ }, t("common.send"), " ", type)), /*#__PURE__*/React.createElement(Pane.Body, null, isWhatsappTemplate ? /*#__PURE__*/React.createElement(Whatsapp, {
6727
+ handleSubmit: handleSubmit,
6728
+ templates: templates,
6729
+ onClose: onClose
6730
+ }) : /*#__PURE__*/React.createElement(EmailAndSms, {
6593
6731
  customFields: customFields,
6594
6732
  customFieldsInitialValues: customFieldsInitialValues,
6595
6733
  customFieldsValidationSchema: customFieldsValidationSchema,
6596
6734
  handleSubmit: handleSubmit,
6597
6735
  isEmailTemplate: isEmailTemplate,
6598
6736
  setInitialFocusField: setInitialFocusField,
6737
+ templateVariables: templateVariables,
6599
6738
  templates: templates,
6600
6739
  onClose: onClose
6601
6740
  })));