Skip to content

Commit

Permalink
error on too small signature
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj committed Mar 10, 2024
1 parent e56dcb5 commit f023157
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 5 additions & 1 deletion app/javascript/submission_form/crop_canvas.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function cropCanvasAndExportToPNG (canvas) {
function cropCanvasAndExportToPNG (canvas, { errorOnTooSmall } = { errorOnTooSmall: false }) {
const ctx = canvas.getContext('2d')

const width = canvas.width
Expand Down Expand Up @@ -33,6 +33,10 @@ function cropCanvasAndExportToPNG (canvas) {
croppedCanvas.height = croppedHeight
const croppedCtx = croppedCanvas.getContext('2d')

if (errorOnTooSmall && (croppedWidth < 20 || croppedHeight < 20)) {
return Promise.reject(new Error('Image too small'))
}

croppedCtx.drawImage(canvas, leftmost, topmost, croppedWidth, croppedHeight, 0, 0, croppedWidth, croppedHeight)

return new Promise((resolve, reject) => {
Expand Down
6 changes: 5 additions & 1 deletion app/javascript/submission_form/form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,11 @@ export default {
this.isSubmitting = false
})
}).catch(error => {
console.log(error)
if (error?.message === 'Image too small') {
alert('Signature is too small - please redraw.')
} else {
console.log(error)
}
}).finally(() => {
this.isSubmitting = false
})
Expand Down
10 changes: 6 additions & 4 deletions app/javascript/submission_form/signature_step.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<a
id="type_text_button"
href="#"
class="btn btn-outline btn-sm font-medium"
class="btn btn-outline btn-sm font-medium inline-flex flex-nowrap"
@click.prevent="toggleTextInput"
>
<IconTextSize :width="16" />
Expand All @@ -56,7 +56,7 @@
:data-tip="t('take_photo')"
>
<label
class="btn btn-outline btn-sm font-medium"
class="btn btn-outline btn-sm font-medium inline-flex flex-nowrap"
>
<IconCamera :width="16" />
<input
Expand Down Expand Up @@ -386,8 +386,8 @@ export default {
return Promise.resolve({})
}
return new Promise((resolve) => {
cropCanvasAndExportToPNG(this.$refs.canvas).then(async (blob) => {
return new Promise((resolve, reject) => {
cropCanvasAndExportToPNG(this.$refs.canvas, { errorOnTooSmall: true }).then(async (blob) => {
const file = new File([blob], 'signature.png', { type: 'image/png' })
if (this.isDirectUpload) {
Expand Down Expand Up @@ -429,6 +429,8 @@ export default {
return resolve(attachment)
})
}
}).catch((error) => {
return reject(error)
})
})
}
Expand Down

0 comments on commit f023157

Please sign in to comment.