Rules.vue 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. <template>
  2. <div>
  3. <div class="mb-6 flex flex-col md:flex-row justify-between md:items-center">
  4. <div class="flex items-center">
  5. <icon name="move" class="block w-6 h-6 mr-2 text-grey-300 fill-current" />
  6. You can drag and drop rules to order them.
  7. </div>
  8. <button
  9. @click="openCreateModal"
  10. class="bg-cyan-400 hover:bg-cyan-300 text-cyan-900 font-bold py-3 px-4 rounded focus:outline-none ml-auto"
  11. >
  12. Add New Rule
  13. </button>
  14. </div>
  15. <div v-if="initialRules.length" class="bg-white shadow">
  16. <draggable
  17. tag="ul"
  18. v-model="rows"
  19. v-bind="dragOptions"
  20. handle=".handle"
  21. @change="reorderRules"
  22. >
  23. <transition-group type="transition" name="flip-list">
  24. <li
  25. class="relative flex items-center py-3 px-5 border-b border-grey-100"
  26. v-for="row in rows"
  27. :key="row.name"
  28. >
  29. <div class="flex items-center w-3/5">
  30. <icon
  31. name="menu"
  32. class="handle block w-6 h-6 text-grey-300 fill-current cursor-pointer"
  33. />
  34. <span class="m-4">{{ row.name }} </span>
  35. </div>
  36. <div class="w-1/5 relative flex">
  37. <Toggle
  38. v-model="row.active"
  39. @on="activateRule(row.id)"
  40. @off="deactivateRule(row.id)"
  41. />
  42. </div>
  43. <div class="w-1/5 flex justify-end">
  44. <icon
  45. name="edit"
  46. class="block w-6 h-6 mr-3 text-grey-300 fill-current cursor-pointer"
  47. @click.native="openEditModal(row)"
  48. />
  49. <icon
  50. name="trash"
  51. class="block w-6 h-6 text-grey-300 fill-current cursor-pointer"
  52. @click.native="openDeleteModal(row.id)"
  53. />
  54. </div>
  55. </li>
  56. </transition-group>
  57. </draggable>
  58. </div>
  59. <div v-else class="bg-white rounded shadow overflow-x-auto">
  60. <div class="p-8 text-center text-lg text-grey-700">
  61. <h1 class="mb-6 text-2xl text-indigo-800 font-semibold">
  62. It doesn't look like you have any rules yet!
  63. </h1>
  64. <div class="mx-auto mb-6 w-24 border-b-2 border-grey-200"></div>
  65. <p class="mb-4">Click the button above to create a new rule.</p>
  66. </div>
  67. </div>
  68. <Modal :open="createRuleModalOpen" @close="createRuleModalOpen = false" :overflow="true">
  69. <div class="max-w-2xl w-full bg-white rounded-lg shadow-2xl p-6 my-12">
  70. <h2
  71. class="font-semibold text-grey-900 text-2xl leading-tight border-b-2 border-grey-100 pb-4"
  72. >
  73. Create new rule
  74. </h2>
  75. <p class="mt-4 text-grey-700">
  76. Rules work on all emails, including replies and also send froms. New conditions and
  77. actions will be added over time.
  78. </p>
  79. <label for="rule_name" class="block text-grey-700 text-sm my-2"> Name: </label>
  80. <p v-show="errors.ruleName" class="mb-3 text-red-500 text-sm">
  81. {{ errors.ruleName }}
  82. </p>
  83. <input
  84. v-model="createRuleObject.name"
  85. id="rule_name"
  86. type="text"
  87. class="w-full appearance-none bg-grey-100 border border-transparent text-grey-700 focus:outline-none rounded p-2"
  88. :class="errors.ruleName ? 'border-red-500' : ''"
  89. placeholder="Enter name"
  90. autofocus
  91. />
  92. <fieldset class="border border-cyan-400 p-4 my-4 rounded-sm">
  93. <legend class="px-2 leading-none text-sm">Conditions</legend>
  94. <!-- Loop for conditions -->
  95. <div v-for="(condition, key) in createRuleObject.conditions" :key="key">
  96. <!-- AND/OR operator -->
  97. <div v-if="key !== 0" class="flex justify-center my-2">
  98. <div class="relative">
  99. <select
  100. v-model="createRuleObject.operator"
  101. id="rule_operator"
  102. class="block appearance-none w-full text-grey-700 bg-white p-2 pr-6 rounded shadow focus:ring"
  103. required
  104. >
  105. <option value="AND">AND</option>
  106. <option value="OR">OR</option>
  107. </select>
  108. <div
  109. class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700"
  110. >
  111. <svg
  112. class="fill-current h-4 w-4"
  113. xmlns="http://www.w3.org/2000/svg"
  114. viewBox="0 0 20 20"
  115. >
  116. <path
  117. d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"
  118. />
  119. </svg>
  120. </div>
  121. </div>
  122. </div>
  123. <div class="p-2 w-full bg-grey-100">
  124. <div class="flex">
  125. <div class="flex items-center">
  126. <span>If</span>
  127. <span class="ml-2">
  128. <div class="relative">
  129. <select
  130. v-model="createRuleObject.conditions[key].type"
  131. id="rule_condition_types"
  132. class="block appearance-none w-32 text-grey-700 bg-white p-2 pr-6 rounded shadow focus:ring"
  133. required
  134. >
  135. <option
  136. v-for="option in conditionTypeOptions"
  137. :key="option.value"
  138. :value="option.value"
  139. >
  140. {{ option.label }}
  141. </option>
  142. </select>
  143. <div
  144. class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700"
  145. >
  146. <svg
  147. class="fill-current h-4 w-4"
  148. xmlns="http://www.w3.org/2000/svg"
  149. viewBox="0 0 20 20"
  150. >
  151. <path
  152. d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"
  153. />
  154. </svg>
  155. </div>
  156. </div>
  157. </span>
  158. <span
  159. v-if="conditionMatchOptions(createRuleObject, key).length"
  160. class="ml-4 flex"
  161. >
  162. <div class="relative mr-4">
  163. <select
  164. v-model="createRuleObject.conditions[key].match"
  165. id="rule_condition_matches"
  166. class="block appearance-none w-40 text-grey-700 bg-white p-2 pr-6 rounded shadow focus:ring"
  167. required
  168. >
  169. <option
  170. v-for="option in conditionMatchOptions(createRuleObject, key)"
  171. :key="option"
  172. :value="option"
  173. >
  174. {{ option }}
  175. </option>
  176. </select>
  177. <div
  178. class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700"
  179. >
  180. <svg
  181. class="fill-current h-4 w-4"
  182. xmlns="http://www.w3.org/2000/svg"
  183. viewBox="0 0 20 20"
  184. >
  185. <path
  186. d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"
  187. />
  188. </svg>
  189. </div>
  190. </div>
  191. <div class="flex">
  192. <input
  193. v-model="createRuleObject.conditions[key].currentConditionValue"
  194. @keyup.enter="addValueToCondition(createRuleObject, key)"
  195. type="text"
  196. class="w-full appearance-none bg-white border border-transparent rounded-l text-grey-700 focus:outline-none p-2"
  197. :class="errors.createRuleValues ? 'border-red-500' : ''"
  198. placeholder="Enter value"
  199. autofocus
  200. />
  201. <button class="p-2 bg-grey-200 rounded-r text-grey-600">
  202. <icon
  203. name="check"
  204. class="block w-6 h-6 text-grey-600 fill-current cursor-pointer"
  205. @click.native="addValueToCondition(createRuleObject, key)"
  206. />
  207. </button>
  208. </div>
  209. </span>
  210. </div>
  211. <div class="flex items-center">
  212. <!-- delete button -->
  213. <icon
  214. v-if="createRuleObject.conditions.length > 1"
  215. name="trash"
  216. class="block ml-4 w-6 h-6 text-grey-300 fill-current cursor-pointer"
  217. @click.native="deleteCondition(createRuleObject, key)"
  218. />
  219. </div>
  220. </div>
  221. <div class="mt-2">
  222. <span
  223. v-for="(value, index) in createRuleObject.conditions[key].values"
  224. :key="index"
  225. >
  226. <span class="bg-green-200 text-sm font-semibold rounded-sm pl-1">
  227. {{ value }}
  228. <icon
  229. name="close"
  230. class="inline-block w-4 h-4 text-grey-900 fill-current cursor-pointer"
  231. @click.native="createRuleObject.conditions[key].values.splice(index, 1)"
  232. />
  233. </span>
  234. <span
  235. class="mx-1"
  236. v-if="index + 1 !== createRuleObject.conditions[key].values.length"
  237. >
  238. or
  239. </span>
  240. </span>
  241. </div>
  242. </div>
  243. </div>
  244. <!-- add condition button -->
  245. <button
  246. @click="addCondition(createRuleObject)"
  247. class="mt-4 p-2 text-grey-800 bg-white hover:bg-grey-50 border border-grey-100 rounded focus:outline-none"
  248. >
  249. Add condition
  250. </button>
  251. <p v-show="errors.ruleConditions" class="mt-2 text-red-500 text-sm">
  252. {{ errors.ruleConditions }}
  253. </p>
  254. </fieldset>
  255. <fieldset class="border border-cyan-400 p-4 my-4 rounded-sm">
  256. <legend class="px-2 leading-none text-sm">Actions</legend>
  257. <!-- Loop for actions -->
  258. <div v-for="(action, key) in createRuleObject.actions" :key="key">
  259. <!-- AND/OR operator -->
  260. <div v-if="key !== 0" class="flex justify-center my-2">
  261. <div class="relative">AND</div>
  262. </div>
  263. <div class="p-2 w-full bg-grey-100">
  264. <div class="flex">
  265. <div class="flex items-center">
  266. <span>Then</span>
  267. <span class="ml-2">
  268. <div class="relative">
  269. <select
  270. v-model="createRuleObject.actions[key].type"
  271. @change="ruleActionChange(createRuleObject.actions[key])"
  272. id="rule_action_types"
  273. class="block appearance-none text-grey-700 bg-white p-2 pr-6 rounded shadow focus:ring"
  274. required
  275. >
  276. <option
  277. v-for="option in actionTypeOptions"
  278. :key="option.value"
  279. :value="option.value"
  280. >
  281. {{ option.label }}
  282. </option>
  283. </select>
  284. <div
  285. class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700"
  286. >
  287. <svg
  288. class="fill-current h-4 w-4"
  289. xmlns="http://www.w3.org/2000/svg"
  290. viewBox="0 0 20 20"
  291. >
  292. <path
  293. d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"
  294. />
  295. </svg>
  296. </div>
  297. </div>
  298. </span>
  299. <span
  300. v-if="
  301. createRuleObject.actions[key].type === 'subject' ||
  302. createRuleObject.actions[key].type === 'displayFrom'
  303. "
  304. class="ml-4 flex"
  305. >
  306. <div class="flex">
  307. <input
  308. v-model="createRuleObject.actions[key].value"
  309. type="text"
  310. class="w-full appearance-none bg-white border border-transparent rounded text-grey-700 focus:outline-none p-2"
  311. :class="errors.createRuleActionValue ? 'border-red-500' : ''"
  312. placeholder="Enter value"
  313. autofocus
  314. />
  315. </div>
  316. </span>
  317. <span
  318. v-else-if="createRuleObject.actions[key].type === 'banner'"
  319. class="ml-4 flex"
  320. >
  321. <div class="relative mr-4">
  322. <select
  323. v-model="createRuleObject.actions[key].value"
  324. id="create_rule_action_banner"
  325. class="block appearance-none w-40 text-grey-700 bg-white p-2 pr-6 rounded shadow focus:ring"
  326. required
  327. >
  328. <option selected value="top">Top</option>
  329. <option selected value="bottom">Bottom</option>
  330. <option selected value="off">Off</option>
  331. </select>
  332. <div
  333. class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700"
  334. >
  335. <svg
  336. class="fill-current h-4 w-4"
  337. xmlns="http://www.w3.org/2000/svg"
  338. viewBox="0 0 20 20"
  339. >
  340. <path
  341. d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"
  342. />
  343. </svg>
  344. </div>
  345. </div>
  346. </span>
  347. </div>
  348. <div class="flex items-center">
  349. <!-- delete button -->
  350. <icon
  351. v-if="createRuleObject.actions.length > 1"
  352. name="trash"
  353. class="block ml-4 w-6 h-6 text-grey-300 fill-current cursor-pointer"
  354. @click.native="deleteAction(createRuleObject, key)"
  355. />
  356. </div>
  357. </div>
  358. </div>
  359. </div>
  360. <!-- add action button -->
  361. <button
  362. @click="addAction(createRuleObject)"
  363. class="mt-4 p-2 text-grey-800 bg-white hover:bg-grey-50 border border-grey-100 rounded focus:outline-none"
  364. >
  365. Add action
  366. </button>
  367. <p v-show="errors.ruleActions" class="mt-2 text-red-500 text-sm">
  368. {{ errors.ruleActions }}
  369. </p>
  370. </fieldset>
  371. <div class="mt-6">
  372. <button
  373. @click="createNewRule"
  374. class="bg-cyan-400 hover:bg-cyan-300 text-cyan-900 font-bold py-3 px-4 rounded focus:outline-none"
  375. :class="createRuleLoading ? 'cursor-not-allowed' : ''"
  376. :disabled="createRuleLoading"
  377. >
  378. Create Rule
  379. <loader v-if="createRuleLoading" />
  380. </button>
  381. <button
  382. @click="createRuleModalOpen = false"
  383. class="ml-4 px-4 py-3 text-grey-800 font-semibold bg-white hover:bg-grey-50 border border-grey-100 rounded focus:outline-none"
  384. >
  385. Cancel
  386. </button>
  387. </div>
  388. </div>
  389. </Modal>
  390. <Modal :open="editRuleModalOpen" @close="closeEditModal" :overflow="true">
  391. <div class="max-w-2xl w-full bg-white rounded-lg shadow-2xl p-6 my-12">
  392. <h2
  393. class="font-semibold text-grey-900 text-2xl leading-tight border-b-2 border-grey-100 pb-4"
  394. >
  395. Edit rule
  396. </h2>
  397. <p class="mt-4 text-grey-700">
  398. Rules work on all emails, including replies and also send froms. New conditions and
  399. actions will be added over time.
  400. </p>
  401. <label for="edit_rule_name" class="block text-grey-700 text-sm my-2"> Name: </label>
  402. <p v-show="errors.ruleName" class="mb-3 text-red-500 text-sm">
  403. {{ errors.ruleName }}
  404. </p>
  405. <input
  406. v-model="editRuleObject.name"
  407. id="edit_rule_name"
  408. type="text"
  409. class="w-full appearance-none bg-grey-100 border border-transparent text-grey-700 focus:outline-none rounded p-2"
  410. :class="errors.ruleName ? 'border-red-500' : ''"
  411. placeholder="Enter name"
  412. autofocus
  413. />
  414. <fieldset class="border border-cyan-400 p-4 my-4 rounded-sm">
  415. <legend class="px-2 leading-none text-sm">Conditions</legend>
  416. <!-- Loop for conditions -->
  417. <div v-for="(condition, key) in editRuleObject.conditions" :key="key">
  418. <!-- AND/OR operator -->
  419. <div v-if="key !== 0" class="flex justify-center my-2">
  420. <div class="relative">
  421. <select
  422. v-model="editRuleObject.operator"
  423. id="edit_rule_operator"
  424. class="block appearance-none w-full text-grey-700 bg-white p-2 pr-6 rounded shadow focus:ring"
  425. required
  426. >
  427. <option value="AND">AND</option>
  428. <option value="OR">OR</option>
  429. </select>
  430. <div
  431. class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700"
  432. >
  433. <svg
  434. class="fill-current h-4 w-4"
  435. xmlns="http://www.w3.org/2000/svg"
  436. viewBox="0 0 20 20"
  437. >
  438. <path
  439. d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"
  440. />
  441. </svg>
  442. </div>
  443. </div>
  444. </div>
  445. <div class="p-2 w-full bg-grey-100">
  446. <div class="flex">
  447. <div class="flex items-center">
  448. <span>If</span>
  449. <span class="ml-2">
  450. <div class="relative">
  451. <select
  452. v-model="editRuleObject.conditions[key].type"
  453. id="edit_rule_condition_types"
  454. class="block appearance-none w-32 text-grey-700 bg-white p-2 pr-6 rounded shadow focus:ring"
  455. required
  456. >
  457. <option
  458. v-for="option in conditionTypeOptions"
  459. :key="option.value"
  460. :value="option.value"
  461. >
  462. {{ option.label }}
  463. </option>
  464. </select>
  465. <div
  466. class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700"
  467. >
  468. <svg
  469. class="fill-current h-4 w-4"
  470. xmlns="http://www.w3.org/2000/svg"
  471. viewBox="0 0 20 20"
  472. >
  473. <path
  474. d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"
  475. />
  476. </svg>
  477. </div>
  478. </div>
  479. </span>
  480. <span v-if="conditionMatchOptions(editRuleObject, key).length" class="ml-4 flex">
  481. <div class="relative mr-4">
  482. <select
  483. v-model="editRuleObject.conditions[key].match"
  484. id="edit_rule_condition_matches"
  485. class="block appearance-none w-40 text-grey-700 bg-white p-2 pr-6 rounded shadow focus:ring"
  486. required
  487. >
  488. <option
  489. v-for="option in conditionMatchOptions(editRuleObject, key)"
  490. :key="option"
  491. :value="option"
  492. >
  493. {{ option }}
  494. </option>
  495. </select>
  496. <div
  497. class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700"
  498. >
  499. <svg
  500. class="fill-current h-4 w-4"
  501. xmlns="http://www.w3.org/2000/svg"
  502. viewBox="0 0 20 20"
  503. >
  504. <path
  505. d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"
  506. />
  507. </svg>
  508. </div>
  509. </div>
  510. <div class="flex">
  511. <input
  512. v-model="editRuleObject.conditions[key].currentConditionValue"
  513. @keyup.enter="addValueToCondition(editRuleObect, key)"
  514. type="text"
  515. class="w-full appearance-none bg-white border border-transparent rounded-l text-grey-700 focus:outline-none p-2"
  516. :class="errors.ruleConditions ? 'border-red-500' : ''"
  517. placeholder="Enter value"
  518. autofocus
  519. />
  520. <button class="p-2 bg-grey-200 rounded-r text-grey-600">
  521. <icon
  522. name="check"
  523. class="block w-6 h-6 text-grey-600 fill-current cursor-pointer"
  524. @click.native="addValueToCondition(editRuleObject, key)"
  525. />
  526. </button>
  527. </div>
  528. </span>
  529. </div>
  530. <div class="flex items-center">
  531. <!-- delete button -->
  532. <icon
  533. v-if="editRuleObject.conditions.length > 1"
  534. name="trash"
  535. class="block ml-4 w-6 h-6 text-grey-300 fill-current cursor-pointer"
  536. @click.native="deleteCondition(editRuleObject, key)"
  537. />
  538. </div>
  539. </div>
  540. <div class="mt-2">
  541. <span v-for="(value, index) in editRuleObject.conditions[key].values" :key="index">
  542. <span class="bg-green-200 text-sm font-semibold rounded-sm pl-1">
  543. {{ value }}
  544. <icon
  545. name="close"
  546. class="inline-block w-4 h-4 text-grey-900 fill-current cursor-pointer"
  547. @click.native="editRuleObject.conditions[key].values.splice(index, 1)"
  548. />
  549. </span>
  550. <span
  551. class="mx-1"
  552. v-if="index + 1 !== editRuleObject.conditions[key].values.length"
  553. >
  554. or
  555. </span>
  556. </span>
  557. </div>
  558. </div>
  559. </div>
  560. <!-- add condition button -->
  561. <button
  562. @click="addCondition(editRuleObject)"
  563. class="mt-4 p-2 text-grey-800 bg-white hover:bg-grey-50 border border-grey-100 rounded focus:outline-none"
  564. >
  565. Add condition
  566. </button>
  567. <p v-show="errors.ruleConditions" class="mt-2 text-red-500 text-sm">
  568. {{ errors.ruleConditions }}
  569. </p>
  570. </fieldset>
  571. <fieldset class="border border-cyan-400 p-4 my-4 rounded-sm">
  572. <legend class="px-2 leading-none text-sm">Actions</legend>
  573. <!-- Loop for actions -->
  574. <div v-for="(action, key) in editRuleObject.actions" :key="key">
  575. <!-- AND/OR operator -->
  576. <div v-if="key !== 0" class="flex justify-center my-2">
  577. <div class="relative">AND</div>
  578. </div>
  579. <div class="p-2 w-full bg-grey-100">
  580. <div class="flex">
  581. <div class="flex items-center">
  582. <span>Then</span>
  583. <span class="ml-2">
  584. <div class="relative">
  585. <select
  586. v-model="editRuleObject.actions[key].type"
  587. @change="ruleActionChange(editRuleObject.actions[key])"
  588. id="rule_action_types"
  589. class="block appearance-none text-grey-700 bg-white p-2 pr-6 rounded shadow focus:ring"
  590. required
  591. >
  592. <option
  593. v-for="option in actionTypeOptions"
  594. :key="option.value"
  595. :value="option.value"
  596. >
  597. {{ option.label }}
  598. </option>
  599. </select>
  600. <div
  601. class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700"
  602. >
  603. <svg
  604. class="fill-current h-4 w-4"
  605. xmlns="http://www.w3.org/2000/svg"
  606. viewBox="0 0 20 20"
  607. >
  608. <path
  609. d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"
  610. />
  611. </svg>
  612. </div>
  613. </div>
  614. </span>
  615. <span
  616. v-if="
  617. editRuleObject.actions[key].type === 'subject' ||
  618. editRuleObject.actions[key].type === 'displayFrom'
  619. "
  620. class="ml-4 flex"
  621. >
  622. <div class="flex">
  623. <input
  624. v-model="editRuleObject.actions[key].value"
  625. type="text"
  626. class="w-full appearance-none bg-white border border-transparent rounded text-grey-700 focus:outline-none p-2"
  627. :class="errors.ruleActions ? 'border-red-500' : ''"
  628. placeholder="Enter value"
  629. autofocus
  630. />
  631. </div>
  632. </span>
  633. <span v-else-if="editRuleObject.actions[key].type === 'banner'" class="ml-4 flex">
  634. <div class="relative mr-4">
  635. <select
  636. v-model="editRuleObject.actions[key].value"
  637. id="edit_rule_action_banner"
  638. class="block appearance-none w-40 text-grey-700 bg-white p-2 pr-6 rounded shadow focus:ring"
  639. required
  640. >
  641. <option value="top">Top</option>
  642. <option value="bottom">Bottom</option>
  643. <option value="off">Off</option>
  644. </select>
  645. <div
  646. class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700"
  647. >
  648. <svg
  649. class="fill-current h-4 w-4"
  650. xmlns="http://www.w3.org/2000/svg"
  651. viewBox="0 0 20 20"
  652. >
  653. <path
  654. d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"
  655. />
  656. </svg>
  657. </div>
  658. </div>
  659. </span>
  660. </div>
  661. <div class="flex items-center">
  662. <!-- delete button -->
  663. <icon
  664. v-if="editRuleObject.actions.length > 1"
  665. name="trash"
  666. class="block ml-4 w-6 h-6 text-grey-300 fill-current cursor-pointer"
  667. @click.native="deleteAction(editRuleObject, key)"
  668. />
  669. </div>
  670. </div>
  671. </div>
  672. </div>
  673. <!-- add action button -->
  674. <button
  675. @click="addAction(editRuleObject)"
  676. class="mt-4 p-2 text-grey-800 bg-white hover:bg-grey-50 border border-grey-100 rounded focus:outline-none"
  677. >
  678. Add action
  679. </button>
  680. <p v-show="errors.ruleActions" class="mt-2 text-red-500 text-sm">
  681. {{ errors.ruleActions }}
  682. </p>
  683. </fieldset>
  684. <div class="mt-6">
  685. <button
  686. @click="editRule"
  687. class="bg-cyan-400 hover:bg-cyan-300 text-cyan-900 font-bold py-3 px-4 rounded focus:outline-none"
  688. :class="editRuleLoading ? 'cursor-not-allowed' : ''"
  689. :disabled="editRuleLoading"
  690. >
  691. Save
  692. <loader v-if="editRuleLoading" />
  693. </button>
  694. <button
  695. @click="closeEditModal"
  696. class="ml-4 px-4 py-3 text-grey-800 font-semibold bg-white hover:bg-grey-50 border border-grey-100 rounded focus:outline-none"
  697. >
  698. Cancel
  699. </button>
  700. </div>
  701. </div>
  702. </Modal>
  703. <Modal :open="deleteRuleModalOpen" @close="closeDeleteModal">
  704. <div class="max-w-lg w-full bg-white rounded-lg shadow-2xl px-6 py-6">
  705. <h2
  706. class="font-semibold text-grey-900 text-2xl leading-tight border-b-2 border-grey-100 pb-4"
  707. >
  708. Delete rule
  709. </h2>
  710. <p class="mt-4 text-grey-700">Are you sure you want to delete this rule?</p>
  711. <div class="mt-6">
  712. <button
  713. type="button"
  714. @click="deleteRule(ruleIdToDelete)"
  715. class="px-4 py-3 text-white font-semibold bg-red-500 hover:bg-red-600 border border-transparent rounded focus:outline-none"
  716. :class="deleteRuleLoading ? 'cursor-not-allowed' : ''"
  717. :disabled="deleteRuleLoading"
  718. >
  719. Delete rule
  720. <loader v-if="deleteRuleLoading" />
  721. </button>
  722. <button
  723. @click="closeDeleteModal"
  724. class="ml-4 px-4 py-3 text-grey-800 font-semibold bg-white hover:bg-grey-50 border border-grey-100 rounded focus:outline-none"
  725. >
  726. Cancel
  727. </button>
  728. </div>
  729. </div>
  730. </Modal>
  731. </div>
  732. </template>
  733. <script>
  734. import Modal from './../components/Modal.vue'
  735. import Toggle from './../components/Toggle.vue'
  736. import draggable from 'vuedraggable'
  737. export default {
  738. props: {
  739. initialRules: {
  740. type: Array,
  741. required: true,
  742. },
  743. },
  744. components: {
  745. Modal,
  746. Toggle,
  747. draggable,
  748. },
  749. data() {
  750. return {
  751. editRuleObject: {},
  752. ruleIdToDelete: '',
  753. deleteRuleLoading: false,
  754. deleteRuleModalOpen: false,
  755. createRuleModalOpen: false,
  756. editRuleModalOpen: false,
  757. createRuleLoading: false,
  758. editRuleLoading: false,
  759. createRuleObject: {
  760. name: '',
  761. conditions: [
  762. {
  763. type: 'select',
  764. match: 'contains',
  765. values: [],
  766. },
  767. ],
  768. actions: [
  769. {
  770. type: 'select',
  771. value: '',
  772. },
  773. ],
  774. operator: 'AND',
  775. },
  776. rows: this.initialRules,
  777. conditionTypeOptions: [
  778. {
  779. value: 'select',
  780. label: 'Select',
  781. },
  782. {
  783. value: 'sender',
  784. label: 'the sender',
  785. },
  786. {
  787. value: 'subject',
  788. label: 'the subject',
  789. },
  790. {
  791. value: 'alias',
  792. label: 'the alias',
  793. },
  794. ],
  795. actionTypeOptions: [
  796. {
  797. value: 'select',
  798. label: 'Select',
  799. },
  800. {
  801. value: 'subject',
  802. label: 'replace the subject with',
  803. },
  804. {
  805. value: 'displayFrom',
  806. label: 'replace the "from name" with',
  807. },
  808. {
  809. value: 'encryption',
  810. label: 'turn PGP encryption off',
  811. },
  812. {
  813. value: 'banner',
  814. label: 'set the banner information location to',
  815. },
  816. {
  817. value: 'block',
  818. label: 'block the email',
  819. },
  820. ],
  821. errors: {},
  822. }
  823. },
  824. computed: {
  825. activeRules() {
  826. return _.filter(this.rows, rule => rule.active)
  827. },
  828. dragOptions() {
  829. return {
  830. animation: 0,
  831. group: 'description',
  832. disabled: false,
  833. ghostClass: 'ghost',
  834. }
  835. },
  836. rowsIds() {
  837. return _.map(this.rows, row => row.id)
  838. },
  839. },
  840. methods: {
  841. openCreateModal() {
  842. this.errors = {}
  843. this.createRuleModalOpen = true
  844. },
  845. openDeleteModal(id) {
  846. this.deleteRuleModalOpen = true
  847. this.ruleIdToDelete = id
  848. },
  849. closeDeleteModal() {
  850. this.deleteRuleModalOpen = false
  851. this.ruleIdToDelete = ''
  852. },
  853. openEditModal(rule) {
  854. this.errors = {}
  855. this.editRuleModalOpen = true
  856. this.editRuleObject = _.cloneDeep(rule)
  857. },
  858. closeEditModal() {
  859. this.editRuleModalOpen = false
  860. this.editRuleObject = {}
  861. },
  862. deleteRule(id) {
  863. this.deleteRuleLoading = true
  864. axios
  865. .delete(`/api/v1/rules/${id}`)
  866. .then(response => {
  867. this.rows = _.reject(this.rows, rule => rule.id === id)
  868. this.deleteRuleModalOpen = false
  869. this.deleteRuleLoading = false
  870. })
  871. .catch(error => {
  872. this.error()
  873. this.deleteRuleModalOpen = false
  874. this.deleteRuleLoading = false
  875. })
  876. },
  877. createNewRule() {
  878. this.errors = {}
  879. if (!this.createRuleObject.name.length) {
  880. return (this.errors.ruleName = 'Please enter a rule name')
  881. }
  882. if (this.createRuleObject.name.length > 50) {
  883. return (this.errors.ruleName = 'Rule name cannot exceed 50 characters')
  884. }
  885. if (!this.createRuleObject.conditions[0].values.length) {
  886. return (this.errors.ruleConditions = 'You must add some values for the condition')
  887. }
  888. if (
  889. !this.createRuleObject.actions[0].value &&
  890. this.createRuleObject.actions[0].value !== false
  891. ) {
  892. return (this.errors.ruleActions = 'You must add a value for the action')
  893. }
  894. this.createRuleLoading = true
  895. axios
  896. .post(
  897. '/api/v1/rules',
  898. JSON.stringify({
  899. name: this.createRuleObject.name,
  900. conditions: this.createRuleObject.conditions,
  901. actions: this.createRuleObject.actions,
  902. operator: this.createRuleObject.operator,
  903. }),
  904. {
  905. headers: { 'Content-Type': 'application/json' },
  906. }
  907. )
  908. .then(({ data }) => {
  909. this.createRuleLoading = false
  910. this.resetCreateRuleObject()
  911. this.rows.push(data.data)
  912. this.createRuleModalOpen = false
  913. this.success('New rule created successfully')
  914. })
  915. .catch(error => {
  916. this.createRuleLoading = false
  917. this.error()
  918. })
  919. },
  920. editRule() {
  921. this.errors = {}
  922. if (!this.editRuleObject.name.length) {
  923. return (this.errors.ruleName = 'Please enter a rule name')
  924. }
  925. if (this.editRuleObject.name.length > 50) {
  926. return (this.errors.ruleName = 'Rule name cannot exceed 50 characters')
  927. }
  928. if (!this.editRuleObject.conditions[0].values.length) {
  929. return (this.errors.ruleConditions = 'You must add some values for the condition')
  930. }
  931. if (!this.editRuleObject.actions[0].value && this.editRuleObject.actions[0].value !== false) {
  932. return (this.errors.ruleActions = 'You must add a value for the action')
  933. }
  934. this.editRuleLoading = true
  935. axios
  936. .patch(
  937. `/api/v1/rules/${this.editRuleObject.id}`,
  938. JSON.stringify({
  939. name: this.editRuleObject.name,
  940. conditions: this.editRuleObject.conditions,
  941. actions: this.editRuleObject.actions,
  942. operator: this.editRuleObject.operator,
  943. }),
  944. {
  945. headers: { 'Content-Type': 'application/json' },
  946. }
  947. )
  948. .then(response => {
  949. let rule = _.find(this.rows, ['id', this.editRuleObject.id])
  950. this.editRuleLoading = false
  951. rule.name = this.editRuleObject.name
  952. rule.conditions = this.editRuleObject.conditions
  953. rule.actions = this.editRuleObject.actions
  954. rule.operator = this.editRuleObject.operator
  955. this.editRuleObject = {}
  956. this.editRuleModalOpen = false
  957. this.success('Rule successfully updated')
  958. })
  959. .catch(error => {
  960. this.editRuleLoading = false
  961. if (error.response.data) {
  962. this.error(Object.entries(error.response.data.errors)[0][1][0])
  963. } else {
  964. this.error()
  965. }
  966. })
  967. },
  968. activateRule(id) {
  969. axios
  970. .post(
  971. `/api/v1/active-rules`,
  972. JSON.stringify({
  973. id: id,
  974. }),
  975. {
  976. headers: { 'Content-Type': 'application/json' },
  977. }
  978. )
  979. .then(response => {
  980. //
  981. })
  982. .catch(error => {
  983. if (error.response !== undefined) {
  984. this.error(error.response.data)
  985. } else {
  986. this.error()
  987. }
  988. })
  989. },
  990. deactivateRule(id) {
  991. axios
  992. .delete(`/api/v1/active-rules/${id}`)
  993. .then(response => {
  994. //
  995. })
  996. .catch(error => {
  997. if (error.response !== undefined) {
  998. this.error(error.response.data)
  999. } else {
  1000. this.error()
  1001. }
  1002. })
  1003. },
  1004. reorderRules() {
  1005. axios
  1006. .post(
  1007. `/api/v1/reorder-rules`,
  1008. JSON.stringify({
  1009. ids: this.rowsIds,
  1010. }),
  1011. {
  1012. headers: { 'Content-Type': 'application/json' },
  1013. }
  1014. )
  1015. .then(response => {
  1016. this.success('Rule order successfully updated')
  1017. })
  1018. .catch(error => {
  1019. if (error.response !== undefined) {
  1020. this.error(error.response.data)
  1021. } else {
  1022. this.error()
  1023. }
  1024. })
  1025. },
  1026. conditionMatchOptions(object, key) {
  1027. if (_.includes(['sender', 'subject', 'alias'], object.conditions[key].type)) {
  1028. return [
  1029. 'contains',
  1030. 'does not contain',
  1031. 'is exactly',
  1032. 'is not',
  1033. 'starts with',
  1034. 'does not start with',
  1035. 'ends with',
  1036. 'does not end with',
  1037. ]
  1038. }
  1039. return []
  1040. },
  1041. addCondition(object) {
  1042. object.conditions.push({
  1043. type: 'select',
  1044. match: 'contains',
  1045. values: [],
  1046. })
  1047. },
  1048. deleteCondition(object, key) {
  1049. object.conditions.splice(key, 1)
  1050. },
  1051. addValueToCondition(object, key) {
  1052. if (object.conditions[key].currentConditionValue) {
  1053. object.conditions[key].values.push(object.conditions[key].currentConditionValue)
  1054. }
  1055. // Reset current conditon value input
  1056. object.conditions[key].currentConditionValue = ''
  1057. },
  1058. addAction(object) {
  1059. object.actions.push({
  1060. type: 'select',
  1061. value: '',
  1062. })
  1063. },
  1064. deleteAction(object, key) {
  1065. object.actions.splice(key, 1)
  1066. },
  1067. resetCreateRuleObject() {
  1068. this.createRuleObject = {
  1069. name: '',
  1070. conditions: [
  1071. {
  1072. type: 'select',
  1073. match: 'contains',
  1074. values: [],
  1075. },
  1076. ],
  1077. actions: [
  1078. {
  1079. type: 'select',
  1080. value: '',
  1081. },
  1082. ],
  1083. operator: 'AND',
  1084. }
  1085. },
  1086. ruleActionChange(action) {
  1087. if (action.type === 'subject' || action.type === 'displayFrom' || action.type === 'select') {
  1088. action.value = ''
  1089. } else if (action.type === 'encryption') {
  1090. action.value = false
  1091. } else if (action.type === 'banner') {
  1092. action.value = 'top'
  1093. } else if (action.type === 'block') {
  1094. action.value = true
  1095. }
  1096. },
  1097. success(text = '') {
  1098. this.$notify({
  1099. title: 'Success',
  1100. text: text,
  1101. type: 'success',
  1102. })
  1103. },
  1104. error(text = 'An error has occurred, please try again later') {
  1105. this.$notify({
  1106. title: 'Error',
  1107. text: text,
  1108. type: 'error',
  1109. })
  1110. },
  1111. },
  1112. }
  1113. </script>
  1114. <style>
  1115. .flip-list-move {
  1116. transition: transform 0.5s;
  1117. }
  1118. .ghost {
  1119. opacity: 0.5;
  1120. background: #c8ebfb;
  1121. }
  1122. </style>