MDL-63303 message: fix bugs in message drawer part 4
[moodle.git] / message / amd / src / message_drawer_view_contacts_section_requests.js
blob08467f73cd6c4e6a1636acb27262f5c4e463c953
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.
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/>.
16 /**
17  * Controls the requests section of the contacts page.
18  *
19  * @module     core_message/message_drawer_view_contacts_section_requests
20  * @package    message
21  * @copyright  2018 Ryan Wyllie <ryan@moodle.com>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
24 define(
26     'jquery',
27     'core/notification',
28     'core/pubsub',
29     'core/templates',
30     'core_message/message_repository',
31     'core_message/message_drawer_events',
32     'core_message/message_drawer_lazy_load_list'
34 function(
35     $,
36     Notification,
37     PubSub,
38     Templates,
39     MessageRepository,
40     MessageDrawerEvents,
41     LazyLoadList
42 ) {
44     var SELECTORS = {
45         CONTACT_REQUEST: '[data-region="contact-request"]'
46     };
48     var TEMPLATES = {
49         REQUESTS_LIST: 'core_message/message_drawer_view_contacts_body_section_requests_list'
50     };
52     /**
53      * Render the requests in the content container.
54      *
55      * @param {Object} contentContainer List container element.
56      * @param {Array} requests List of requests.
57      * @return {Object} jQuery promise
58      */
59     var render = function(contentContainer, requests) {
60         var formattedRequests = requests.map(function(request) {
61             return {
62                 // This is actually the user id.
63                 id: request.id,
64                 profileimageurl: request.profileimageurl,
65                 fullname: request.fullname
66             };
67         });
68         return Templates.render(TEMPLATES.REQUESTS_LIST, {requests: formattedRequests})
69             .then(function(html) {
70                 contentContainer.append(html);
71                 return html;
72             })
73             .catch(Notification.exception);
74     };
76     /**
77      * Load the user contacts and call the renderer.
78      *
79      * @param {Object} listRoot The lazy loaded list root element
80      * @param {Integer} userId The logged in user id.
81      * @return {Object} jQuery promise
82      */
83     var load = function(listRoot, userId) {
84         return MessageRepository.getContactRequests(userId)
85             .then(function(requests) {
86                 LazyLoadList.setLoadedAll(listRoot, true);
87                 return requests;
88             })
89             .catch(Notification.exception);
90     };
92     /**
93      * Handle when a contact request is accepted or declined by removing the contact
94      * list from the page.
95      *
96      * @param {Object} root The section root element
97      * @return {Function} The event handler function
98      */
99     var handleContactRequestProcessed = function(root) {
100         return function(request) {
101             root.find('[data-request-id="' + request.userid + '"]').remove();
102             var contactRequests = root.find(SELECTORS.CONTACT_REQUEST);
104             if (!contactRequests.length) {
105                 LazyLoadList.showEmptyMessage(root);
106                 LazyLoadList.hideContent(root);
107             }
108         };
109     };
111     /**
112      * Listen for any events that might affect the requests section.
113      *
114      * @param {Object} root The section root element
115      */
116     var registerEventListeners = function(root) {
117         PubSub.subscribe(MessageDrawerEvents.CONTACT_REQUEST_ACCEPTED, handleContactRequestProcessed(root));
118         PubSub.subscribe(MessageDrawerEvents.CONTACT_REQUEST_DECLINED, handleContactRequestProcessed(root));
119     };
121     /**
122      * Setup the requests section.
123      *
124      * @param {Object} root Requests section container.
125      */
126     var show = function(root) {
127         if (!root.attr('data-contacts-init')) {
128             registerEventListeners(root);
129             root.attr('data-contacts-init', true);
130         }
132         // The root element is already the lazy loaded list root.
133         LazyLoadList.show(root, load, render);
134     };
136     return {
137         show: show,
138     };