signature: check for signature state

This commit is contained in:
nobohan
2024-07-12 11:52:22 +02:00
parent 77da2c1ac6
commit 421226c0dc
3 changed files with 85 additions and 34 deletions

View File

@@ -32,7 +32,11 @@
v-if="signature.zones.length > 1"
>
<div class="col-4 gap-2 d-grid">
<button :disabled="zone < 1" class="btn btn-light btn-sm" @click="turn_signature(-1)">
<button
:disabled="zone < 1"
class="btn btn-light btn-sm"
@click="turn_signature(-1)"
>
{{ $t("last_sign_zone") }}
</button>
</div>
@@ -71,7 +75,7 @@
<canvas class="m-auto" id="canvas"></canvas>
</div>
<div class="col-12 p-4" id="action-buttons" v-if="!signed">
<div class="col-12 p-4" id="action-buttons" v-if="signedState !== 'signed'">
<div class="row">
<div class="col-6">
<button
@@ -109,7 +113,7 @@
<script setup lang="ts">
import { ref, Ref, reactive } from "vue";
import { Signature, SignatureZone } from "../../types";
import { Signature, SignatureZone, SignedState } from "../../types";
import { makeFetch } from "../../../../../ChillMainBundle/Resources/public/lib/api/apiMethods";
import * as pdfjsLib from "pdfjs-dist";
import {
@@ -134,7 +138,7 @@ pdfjsLib.GlobalWorkerOptions.workerSrc = "pdfjs-dist/build/pdf.worker.mjs";
const modalOpen: Ref<boolean> = ref(false);
const loading: Ref<boolean> = ref(false);
const signed: Ref<boolean> = ref(false);
const signedState: Ref<SignedState> = ref("pending");
const page: Ref<number> = ref(1);
const pageCount: Ref<number> = ref(0);
const zone: Ref<number> = ref(0);
@@ -235,10 +239,7 @@ const canvas_click = (e: PointerEvent, canvas: HTMLCanvasElement) =>
const ctx = canvas.getContext("2d");
if (ctx) {
set_page(page.value);
setTimeout(
() => draw_zone(z, ctx, canvas.width, canvas.height),
200
);
setTimeout(() => draw_zone(z, ctx, canvas.width, canvas.height), 200);
}
userSignatureZones.value = z;
}
@@ -318,28 +319,74 @@ const add_zones = (page: number) => {
}
};
const check_signature = () => {
const url = `/api/1.0/document/workflow/${signature.id}/check-signature`;
return makeFetch("GET", url)
.then((r) => {
signedState.value = r as SignedState;
if (signedState.value === "pending") {
check_for_ready();
}
})
.catch((error) => {
signedState.value = "error";
console.log('Error while checking the signature', error);
//TODO toast error
});
};
const maxTryForReady = 60; //2 minutes for trying to sign
let tryForReady = 0;
const stop_try_signing = () => {
loading.value = false;
modalOpen.value = false;
};
const check_for_ready = () => {
if (tryForReady > maxTryForReady) {
signedState.value = "error";
stop_try_signing();
console.log("Reached the maximum number of tentative to try signing");
//TODO toast error
}
if (signedState.value === "rejected") {
stop_try_signing();
console.log("Signature rejected by the server");
//TODO toast error
}
if (signedState.value === "canceled") {
stop_try_signing();
console.log("Signature canceledconsole.log('Error while posting the signature', error);");
//TODO toast error
}
if (signedState.value === "pending") {
tryForReady = tryForReady + 1;
setTimeout(() => check_signature(), 2000);
} else {
stop_try_signing();
if (signedState.value === "signed") {
//TODO recharger le document signé
}
}
};
const confirm_sign = () => {
console.log(userSignatureZones.value);
loading.value = true;
//TODO POST userSignatureZones to backend
const url = `/api/1.0/document/workflow/${signature.id}/signature-request`;
const body = {
storedObject: signature.storedObject,
zone: userSignatureZones.value,
};
makeFetch('POST', url, body)
.then(r => {
})
.catch((error) => {
});
// setTimeout(() => {
// loading.value = false;
// modalOpen.value = false;
// signed.value = true;
// }, 3000); // FAKE
makeFetch("POST", url, body)
.then((r) => {
check_for_ready();
})
.catch((error) => {
console.log('Error while posting the signature', error);
stop_try_signing();
//TODO toast
});
};
const undo_sign = async () => {