UNPKG

@dbp-topics/dispatch

Version:

[GitHub Repository](https://github.com/digital-blueprint/dispatch-frontend) | [npmjs package](https://www.npmjs.com/package/@dbp-topics/dispatch) | [Unpkg CDN](https://unpkg.com/browse/@dbp-topics/dispatch/) | [Dispatch Bundle](https://github.com/digital-

1,367 lines (1,193 loc) 118 kB
import DBPLitElement from '@dbp-toolkit/common/dbp-lit-element'; import {send} from "@dbp-toolkit/common/notification"; import MicroModal from "./micromodal.es"; import {FileSource, FileSink} from "@dbp-toolkit/file-handling"; import {html} from "lit"; import * as dispatchHelper from './utils'; import {PersonSelect} from "@dbp-toolkit/person-select"; import {ResourceSelect} from "@dbp-toolkit/resource-select"; import {IconButton} from "@dbp-toolkit/common"; export default class DBPDispatchLitElement extends DBPLitElement { constructor() { super(); this.isSessionRefreshed = false; this.auth = {}; this.currentItem = {}; this.currentRecipient = {}; this.subject = ''; this.groupId = ''; this.groupValue = this.loadGroupValue(); this.tempItem = {}; this.tempValue = {}; this.tempChange = false; } static get scopedElements() { return { 'dbp-file-source': FileSource, 'dbp-file-sink': FileSink, 'dbp-person-select': PersonSelect, 'dbp-resource-select': ResourceSelect, 'dbp-icon-button': IconButton }; } static get properties() { return { ...super.properties, auth: { type: Object }, currentItem: {type: Object, attribute: false}, currentRecipient: {type: Object, attribute: false}, subject: {type: String, attribute: false}, groupId: {type: String, attribute: false}, tempItem: {type: Object, attribute: false}, tempValue: {type: Object, attribute: false}, fileHandlingEnabledTargets: {type: String, attribute: 'file-handling-enabled-targets'}, nextcloudWebAppPasswordURL: {type: String, attribute: 'nextcloud-web-app-password-url'}, nextcloudWebDavURL: {type: String, attribute: 'nextcloud-webdav-url'}, nextcloudName: {type: String, attribute: 'nextcloud-name'}, nextcloudFileURL: {type: String, attribute: 'nextcloud-file-url'}, nextcloudAuthInfo: {type: String, attribute: 'nextcloud-auth-info'}, }; } connectedCallback() { super.connectedCallback(); this._loginStatus = ''; this._loginState = []; } /** * Request a re-rendering every time isLoggedIn()/isLoading() changes */ _updateAuth() { this._loginStatus = this.auth['login-status']; let newLoginState = [this.isLoggedIn(), this.isLoading()]; if (this._loginState.toString() !== newLoginState.toString()) { this.requestUpdate(); } this._loginState = newLoginState; } update(changedProperties) { changedProperties.forEach((oldValue, propName) => { switch (propName) { case "auth": this._updateAuth(); break; } }); super.update(changedProperties); } /** * Returns if a person is set in or not * * @returns {boolean} true or false */ isLoggedIn() { return (this.auth.person !== undefined && this.auth.person !== null); } /** * Returns true if a person has successfully logged in * * @returns {boolean} true or false */ isLoading() { if (this._loginStatus === "logged-out") return false; return (!this.isLoggedIn() && this.auth.token !== undefined); } /** * Send a fetch to given url with given options * * @param url * @param options * @returns {object} response (error or result) */ async httpGetAsync(url, options) { let response = await fetch(url, options).then(result => { if (!result.ok) throw result; return result; }).catch(error => { return error; }); return response; } /** * Gets the list of all dispatch requests of the current logged-in user * * @param groupId * @returns {object} response */ async getListOfDispatchRequests(groupId) { const options = { method: 'GET', headers: { 'Content-Type': 'application/ld+json', Authorization: "Bearer " + this.auth.token }, }; return await this.httpGetAsync(this.entryPointUrl + '/dispatch/requests?perPage=999&groupId=' + groupId, options); } /** * Gets the dispatch request of the current logged-in user with the given identifier * * @param identifier * @returns {object} response */ async getDispatchRequest(identifier) { const options = { method: 'GET', headers: { 'Content-Type': 'application/ld+json', Authorization: "Bearer " + this.auth.token }, }; return await this.httpGetAsync(this.entryPointUrl + '/dispatch/requests/' + identifier, options); } /** * Gets the dispatch recipient of the given ID * * @param identifier * @returns {object} response */ async getDispatchRecipient(identifier) { const options = { method: 'GET', headers: { 'Content-Type': 'application/ld+json', Authorization: "Bearer " + this.auth.token }, }; return await this.httpGetAsync(this.entryPointUrl + '/dispatch/request-recipients/' + identifier, options); } /** * Sends a dispatch post request * * @returns {object} response */ async sendCreateDispatchRequest() { let body = { "name": this.subject, "senderGivenName": this.currentItem.senderGivenName, "senderFamilyName": this.currentItem.senderFamilyName, "senderAddressCountry": this.currentItem.senderAddressCountry, "senderPostalCode": this.currentItem.senderPostalCode, "senderAddressLocality": this.currentItem.senderAddressLocality, "senderStreetAddress": this.currentItem.senderStreetAddress, "senderBuildingNumber": this.currentItem.senderBuildingNumber, "groupId": this.groupId, }; const options = { method: 'POST', headers: { 'Content-Type': 'application/ld+json', Authorization: 'Bearer ' + this.auth.token, }, body: JSON.stringify(body), }; return await this.httpGetAsync(this.entryPointUrl + '/dispatch/requests', options); } /** * Sends a delete dispatch request * * @param identifier * @returns {object} response */ async sendDeleteDispatchRequest(identifier) { const options = { method: 'DELETE', headers: { 'Content-Type': 'application/ld+json', Authorization: 'Bearer ' + this.auth.token, }, }; return await this.httpGetAsync(this.entryPointUrl + '/dispatch/requests/' + identifier, options); } /** * Sends a put dispatch request * * @param identifier * @param senderGivenName * @param senderFamilyName * @param senderAddressCountry * @param senderPostalCode * @param senderAddressLocality * @param senderStreetAddress * @param senderBuildingNumber * @param groupId * @returns {object} response */ async sendEditDispatchRequest(identifier, senderGivenName, senderFamilyName, senderAddressCountry, senderPostalCode, senderAddressLocality, senderStreetAddress, senderBuildingNumber, groupId) { let body = { "senderGivenName": senderGivenName, "senderFamilyName": senderFamilyName, "senderAddressCountry": senderAddressCountry, "senderPostalCode": senderPostalCode, "senderAddressLocality": senderAddressLocality, "senderStreetAddress": senderStreetAddress, "senderBuildingNumber": senderBuildingNumber, "groupId": groupId }; const options = { method: 'PUT', headers: { 'Content-Type': 'application/ld+json', Authorization: 'Bearer ' + this.auth.token, }, body: JSON.stringify(body), }; return await this.httpGetAsync(this.entryPointUrl + '/dispatch/requests/' + identifier, options); } /** * Sends a submit dispatch request * * @param identifier * @returns {object} response */ async sendSubmitDispatchRequest(identifier) { let body = {}; const options = { method: 'POST', headers: { 'Content-Type': 'application/ld+json', Authorization: 'Bearer ' + this.auth.token, }, body: JSON.stringify(body), }; return await this.httpGetAsync(this.entryPointUrl + '/dispatch/requests/' + identifier + '/submit', options); } /** * Sends a dispatch request-recipients post request * * @param id * @param personIdentifier * @param givenName * @param familyName * @param birthDate * @param addressCountry * @param postalCode * @param addressLocality * @param streetAddress * @param buildingNumber * @returns {object} response */ async sendAddRequestRecipientsRequest(id, personIdentifier, givenName, familyName, birthDate, addressCountry, postalCode, addressLocality, streetAddress, buildingNumber) { let body; if (personIdentifier === null) { body = { "dispatchRequestIdentifier": id, "givenName": givenName, "familyName": familyName, "addressCountry": addressCountry, "postalCode": postalCode, "addressLocality": addressLocality, "streetAddress": streetAddress, "buildingNumber": buildingNumber, "birthDate": birthDate }; } else { body = { "dispatchRequestIdentifier": id, "personIdentifier": personIdentifier }; } const options = { method: 'POST', headers: { 'Content-Type': 'application/ld+json', Authorization: 'Bearer ' + this.auth.token, }, body: JSON.stringify(body), }; return await this.httpGetAsync(this.entryPointUrl + '/dispatch/request-recipients', options); } async sendUpdateRecipientRequest(recipientId, id, personIdentifier, givenName, familyName, birthDate, addressCountry, postalCode, addressLocality, streetAddress, buildingNumber) { let body; if (personIdentifier === null) { body = { "dispatchRequestIdentifier": id, "givenName": givenName, "familyName": familyName, "addressCountry": addressCountry, "postalCode": postalCode, "addressLocality": addressLocality, "streetAddress": streetAddress, "buildingNumber": buildingNumber, "birthDate": birthDate }; } else { body = { "dispatchRequestIdentifier": id, "personIdentifier": personIdentifier }; } const options = { method: 'PUT', headers: { 'Content-Type': 'application/ld+json', Authorization: 'Bearer ' + this.auth.token, }, body: JSON.stringify(body), }; return await this.httpGetAsync(this.entryPointUrl + '/dispatch/request-recipients/' + recipientId, options); } async sendDeleteRecipientRequest(id) { const options = { method: 'DELETE', headers: { Authorization: 'Bearer ' + this.auth.token, }, }; return await this.httpGetAsync(this.entryPointUrl + '/dispatch/request-recipients/' + id, options); } async sendAddFileToRequest(id, file) { let formData = new FormData(); formData.append('dispatchRequestIdentifier', id); formData.append('file', file); const options = { method: 'POST', headers: { Authorization: 'Bearer ' + this.auth.token, }, body: formData, }; return await this.httpGetAsync(this.entryPointUrl + '/dispatch/request-files', options); } async sendDeleteFileRequest(id) { const options = { method: 'DELETE', headers: { Authorization: 'Bearer ' + this.auth.token, }, }; return await this.httpGetAsync(this.entryPointUrl + '/dispatch/request-files/' + id, options); } async sendGetPersonDetailsRequest(identifier) { const options = { method: 'GET', headers: { 'Content-Type': 'application/ld+json', Authorization: "Bearer " + this.auth.token }, }; // return await this.httpGetAsync(this.entryPointUrl + identifier, options); ///'base/people/' return await this.httpGetAsync(this.entryPointUrl + identifier + '?includeLocal=streetAddress%2CaddressLocality%2CpostalCode%2CaddressCountry', options); } async sendChangeSubjectRequest(identifier, subject) { let body = { "name": subject, }; const options = { method: 'PUT', headers: { 'Content-Type': 'application/ld+json', Authorization: 'Bearer ' + this.auth.token, }, body: JSON.stringify(body), }; return await this.httpGetAsync(this.entryPointUrl + '/dispatch/requests/' + identifier, options); } async sendGetStatusChangeRequest(identifier) { const options = { method: 'GET', headers: { 'Content-Type': 'application/ld+json', Authorization: "Bearer " + this.auth.token }, }; return await this.httpGetAsync(this.entryPointUrl + identifier, options); } /* * Open file source * */ openFileSource() { const fileSource = this._('#file-source'); if (fileSource) { this._('#file-source').openDialog(); } } async onFileSelected(event) { await this.addFile(event.detail.file); } async addFile(file) { this._('#add-files-btn').start(); try { const i18n = this._i18n; let id = this.currentItem.identifier; let response = await this.sendAddFileToRequest(id, file); let responseBody = await response.json(); if (responseBody !== undefined && response.status === 201) { send({ "summary": i18n.t('show-requests.successfully-added-file-title'), "body": i18n.t('show-requests.successfully-added-file-text'), "type": "success", "timeout": 5, }); let resp = await this.getDispatchRequest(id); let responseBody = await resp.json(); if (responseBody !== undefined && responseBody.status !== 403) { this.currentItem = responseBody; } } else { // TODO error handling send({ "summary": 'Error!', "body": 'File could not be added.', "type": "danger", "timeout": 5, }); } } catch (e) { //TODO send({ "summary": 'Error!', "body": 'There was an error.', "type": "danger", "timeout": 5, }); } finally { this._('#add-files-btn').stop(); } } async deleteFile(file) { const i18n = this._i18n; this._('#delete-file-btn').start(); try { let response = await this.sendDeleteFileRequest(file.identifier, file); if (response.status === 204) { send({ "summary": i18n.t('show-requests.successfully-deleted-file-title'), "body": i18n.t('show-requests.successfully-deleted-file-text'), "type": "success", "timeout": 5, }); let id = this.currentItem.identifier; let resp = await this.getDispatchRequest(id); let responseBody = await resp.json(); if (responseBody !== undefined && responseBody.status !== 403) { this.currentItem = responseBody; } } else { // TODO error handling send({ "summary": 'Error!', "body": 'File could not be deleted.', "type": "danger", "timeout": 5, }); } } finally { this._('#delete-file-btn').stop(); } } /** * Open Filesink for a single File * * @param fileContentUrl * @param fileName */ async downloadFileClickHandler(fileContentUrl, fileName) { let files = []; const arr = dispatchHelper.convertDataURIToBinary(fileContentUrl); const binaryFile = new File([arr], fileName, { type: dispatchHelper.getDataURIContentType(fileContentUrl), }); files.push(binaryFile); // this.signedFilesToDownload = files.length; this._('#file-sink').files = [...files]; } async _onDownloadFileClicked(event, statusRequestId) { let button = event.target; button.start(); try { let response = await this.sendGetStatusChangeRequest(statusRequestId); let responseBody = await response.json(); if (responseBody !== undefined && response.status === 200) { console.log('resp: ', responseBody); let fileContentUrl = responseBody['fileContentUrl']; let fileName = 'DeliveryNotification';//responseBody['description']; //TODO await this.downloadFileClickHandler(fileContentUrl, fileName); } else { //TODO } } finally { button.stop(); } } parseListOfRequests(response) { let list = []; let numTypes = parseInt(response['hydra:totalItems']); if (isNaN(numTypes)) { numTypes = 0; } for (let i = 0; i < numTypes; i++ ) { list[i] = response['hydra:member'][i]; } list.sort(this.compareListItems); return list; } async addRecipientToRequest(event, item) { this._('#add-recipient-btn').start(); try { const i18n = this._i18n; let id = this.currentItem.identifier; let givenName = this.currentRecipient.givenName; let familyName = this.currentRecipient.familyName; let addressCountry = this.currentRecipient.addressCountry; let postalCode = this.currentRecipient.postalCode; let addressLocality = this.currentRecipient.addressLocality; let streetAddress = this.currentRecipient.streetAddress; let buildingNumber = this.currentRecipient.buildingNumber; let birthDate = this.currentRecipient.birthDate ? this.currentRecipient.birthDate : null; let personIdentifier = this.currentRecipient.personIdentifier ? this.currentRecipient.personIdentifier : null; let response = await this.sendAddRequestRecipientsRequest(id, personIdentifier, givenName, familyName, birthDate, addressCountry, postalCode, addressLocality, streetAddress, buildingNumber); let responseBody = await response.json(); if (responseBody !== undefined && response.status === 201) { send({ "summary": i18n.t('show-requests.successfully-added-recipient-title'), "body": i18n.t('show-requests.successfully-added-recipient-text'), "type": "success", "timeout": 5, }); let resp = await this.getDispatchRequest(id); let responseBody = await resp.json(); if (responseBody !== undefined && responseBody.status !== 403) { this.currentItem = responseBody; // console.log(this.currentItem); this.currentRecipient = {}; } this._('#recipient-selector').clear(); this.currentRecipient.personIdentifier = ''; this.currentRecipient.givenName = ''; this.currentRecipient.familyName = ''; this.currentRecipient.postalCode = ''; this.currentRecipient.addressLocality = ''; this.currentRecipient.streetAddress = ''; this.currentRecipient.buildingNumber = ''; this.currentRecipient.birthDate = ''; this.currentRecipient.addressLocality = ''; this.currentRecipient.postalCode = ''; this.currentRecipient.streetAddress = ''; this.currentRecipient.addressCountry = dispatchHelper.getCountryMapping('AT'); this._('#tf-add-recipient-gn-dialog').value = this.currentRecipient.givenName; this._('#tf-add-recipient-fn-dialog').value = this.currentRecipient.familyName; this._('#tf-add-recipient-pc-dialog').value = this.currentRecipient.postalCode; this._('#tf-add-recipient-al-dialog').value = this.currentRecipient.addressLocality; this._('#tf-add-recipient-sa-dialog').value = this.currentRecipient.streetAddress; this._('#tf-add-recipient-bn-dialog').value = this.currentRecipient.buildingNumber; this._('#tf-add-recipient-birthdate').value = this.currentRecipient.birthDate; this._('#add-recipient-country-select').value = 'AT'; this.requestUpdate(); } else { // TODO error handling send({ "summary": 'Error!', "body": 'Could not add recipient. Response code: ' + response.status, "type": "danger", "timeout": 5, }); } } catch (e) { //TODO send({ "summary": 'Error!', "body": 'Could not add recipient.', "type": "danger", "timeout": 5, }); } finally { this._('#add-recipient-btn').stop(); } } async updateRecipient(event, item) { let button = event.target; button.start(); // this._('#edit-recipient-btn').start(); try { const i18n = this._i18n; let id = this.currentItem.identifier; let recipientId = this.currentRecipient.identifier; let givenName = this.currentRecipient.givenName; let familyName = this.currentRecipient.familyName; let addressCountry = this.currentRecipient.addressCountry; let postalCode = this.currentRecipient.postalCode; let addressLocality = this.currentRecipient.addressLocality; let streetAddress = this.currentRecipient.streetAddress; let buildingNumber = this.currentRecipient.buildingNumber; let birthDate = this.currentRecipient.birthDate; let personIdentifier = this.currentRecipient.personIdentifier; let response = await this.sendUpdateRecipientRequest(recipientId, id, personIdentifier, givenName, familyName, birthDate, addressCountry, postalCode, addressLocality, streetAddress, buildingNumber); let responseBody = await response.json(); if (responseBody !== undefined && response.status === 200) { send({ "summary": i18n.t('show-requests.successfully-edited-recipient-title'), "body": i18n.t('show-requests.successfully-edited-recipient-text'), "type": "success", "timeout": 5, }); this.currentRecipient = responseBody; let resp = await this.getDispatchRequest(id); let responseBody2 = await resp.json(); if (responseBody2 !== undefined && responseBody2.status !== 403) { this.currentItem = responseBody2; } } else { // TODO error handling send({ "summary": 'Error!', "body": 'Could not update recipient. Response code: ' + response.status, "type": "danger", "timeout": 5, }); } } catch (e) { //TODO } finally { // this._('#edit-recipient-btn').stop(); button.stop(); } } async deleteRecipient(event, recipient) { const i18n = this._i18n; // console.log(recipient); let button = event.target; button.start(); // this._('#delete-recipient-btn').start(); try { let response = await this.sendDeleteRecipientRequest(recipient.identifier); if (response.status === 204) { send({ "summary": i18n.t('show-requests.successfully-deleted-recipient-title'), "body": i18n.t('show-requests.successfully-deleted-recipient-text'), "type": "success", "timeout": 5, }); let id = this.currentItem.identifier; let resp = await this.getDispatchRequest(id); let responseBody = await resp.json(); if (responseBody !== undefined && responseBody.status !== 403) { this.currentItem = responseBody; this.requestCreated = false; } } else { // TODO error handling send({ "summary": 'Error!', "body": 'Could not delete recipient. Response code: ' + response.status, "type": "danger", "timeout": 5, }); } } finally { // this._('#delete-recipient-btn').stop(); button.stop(); } } async fetchStatusOfRecipient(recipient) { const i18n = this._i18n; console.log(recipient); let response = await this.getDispatchRecipient(recipient.identifier); let responseBody = await response.json(); if (responseBody !== undefined && response.status === 200) { send({ "summary": i18n.t('show-requests.successfully-updated-sender-title'), "body": i18n.t('show-requests.successfully-updated-sender-text'), "type": "success", "timeout": 5, }); this.currentRecipient.statusType = responseBody['statusType']; this.currentRecipient.statusDescription = responseBody['description']; } else { // TODO error handling // send({ // "summary": 'Error!', // "body": 'Could not fetch status of recipient with ID: ' + recipient.identifier + '. Response code: ' + response.status, // "type": "danger", // "timeout": 5, // }); } } async editRequest(event, item) { let button = event.target; button.start(); try { let resp = await this.getDispatchRequest(item.identifier); let responseBody = await resp.json(); if (responseBody !== undefined && responseBody.status !== 403) { this.currentItem = responseBody; } this.currentItem.recipients.forEach((element) => { // console.log(element.identifier); this.fetchDetailedRecipientInformation(element.identifier).then(result => { //TODO }); }); this.showListView = false; this.showDetailsView = true; } finally { button.stop(); } } async deleteRequest(event, item) { const i18n = this._i18n; let button = event.target; if (item.dateSubmitted) { send({ "summary": i18n.t('show-requests.delete-not-allowed-title'), "body": i18n.t('show-requests.delete-not-allowed-text'), "type": "danger", "timeout": 5, }); return; } if(confirm(i18n.t('show-requests.delete-dialog-text'))) { this._('#delete-btn').start(); //TODO check if code below works button.start(); try { let response = await this.sendDeleteDispatchRequest(item.identifier); if (response.status === 204) { if (this.dispatchRequestsTable) { this.getListOfRequests(); } send({ "summary": i18n.t('show-requests.successfully-deleted-title'), "body": i18n.t('show-requests.successfully-deleted-text'), "type": "success", "timeout": 5, }); this.clearAll(); } else { // TODO error handling send({ "summary": 'Error!', "body": 'Could not delete request. Response code: ' + response.status, "type": "danger", "timeout": 5, }); } } catch (e) { //TODO } finally { this._('#delete-btn').stop(); button.stop(); } } } async submitRequest(event, item) { const i18n = this._i18n; let button = event.target; if (item.dateSubmitted) { send({ "summary": i18n.t('show-requests.submit-not-allowed-title'), "body": i18n.t('show-requests.submit-not-allowed-text'), "type": "danger", "timeout": 5, }); return; } if (item.files && item.files.length > 0 && item.recipients && item.recipients.length > 0) { if(confirm(i18n.t('show-requests.submit-dialog-text'))) { try { this._('#submit-btn').start(); //TODO button.start(); let response = await this.sendSubmitDispatchRequest(item.identifier); if (response.status === 201) { if (this.dispatchRequestsTable) { this.getListOfRequests(); } send({ "summary": i18n.t('show-requests.successfully-submitted-title'), "body": i18n.t('show-requests.successfully-submitted-text'), "type": "success", "timeout": 5, }); this.clearAll(); this.requestCreated = false; } else if (response.status === 403) { send({ "summary": i18n.t('create-request.error-requested-title'), "body": i18n.t('error-not-permitted'), "type": "danger", "timeout": 5, }); } else { // TODO error handling send({ "summary": 'Error!', "body": 'Could not submit request. Response code: ' + response.status, "type": "danger", "timeout": 5, }); } } catch (e) { //TODO } finally { this._('#submit-btn').stop(); button.stop(); } } } else { send({ "summary": i18n.t('show-requests.empty-fields-submitted-title'), "body": i18n.t('show-requests.empty-fields-submitted-text'), "type": "danger", "timeout": 5, }); } } async changeSubjectRequest(id, subject) { this._('#edit-subject-btn').start(); const i18n = this._i18n; try { let response = await this.sendChangeSubjectRequest(id, subject); let responseBody = await response.json(); if (responseBody !== undefined && response.status === 200) { this.currentItem = responseBody; this.subject = this.currentItem.name; send({ "summary": i18n.t('show-requests.edit-subject-success-title'), "body": i18n.t('show-requests.edit-subject-success-text'), "type": "success", "timeout": 5, }); } else if (response.status === 403) { send({ "summary": i18n.t('create-request.error-requested-title'), "body": i18n.t('error-not-permitted'), "type": "danger", "timeout": 5, }); } else { // TODO show error code specific notification send({ "summary": i18n.t('create-request.error-changed-subject-title'), "body": i18n.t('create-request.error-changed-subject-text'), "type": "danger", "timeout": 5, }); } } finally { this._('#edit-subject-btn').stop(); } } async confirmEditSender() { const i18n = this._i18n; try { this._('#edit-sender-btn').start(); let id = this.currentItem.identifier; let senderGivenName = this._('#tf-edit-sender-gn-dialog').value; let senderFamilyName = this._('#tf-edit-sender-fn-dialog').value; let senderPostalCode = this._('#tf-edit-sender-pc-dialog').value; let senderAddressLocality = this._('#tf-edit-sender-al-dialog').value; let senderStreetAddress = this._('#tf-edit-sender-sa-dialog').value; let senderBuildingNumber = (this._('#tf-edit-sender-bn-dialog') && this._('#tf-edit-sender-bn-dialog').value) ? this._('#tf-edit-sender-bn-dialog').value : ''; let groupId = this.groupId; let e = this._('#edit-sender-country-select'); let value = e.value; let text = e.options[e.selectedIndex].text; let senderAddressCountry = [value, text]; let response = await this.sendEditDispatchRequest(id, senderGivenName, senderFamilyName, senderAddressCountry[0], senderPostalCode, senderAddressLocality, senderStreetAddress, senderBuildingNumber, groupId); let responseBody = await response.json(); if (responseBody !== undefined && response.status === 200) { send({ "summary": i18n.t('show-requests.successfully-updated-sender-title'), "body": i18n.t('show-requests.successfully-updated-sender-text'), "type": "success", "timeout": 5, }); this.currentItem = responseBody; if (this.dispatchRequestsTable) { this.getListOfRequests(); } } else if (response.status === 403) { send({ "summary": i18n.t('create-request.error-requested-title'), "body": i18n.t('error-not-permitted'), "type": "danger", "timeout": 5, }); } else { // TODO error handling send({ "summary": 'Error!', "body": 'Could not edit sender. Response code: ' + response.status, "type": "danger", "timeout": 5, }); } } finally { this._('#edit-sender-btn').stop(); } } async confirmEditSubject() { let subject = this._('#tf-edit-subject-fn-dialog').value; let id = this.currentItem.identifier; await this.changeSubjectRequest(id, subject); } async confirmAddSubject() { this._('#add-subject-confirm-btn').disabled = true; //TODO this.subject = this._('#tf-add-subject-fn-dialog').value ? this._('#tf-add-subject-fn-dialog').value : ''; await this.processCreateDispatchRequest(); this._('#tf-add-subject-fn-dialog').value = ''; this.showDetailsView = true; this.hasSubject = true; this.hasSender = true; this._('#add-subject-confirm-btn').disabled = false; } async fetchDetailedRecipientInformation(identifier) { let response = await this.getDispatchRecipient(identifier); let responseBody = await response.json(); if (responseBody !== undefined && response.status === 200) { this.currentRecipient = responseBody; this.currentRecipient.personIdentifier = responseBody['personIdentifier'] !== '' ? responseBody['personIdentifier'] : null; this.currentRecipient.birthDate = responseBody['birthDate'] !== '' ? this.convertToBirthDate(responseBody['birthDate']) : ''; this.currentRecipient.statusChanges = responseBody['statusChanges']; if (this.currentRecipient.statusChanges.length > 0) { this.currentRecipient.statusDescription = this.currentRecipient.statusChanges[0].description; this.currentRecipient.statusType = this.currentRecipient.statusChanges[0].statusType; } else { this.currentRecipient.statusDescription = null; this.currentRecipient.statusType = null; } this.currentRecipient.deliveryEndDate = responseBody['deliveryEndDate'] ? responseBody['deliveryEndDate'] : ''; this.currentRecipient.appDeliveryId = responseBody['appDeliveryID'] ? responseBody['appDeliveryID'] : ''; // console.log('rec: ', this.currentRecipient); } else { // TODO error handling } } async submitSelected() { const i18n = this._i18n; this._('#submit-all-btn').start(); try { let selectedItems = this.dispatchRequestsTable.getSelectedRows(); // console.log('selectedItems: ', selectedItems); let somethingWentWrong = false; for (let i = 0; i < selectedItems.length; i++) { let id = selectedItems[i].getData()['requestId']; let response = await this.getDispatchRequest(id); let result = await response.json(); if (result.dateSubmitted) { send({ "summary": i18n.t('show-requests.delete-not-allowed-title'), "body": i18n.t('show-requests.delete-not-allowed-text'), //TODO add more specific text here "type": "danger", "timeout": 5, }); somethingWentWrong = true; break; } if (!(result.files && result.files.length > 0 && result.recipients && result.recipients.length > 0)) { send({ "summary": i18n.t('show-requests.empty-fields-submitted-title'), "body": i18n.t('show-requests.empty-fields-submitted-text'), "type": "danger", "timeout": 5, }); somethingWentWrong = true; break; } } if (somethingWentWrong) { return; } let dialogText; if (this.dispatchRequestsTable.getSelectedRows().length > 1) { dialogText = i18n.t('show-requests.submit-more-dialog-text', {count: this.dispatchRequestsTable.getSelectedRows().length}); } else { dialogText = i18n.t('show-requests.submit-dialog-text'); } if (confirm(dialogText)) { for (let i = 0; i < selectedItems.length; i++) { let id = selectedItems[i].getData()['requestId']; let response = await this.getDispatchRequest(id); let result = await response.json(); let submitResponse = await this.sendSubmitDispatchRequest(result.identifier); if (submitResponse.status !== 201) { somethingWentWrong = true; break; } } if (!somethingWentWrong) { this.getListOfRequests(); send({ "summary": i18n.t('show-requests.successfully-submitted-title'), "body": i18n.t('show-requests.successfully-submitted-text'), "type": "success", "timeout": 5, }); this.clearAll(); } else { // TODO error handling send({ "summary": 'Error!', "body": 'Could not submit request.', "type": "danger", "timeout": 5, }); } } } finally { this._('#submit-all-btn').stop(); } } async deleteSelected() { const i18n = this._i18n; this._('#delete-all-btn').start(); try { let selectedItems = this.dispatchRequestsTable.getSelectedRows(); console.log('selectedItems: ', selectedItems); let somethingWentWrong = false; for (let i = 0; i < selectedItems.length; i++) { let id = selectedItems[i].getData()['requestId']; let response = await this.getDispatchRequest(id); let result = await response.json(); if (result.dateSubmitted) { send({ "summary": i18n.t('show-requests.delete-not-allowed-title'), "body": i18n.t('show-requests.delete-not-allowed-text'), "type": "danger", "timeout": 5, }); somethingWentWrong = true; break; } } if (somethingWentWrong) { return; } let dialogText; if (this.dispatchRequestsTable.getSelectedRows().length > 1) { dialogText = i18n.t('show-requests.delete-more-dialog-text', {count: this.dispatchRequestsTable.getSelectedRows().length}); } else { dialogText = i18n.t('show-requests.delete-dialog-text'); } if (confirm(dialogText)) { for (let i = 0; i < selectedItems.length; i++) { let id = selectedItems[i].getData()['requestId']; let response = await this.getDispatchRequest(id); let result = await response.json(); let deleteResponse = await this.sendDeleteDispatchRequest(result.identifier); if (deleteResponse.status !== 204) { somethingWentWrong = true; break; } } if (!somethingWentWrong) { this.getListOfRequests(); send({ "summary": i18n.t('show-requests.successfully-deleted-title'), "body": i18n.t('show-requests.successfully-deleted-text'), "type": "success", "timeout": 5, }); this.clearAll(); } else { // TODO error handling send({ "summary": 'Error!', "body": 'Could not delete request.', "type": "danger", "timeout": 5, }); } } } finally { this._('#delete-all-btn').stop(); } } createFormattedFilesList(list) { const i18n = this._i18n; let output = ''; list.forEach((file) => { output += file.name + "<br>"; }); if (output !== '') { return output; } else { return i18n.t('show-requests.no-files-attached'); } } createFormattedRecipientsList(list) { const i18n = this._i18n; let output = ''; list.forEach((recipient) => { output += recipient.familyName + ", " + recipient.givenName + "<br>"; }); if (output !== '') { return output; } else { return i18n.t('show-requests.no-recipients-added'); } } setControlsHtml(item) { let div = this.createScopedElement('div'); div.classList.add('tabulator-icon-buttons'); if (item.dateSubmitted || !this.mayWrite) { let btn = this.createScopedElement('dbp-icon-button'); btn.addEventListener('click', async event => { this.editRequest(event, item); event.stopPropagation(); }); btn.setAttribute('icon-name', 'keyword-research'); div.appendChild(btn); } else { let btn_edit = this.createScopedElement('dbp-icon-button'); btn_edit.addEventListener('click', async event => { this.editRequest(event, item); event.stopPropagation(); }); btn_edit.setAttribute('icon-name', 'pencil'); div.appendChild(btn_edit); let btn_delete = this.createScopedElement('dbp-icon-button'); btn_delete.addEventListener('click', async event => { this.deleteRequest(event, item); event.stopPropagation(); }); btn_delete.setAttribute('icon-name', 'trash'); div.appendChild(btn_delete); let btn_submit = this.createScopedElement('dbp-icon-button'); btn_submit.addEventListener('click', async event => { this.submitRequest(event, item); event.stopPropagation(); }); btn_submit.setAttribute('icon-name', 'send-diagonal'); div.appendChild(btn_submit); } return div; } createTableObject(list) { const i18n = this._i18n; let tableObject = []; list.forEach((item) => { let span = this.createScopedElement('span'); span.classList.add('muted'); span.textContent = i18n.t('show-requests.no-subject-found'); let content = { requestId: item.identifier, subject: item.name ? item.name : span, status: item.dateSubmitted ? i18n.t('show-requests.status-completed') : i18n.t('show-requests.empty-date-submitted'), dateCreated: item.dateCreated, details: "Details", // sender: item.senderFamilyName ? item.senderFamilyName + " " + item.senderGivenName + "<br>" // + item.senderStreetAddress + " " + item.senderBuildingNumber + "<br>" // + item.senderPostalCode + " " + item.senderAddressLocality + "<br>" // + item.senderAddressCountry : i18n.t('show-requests.empty-sender-text'), files: this.createFormattedFilesList(item.files), recipients: this.createFormattedRecipientsList(item.recipients), dateSubmitted: item.dateSubmitted ? this.convertToReadableDate(item.dateSubmitted) : i18n.t('show-requests.date-submitted-not-submitted'), controls: this.setControlsHtml(item), }; tableObject.push(content); }); return tableObject; } /** * Get a list of all requests * * @returns {Array} list */ async getListOfRequests() { const i18n = this._i18n; this.initialRequestsLoading = !this._initialFetchDone; try { let response = await this.getListOfDispatchRequests(this.g