calendar/amd/src/manage_subscriptions.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * A module to handle Delete/Update operations of the manage subscription page.
  17. *
  18. * @module core_calendar/manage_subscriptions
  19. * @copyright 2021 Huong Nguyen <huongnv13@gmail.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @since 4.0
  22. */
  23. import * as CalendarSelectors from 'core_calendar/selectors';
  24. import * as CalendarRepository from 'core_calendar/repository';
  25. import ModalSaveCancel from 'core/modal_save_cancel';
  26. import * as ModalEvents from 'core/modal_events';
  27. import {exception as displayException, addNotification, fetchNotifications} from 'core/notification';
  28. import Prefetch from 'core/prefetch';
  29. import {getString} from 'core/str';
  30. import {eventTypes} from 'core/local/inplace_editable/events';
  31. /**
  32. * Get subscription id for given element.
  33. *
  34. * @param {HTMLElement} element update/delete link
  35. * @return {Number}
  36. */
  37. const getSubscriptionId = element => {
  38. return parseInt(element.closest('tr').dataset.subid);
  39. };
  40. /**
  41. * Get subscription name for given element.
  42. *
  43. * @param {HTMLElement} element update/delete link
  44. * @return {String}
  45. */
  46. const getSubscriptionName = element => {
  47. return element.closest('tr').dataset.subname;
  48. };
  49. /**
  50. * Get subscription table row for subscription id.
  51. *
  52. * @param {string} subscriptionId Subscription id
  53. * @return {Element}
  54. */
  55. const getSubscriptionRow = subscriptionId => {
  56. return document.querySelector(`tr[data-subid="${subscriptionId}"]`);
  57. };
  58. /**
  59. * Create modal.
  60. *
  61. * @param {HTMLElement} element
  62. * @param {string} messageCode Message code.
  63. * @return {promise} Promise for modal
  64. */
  65. const createModal = (element, messageCode) => {
  66. const subscriptionName = getSubscriptionName(element);
  67. return ModalSaveCancel.create({
  68. title: getString('confirmation', 'admin'),
  69. body: getString(messageCode, 'calendar', subscriptionName),
  70. buttons: {
  71. save: getString('yes')
  72. },
  73. }).then(modal => {
  74. modal.getRoot().on(ModalEvents.hidden, () => {
  75. element.focus();
  76. });
  77. modal.show();
  78. return modal;
  79. });
  80. };
  81. /**
  82. * Response handler for delete action.
  83. *
  84. * @param {HTMLElement} element
  85. * @param {Object} data
  86. * @return {Promise}
  87. */
  88. const responseHandlerForDelete = async(element, data) => {
  89. const subscriptionName = getSubscriptionName(element);
  90. const message = data.status ? await getString('subscriptionremoved', 'calendar', subscriptionName) : data.warnings[0].message;
  91. const type = data.status ? 'info' : 'error';
  92. return addNotification({message, type});
  93. };
  94. /**
  95. * Register events for update/delete links.
  96. */
  97. const registerEventListeners = () => {
  98. document.addEventListener('click', e => {
  99. const deleteAction = e.target.closest(CalendarSelectors.actions.deleteSubscription);
  100. if (deleteAction) {
  101. e.preventDefault();
  102. const modalPromise = createModal(deleteAction, 'confirmsubscriptiondelete');
  103. modalPromise.then(modal => {
  104. modal.getRoot().on(ModalEvents.save, () => {
  105. const subscriptionId = getSubscriptionId(deleteAction);
  106. CalendarRepository.deleteSubscription(subscriptionId).then(data => {
  107. const response = responseHandlerForDelete(deleteAction, data);
  108. return response.then(() => {
  109. const subscriptionRow = getSubscriptionRow(subscriptionId);
  110. return subscriptionRow.remove();
  111. });
  112. }).catch(displayException);
  113. });
  114. return modal;
  115. }).catch(displayException);
  116. }
  117. });
  118. document.addEventListener(eventTypes.elementUpdated, e => {
  119. const inplaceEditable = e.target;
  120. if (inplaceEditable.getAttribute('data-component') == 'core_calendar') {
  121. fetchNotifications();
  122. }
  123. });
  124. };
  125. /**
  126. * Initialises.
  127. */
  128. export const init = () => {
  129. Prefetch.prefetchStrings('moodle', ['yes']);
  130. Prefetch.prefetchStrings('core_admin', ['confirmation']);
  131. Prefetch.prefetchStrings('core_calendar', ['confirmsubscriptiondelete', 'subscriptionremoved']);
  132. registerEventListeners();
  133. };