|
| 1 | +/* global fetch */ |
| 2 | +import 'whatwg-fetch'; |
| 3 | +import Exception from "../exceptions/exception"; |
| 4 | +import HousingException from "../exceptions/housingException"; |
| 5 | +import FetchUtil from "../utils/fetchUtil"; |
| 6 | +import MemberSelect from "./memberSelect"; |
| 7 | + |
| 8 | +export default class EditHousing { |
| 9 | + constructor(link) { |
| 10 | + this.link = link; |
| 11 | + this.modal = null; |
| 12 | + this.modalTpl = document.querySelector('#' + this.link.dataset.modal); |
| 13 | + this.type = this.modalTpl.dataset.type; |
| 14 | + this.rmnumber = this.link.dataset.rmnumber; |
| 15 | + |
| 16 | + this.endpoints = { |
| 17 | + roomDetails: '/housing/room/', |
| 18 | + alterRoom: '/housing/update/', |
| 19 | + memberDetails: '/member/' |
| 20 | + }; |
| 21 | + |
| 22 | + this.render(); |
| 23 | + } |
| 24 | + |
| 25 | + render() { |
| 26 | + this.link.addEventListener('click', e => { |
| 27 | + e.preventDefault(); |
| 28 | + |
| 29 | + if (this.rmnumber === "") { |
| 30 | + this.data = {}; |
| 31 | + this.data.occupants = []; |
| 32 | + this._renderModal(); |
| 33 | + } else { |
| 34 | + fetch(this.endpoints.roomDetails + this.rmnumber, { |
| 35 | + method: 'GET', |
| 36 | + headers: { |
| 37 | + Accept: 'application/json' |
| 38 | + }, |
| 39 | + credentials: 'same-origin' |
| 40 | + }) |
| 41 | + .then(FetchUtil.checkStatus) |
| 42 | + .then(FetchUtil.parseJSON) |
| 43 | + .then(data => { |
| 44 | + this.data = data; |
| 45 | + this._renderModal(); |
| 46 | + }); |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + _renderModal() { |
| 52 | + // Clone template modal |
| 53 | + this.modal = this.modalTpl.cloneNode(true); |
| 54 | + this.modal.setAttribute('id', |
| 55 | + this.modal.getAttribute('id') + '-' + this.rmnumber); |
| 56 | + |
| 57 | + // Submit button |
| 58 | + this.modal.querySelector('input[type="submit"]').addEventListener('click', |
| 59 | + e => { |
| 60 | + e.preventDefault(); |
| 61 | + this._submitForm(); |
| 62 | + }); |
| 63 | + |
| 64 | + // Room Number |
| 65 | + const roomInput = this.modal.querySelector('input[name="rmnumber"]'); |
| 66 | + roomInput.value = this.rmnumber; |
| 67 | + |
| 68 | + // Occupants |
| 69 | + const occupantsInput = this.modal.querySelector('input[name="occupants"]'); |
| 70 | + let occupantsStr = ""; |
| 71 | + this.data.occupants.forEach(occupant => { |
| 72 | + occupantsStr += occupant + ","; |
| 73 | + }); |
| 74 | + occupantsInput.value = occupantsStr; |
| 75 | + |
| 76 | + // Initialize selector control |
| 77 | + occupantsInput.dataset.src = "cm_members"; |
| 78 | + new MemberSelect(occupantsInput); // eslint-disable-line no-new |
| 79 | + |
| 80 | + // Add to DOM and show, then remove on hide |
| 81 | + document.getElementsByTagName('body')[0].appendChild(this.modal); |
| 82 | + $(this.modal) |
| 83 | + .on('hidden.bs.modal', e => { |
| 84 | + document.getElementsByTagName('body')[0].removeChild(e.target); |
| 85 | + }) |
| 86 | + .modal('show'); |
| 87 | + } |
| 88 | + |
| 89 | + _submitForm() { |
| 90 | + if (this.modal) { |
| 91 | + this.modal.querySelectorAll('button').forEach(btn => { |
| 92 | + btn.disabled = true; |
| 93 | + }); |
| 94 | + |
| 95 | + // Save details |
| 96 | + let payload = {}; |
| 97 | + payload.occupants = this.modal.querySelector('input[name="occupants"]').value.split(','); // eslint-disable-line max-len |
| 98 | + let room = this.modal.querySelector('input[name="rmnumber"]').value; |
| 99 | + |
| 100 | + FetchUtil.post(this.endpoints.alterRoom + room, payload, { |
| 101 | + successText: 'Occupants have been updated.' |
| 102 | + }, () => { |
| 103 | + // Hide the modal. |
| 104 | + $(this.modal).modal('hide'); |
| 105 | + |
| 106 | + // Update the DOM to reflect the new occupants. |
| 107 | + var occupantList = document.getElementById(room); |
| 108 | + if (occupantList) { |
| 109 | + // The room already exists in the list, update it. |
| 110 | + occupantList.innerHTML = ''; |
| 111 | + payload.occupants.forEach(occupant => { |
| 112 | + fetch(this.endpoints.memberDetails + occupant, { |
| 113 | + method: 'GET', |
| 114 | + headers: { |
| 115 | + Accept: 'application/json' |
| 116 | + }, |
| 117 | + credentials: 'same-origin' |
| 118 | + }) |
| 119 | + .then(FetchUtil.checkStatus) |
| 120 | + .then(FetchUtil.parseJSON) |
| 121 | + .then(data => { |
| 122 | + var newName = document.createElement("li"); |
| 123 | + newName.appendChild(document.createTextNode(data.name)); |
| 124 | + newName.setAttribute("class", "room-name"); |
| 125 | + occupantList.appendChild(newName); |
| 126 | + }); |
| 127 | + }); |
| 128 | + } else { |
| 129 | + // The room is new and needs to be created. |
| 130 | + var roomTable = document.getElementById("housing-table"); |
| 131 | + var newRoom = document.createElement("tr"); |
| 132 | + var newRoomNbrCol = document.createElement("td"); |
| 133 | + var newRoomNbr = document.createElement("h3"); |
| 134 | + newRoomNbr.appendChild(document.createTextNode(room)); |
| 135 | + newRoomNbr.setAttribute("class", "room-number"); |
| 136 | + newRoomNbrCol.appendChild(newRoomNbr); |
| 137 | + newRoomNbrCol.setAttribute("class", "new-table-col"); |
| 138 | + newRoom.appendChild(newRoomNbrCol); |
| 139 | + // Add new occupants to room. |
| 140 | + var newOccupantCol = document.createElement("td"); |
| 141 | + var newOccupantList = document.createElement("ul"); |
| 142 | + newOccupantList.setAttribute("id", room); |
| 143 | + newOccupantList.setAttribute("class", "occupant-list"); |
| 144 | + payload.occupants.forEach(occupant => { |
| 145 | + fetch(this.endpoints.memberDetails + occupant, { |
| 146 | + method: 'GET', |
| 147 | + headers: { |
| 148 | + Accept: 'application/json' |
| 149 | + }, |
| 150 | + credentials: 'same-origin' |
| 151 | + }) |
| 152 | + .then(FetchUtil.checkStatus) |
| 153 | + .then(FetchUtil.parseJSON) |
| 154 | + .then(data => { |
| 155 | + var newName = document.createElement("li"); |
| 156 | + newName.appendChild(document.createTextNode(data.name)); |
| 157 | + newName.setAttribute("class", "room-name"); |
| 158 | + newOccupantList.appendChild(newName); |
| 159 | + }); |
| 160 | + }); |
| 161 | + newOccupantCol.appendChild(newOccupantList); |
| 162 | + newOccupantCol.setAttribute("class", "new-table-col"); |
| 163 | + newRoom.appendChild(newOccupantCol); |
| 164 | + // Add edit button for new room. |
| 165 | + var newEditCol = document.createElement("td"); |
| 166 | + var editBtn = document.getElementById("rm-edit-btn"); |
| 167 | + var newEditBtn = editBtn.cloneNode(true); |
| 168 | + newEditBtn.setAttribute("data-rmnumber", room); |
| 169 | + new EditHousing(newEditBtn); // eslint-disable-no-new |
| 170 | + newEditCol.appendChild(newEditBtn); |
| 171 | + newEditCol.setAttribute("class", "new-table-col"); |
| 172 | + newRoom.appendChild(newEditCol); |
| 173 | + roomTable.appendChild(newRoom); |
| 174 | + } |
| 175 | + }); |
| 176 | + } else { |
| 177 | + throw new Exception(HousingException.SUBMIT_BEFORE_RENDER); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments