update modal.service.ts to add waiting modal

This commit is contained in:
NicolasCantu 2024-12-26 16:08:58 +01:00
parent 375b20466e
commit 267bb06260

View File

@ -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) { async injectValidationModal(processDiff: any) {
const container = document.querySelector('#containerId'); const container = document.querySelector('#containerId');
if (container) { if (container) {
@ -84,9 +93,7 @@ export default class ModalService {
component?.remove(); component?.remove();
} }
// this is kind of too specific for pairing though
public async openPairingConfirmationModal(roleDefinition: Record<string, RoleDefinition>, processId: string, stateId: string) { public async openPairingConfirmationModal(roleDefinition: Record<string, RoleDefinition>, processId: string, stateId: string) {
// pairing specifics
let members; let members;
if (roleDefinition['owner']) { if (roleDefinition['owner']) {
const owner = roleDefinition['owner']; const owner = roleDefinition['owner'];
@ -95,7 +102,6 @@ export default class ModalService {
throw new Error('No "owner" role'); throw new Error('No "owner" role');
} }
// pairing specifics
if (members.length != 1) { if (members.length != 1) {
throw new Error('Must have exactly 1 member'); throw new Error('Must have exactly 1 member');
} }
@ -143,9 +149,12 @@ export default class ModalService {
const modal = document.getElementById('modal'); const modal = document.getElementById('modal');
if (modal) modal.style.display = 'none'; if (modal) modal.style.display = 'none';
if (service.device1) {
console.log("Device 1 detected");
// We send the prd update // We send the prd update
if (this.stateId && this.processId) { if (this.stateId && this.processId) {
try { try {
// Device B shouldn't do this again
const createPrdUpdateReturn = service.createPrdUpdate(this.processId, this.stateId); const createPrdUpdateReturn = service.createPrdUpdate(this.processId, this.stateId);
await service.handleApiReturn(createPrdUpdateReturn); await service.handleApiReturn(createPrdUpdateReturn);
} catch (e) { } catch (e) {
@ -159,6 +168,26 @@ export default class ModalService {
try { try {
const approveChangeReturn = service.approveChange(this.processId!, this.stateId!); const approveChangeReturn = service.approveChange(this.processId!, this.stateId!);
await service.handleApiReturn(approveChangeReturn); 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) { } catch (e) {
throw e; throw e;
} }
@ -168,6 +197,28 @@ export default class ModalService {
// } catch (e) { // } catch (e) {
// throw e; // throw e;
// } // }
} else {
console.log("Device 2 detected");
// 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.paired_addresses = [];
this.processId = null; this.processId = null;
@ -177,6 +228,7 @@ export default class ModalService {
await service.saveDeviceInDatabase(newDevice); await service.saveDeviceInDatabase(newDevice);
navigate('process'); navigate('process');
} }
}
async closeConfirmationModal() { async closeConfirmationModal() {
const service = await Services.getInstance(); const service = await Services.getInstance();