admin/tool/dataprivacy/amd/src/myrequestactions.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. * AMD module to enable users to manage their own data requests.
  17. *
  18. * @module tool_dataprivacy/myrequestactions
  19. * @copyright 2018 Jun Pataleta
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import Ajax from 'core/ajax';
  23. import Notification from 'core/notification';
  24. import Pending from 'core/pending';
  25. import {getStrings} from 'core/str';
  26. const SELECTORS = {
  27. CANCEL_REQUEST: '[data-action="cancel"][data-requestid]',
  28. };
  29. /**
  30. * Initialize module
  31. */
  32. export const init = () => {
  33. document.addEventListener('click', event => {
  34. const triggerElement = event.target.closest(SELECTORS.CANCEL_REQUEST);
  35. if (triggerElement === null) {
  36. return;
  37. }
  38. event.preventDefault();
  39. const requiredStrings = [
  40. {key: 'cancelrequest', component: 'tool_dataprivacy'},
  41. {key: 'cancelrequestconfirmation', component: 'tool_dataprivacy'},
  42. ];
  43. getStrings(requiredStrings).then(([cancelRequest, cancelConfirm]) => {
  44. return Notification.confirm(cancelRequest, cancelConfirm, cancelRequest, null, () => {
  45. const pendingPromise = new Pending('tool/dataprivacy:cancelRequest');
  46. const request = {
  47. methodname: 'tool_dataprivacy_cancel_data_request',
  48. args: {requestid: triggerElement.dataset.requestid}
  49. };
  50. Ajax.call([request])[0].then(response => {
  51. if (response.result) {
  52. window.location.reload();
  53. } else {
  54. Notification.addNotification({
  55. type: 'error',
  56. message: response.warnings[0].message
  57. });
  58. }
  59. return pendingPromise.resolve();
  60. }).catch(Notification.exception);
  61. });
  62. }).catch();
  63. });
  64. };