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,39 +149,85 @@ 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';
// We send the prd update if (service.device1) {
if (this.stateId && this.processId) { 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 { try {
const createPrdUpdateReturn = service.createPrdUpdate(this.processId, this.stateId); const approveChangeReturn = service.approveChange(this.processId!, this.stateId!);
await service.handleApiReturn(createPrdUpdateReturn); 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;
} }
} 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 { // try {
// service.pairDevice(this.paired_addresses); // service.pairDevice(this.paired_addresses);
// } catch (e) { // } catch (e) {
// throw e; // throw e;
// } // }
} else {
console.log("Device 2 detected");
this.paired_addresses = []; // if (this.stateId && this.processId) {
this.processId = null; // try {
this.stateId = null; // // Device B shouldn't do this again
const newDevice = service.dumpDeviceFromMemory(); // const createPrdUpdateReturn = service.createPrdUpdate(this.processId, this.stateId);
console.log(newDevice); // await service.handleApiReturn(createPrdUpdateReturn);
await service.saveDeviceInDatabase(newDevice); // } catch (e) {
navigate('process'); // 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() { async closeConfirmationModal() {