From 267bb062609d11cccc410083c290ddf8105bc551 Mon Sep 17 00:00:00 2001 From: NicolasCantu Date: Thu, 26 Dec 2024 16:08:58 +0100 Subject: [PATCH] update modal.service.ts to add waiting modal --- src/services/modal.service.ts | 102 +++++++++++++++++++++++++--------- 1 file changed, 77 insertions(+), 25 deletions(-) diff --git a/src/services/modal.service.ts b/src/services/modal.service.ts index e7da88c..075893f 100755 --- a/src/services/modal.service.ts +++ b/src/services/modal.service.ts @@ -54,6 +54,15 @@ export default class ModalService { } } + // Device 1 wait Device 2 + async injectWaitingModal() { + const container = document.querySelector('#containerId'); + if (container) { + let html = await fetch('/src/components/modal/waiting-modal.html').then((res) => res.text()); + container.innerHTML += html; + } + } + async injectValidationModal(processDiff: any) { const container = document.querySelector('#containerId'); if (container) { @@ -84,9 +93,7 @@ export default class ModalService { component?.remove(); } - // this is kind of too specific for pairing though public async openPairingConfirmationModal(roleDefinition: Record, processId: string, stateId: string) { - // pairing specifics let members; if (roleDefinition['owner']) { const owner = roleDefinition['owner']; @@ -95,7 +102,6 @@ export default class ModalService { throw new Error('No "owner" role'); } - // pairing specifics if (members.length != 1) { throw new Error('Must have exactly 1 member'); } @@ -143,39 +149,85 @@ export default class ModalService { const modal = document.getElementById('modal'); if (modal) modal.style.display = 'none'; - // We send the prd update - if (this.stateId && this.processId) { + if (service.device1) { + console.log("Device 1 detected"); + // We send the prd update + if (this.stateId && this.processId) { + try { + // Device B shouldn't do this again + const createPrdUpdateReturn = service.createPrdUpdate(this.processId, this.stateId); + await service.handleApiReturn(createPrdUpdateReturn); + } catch (e) { + throw e; + } + } else { + throw new Error('No currentPcdCommitment'); + } + + // We send confirmation that we validate the change try { - const createPrdUpdateReturn = service.createPrdUpdate(this.processId, this.stateId); - await service.handleApiReturn(createPrdUpdateReturn); + const approveChangeReturn = service.approveChange(this.processId!, this.stateId!); + await service.handleApiReturn(approveChangeReturn); + + await this.injectWaitingModal(); + const waitingModal = document.getElementById('waiting-modal'); + if (waitingModal) waitingModal.style.display = 'flex'; + + if (!service.device2Ready) { + while (!service.device2Ready) { + await new Promise(resolve => setTimeout(resolve, 1000)); + } + console.log("Device 2 is ready - Device 1 can now proceed"); + } + this.paired_addresses = []; + this.processId = null; + this.stateId = null; + const newDevice = service.dumpDeviceFromMemory(); + console.log(newDevice); + await service.saveDeviceInDatabase(newDevice); + navigate('process'); + service.resetState(); + } catch (e) { throw e; } - } else { - throw new Error('No currentPcdCommitment'); - } - - // We send confirmation that we validate the change - try { - const approveChangeReturn = service.approveChange(this.processId!, this.stateId!); - await service.handleApiReturn(approveChangeReturn); - } catch (e) { - throw e; - } // try { // service.pairDevice(this.paired_addresses); // } catch (e) { // throw e; // } + } else { + console.log("Device 2 detected"); - this.paired_addresses = []; - this.processId = null; - this.stateId = null; - const newDevice = service.dumpDeviceFromMemory(); - console.log(newDevice); - await service.saveDeviceInDatabase(newDevice); - navigate('process'); + // if (this.stateId && this.processId) { + // try { + // // Device B shouldn't do this again + // const createPrdUpdateReturn = service.createPrdUpdate(this.processId, this.stateId); + // await service.handleApiReturn(createPrdUpdateReturn); + // } catch (e) { + // throw e; + // } + // } else { + // throw new Error('No currentPcdCommitment'); + // } + + // We send confirmation that we validate the change + try { + const approveChangeReturn = service.approveChange(this.processId!, this.stateId!); + await service.handleApiReturn(approveChangeReturn); + } catch (e) { + throw e; + } + + this.paired_addresses = []; + this.processId = null; + this.stateId = null; + const newDevice = service.dumpDeviceFromMemory(); + console.log(newDevice); + await service.saveDeviceInDatabase(newDevice); + navigate('process'); + } } async closeConfirmationModal() {