|
| 1 | +class noWidget { |
| 2 | + #origin = window.location.hostname; |
| 3 | + #baseUrl; |
| 4 | + #loader = 'Carregando Widget...'; |
| 5 | + #mounted = false; |
| 6 | + #wrapper = null; |
| 7 | + #requiredFields = {}; |
| 8 | + #documentKeys = []; |
| 9 | + |
| 10 | + constructor(uuid, env = 'production', options = { domain: '.clicksign.com', show_files: false, pdf: false }) { |
| 11 | + let subDomain = env; |
| 12 | + let baseDomain = options.domain; |
| 13 | + |
| 14 | + if (env === 'production') { |
| 15 | + subDomain = 'app'; |
| 16 | + } else if (/staging\d+/.test(env)) { |
| 17 | + subDomain = env.match(/\d+/)[0]; |
| 18 | + baseDomain = '.clicksign.dev'; |
| 19 | + } |
| 20 | + |
| 21 | + this.#baseUrl = `https://${subDomain}${baseDomain}/notarial/embedded_signature/signatures/${uuid}`; |
| 22 | + this.options = options; |
| 23 | + } |
| 24 | + |
| 25 | + mount({ containerId }) { |
| 26 | + this.#wrapper = document.getElementById(containerId); |
| 27 | + |
| 28 | + if (this.#wrapper === null) { |
| 29 | + this.#mounted = false; |
| 30 | + throw new Error("It's not possible to render widget."); |
| 31 | + } |
| 32 | + |
| 33 | + this.#wrapper.innerHTML = this.#loader; |
| 34 | + this.#getDocuments(this.#wrapper); |
| 35 | + this.#mounted = true; |
| 36 | + } |
| 37 | + |
| 38 | + attach(event, elID, method) { |
| 39 | + const el = document.getElementById(elID); |
| 40 | + el.addEventListener(event, method); |
| 41 | + } |
| 42 | + |
| 43 | + async sign(info) { |
| 44 | + if (!this.#mounted) { throw new Error('Widget not mounted.'); } |
| 45 | + if (this.#requiredFields) { this.#validateFields(info); } |
| 46 | + |
| 47 | + try { |
| 48 | + const response = await fetch(`${this.#baseUrl}/sign`, { |
| 49 | + method: 'POST', |
| 50 | + body: JSON.stringify(info), |
| 51 | + headers: { |
| 52 | + 'Origin': this.#origin, |
| 53 | + 'Content-Type': 'application/json' |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + const responseText = await response.text(); |
| 58 | + if (response.status === 200 && !responseText) { |
| 59 | + return { status: 'success' }; |
| 60 | + } |
| 61 | + |
| 62 | + if (!responseText) { |
| 63 | + throw new Error('Empty response from server'); |
| 64 | + } |
| 65 | + |
| 66 | + let data = JSON.parse(responseText); |
| 67 | + |
| 68 | + if (data.status === 'error') { |
| 69 | + throw data.error; |
| 70 | + } |
| 71 | + |
| 72 | + return data; |
| 73 | + } catch (error) { |
| 74 | + console.error("Verifique os parâmetros da requisição e tente novamente.", error); |
| 75 | + throw error; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + injectLoader(el) { |
| 80 | + this.#loader = el; |
| 81 | + } |
| 82 | + |
| 83 | + getDocumentKeys() { |
| 84 | + return this.#documentKeys; |
| 85 | + } |
| 86 | + |
| 87 | + #validateFields(info) { |
| 88 | + const { signature } = info; |
| 89 | + if (!signature) { throw new Error("Object signature is required."); } |
| 90 | + if (!signature.signer) { throw new Error("Object signature.signer is required."); } |
| 91 | + |
| 92 | + const requiredAttributes = Object.keys(this.#requiredFields) |
| 93 | + .filter((field) => this.#requiredFields[field]); |
| 94 | + |
| 95 | + if (!(requiredAttributes.every((attr) => signature.signer[attr]))) { |
| 96 | + throw new Error(`Can't sign without required fields.️ ${requiredAttributes}`); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + #createDocumentLink(doc, strMatch) { |
| 101 | + let filename = this.options.pdf ? doc.pdf_name : doc.name; |
| 102 | + let download_url = this.options.pdf ? doc.pdf_url : doc.original_url; |
| 103 | + |
| 104 | + let docLink = document.createElement('a'); |
| 105 | + docLink.innerHTML = strMatch && `${strMatch[1]}`; |
| 106 | + docLink.className = 'cs-acceptance-preview'; |
| 107 | + docLink.setAttribute('download', 'true'); |
| 108 | + docLink.textContent = filename; |
| 109 | + docLink.href = download_url; |
| 110 | + |
| 111 | + return docLink; |
| 112 | + } |
| 113 | + |
| 114 | + async #getDocuments(wrapper) { |
| 115 | + try { |
| 116 | + const response = await fetch(`${this.#baseUrl}/documents`, { |
| 117 | + headers: { |
| 118 | + 'Origin': this.#origin, |
| 119 | + 'Content-Type': 'application/json', |
| 120 | + }, |
| 121 | + }); |
| 122 | + |
| 123 | + const responseText = await response.text(); |
| 124 | + if (!responseText) { |
| 125 | + throw new Error('Empty response from server'); |
| 126 | + } |
| 127 | + |
| 128 | + let data; |
| 129 | + try { |
| 130 | + data = JSON.parse(responseText); |
| 131 | + } catch (e) { |
| 132 | + console.error('Failed to parse JSON:', responseText); |
| 133 | + throw new Error('Invalid JSON response from server'); |
| 134 | + } |
| 135 | + |
| 136 | + const { documents, error, required_fields } = data; |
| 137 | + |
| 138 | + if (!documents) { throw new Error(error.message); } |
| 139 | + |
| 140 | + this.#requiredFields = required_fields; |
| 141 | + this.#documentKeys = documents.map(doc => doc.key); |
| 142 | + wrapper.innerHTML = ''; |
| 143 | + |
| 144 | + this.#hideNonRequiredFields(); |
| 145 | + |
| 146 | + const regex = /\(\((.[^\)\)]*)\)\)/g; |
| 147 | + |
| 148 | + documents.forEach((doc) => { |
| 149 | + const docLink = this.#createDocumentLink(doc); |
| 150 | + wrapper.appendChild(docLink); |
| 151 | + }); |
| 152 | + } catch (error) { |
| 153 | + console.error('Failed to get content:', error); |
| 154 | + wrapper.innerHTML = 'Falha ao carregar documentos.'; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + #hideNonRequiredFields() { |
| 159 | + const fields = { |
| 160 | + name: document.getElementById('name').closest('.field'), |
| 161 | + documentation: document.getElementById('cpf-field-container'), |
| 162 | + birthday: document.getElementById('birthdate-field-container') |
| 163 | + }; |
| 164 | + |
| 165 | + for (const fieldName in fields) { |
| 166 | + if (fields.hasOwnProperty(fieldName)) { |
| 167 | + const fieldElement = fields[fieldName]; |
| 168 | + if (fieldElement && !this.#requiredFields[fieldName]) { |
| 169 | + fieldElement.classList.add('hidden-field'); |
| 170 | + } else if (fieldElement) { |
| 171 | + fieldElement.classList.remove('hidden-field'); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments