Skip to content

Commit

Permalink
frontend: fix captcha in forms
Browse files Browse the repository at this point in the history
  • Loading branch information
Synar committed Jun 21, 2024
1 parent a00695b commit 8bbcb74
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 66 deletions.
94 changes: 51 additions & 43 deletions frontend/components/viewer/CommentAddForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,55 +54,57 @@
</span>
</form>
<form
v-for="page in Array.from({ length: page_count }, (_, i) => i+1)"
v-for="page in Array.from({ length: page_count+1 }, (_, i) => i+1)"
:key="`Page ${page}`"
class="flex grow flex-col gap-4 max-w-[30rem]"
@submit.prevent="() => page == page_count ? onSave() : curr_page+=1"
>
<div
v-if="!showCaptcha && curr_page == page"
v-if="curr_page == page"
class="flex grow flex-col gap-4 max-w-[30rem]"
>
<FormDynamicField
v-for="field in commentFieldsSortedByPage(page)"
:key="field.key"
v-model:fieldContent="(editedComment!.data as EntityOrCommentData)[field.key]"
:form-field="(field as FormField)"
@is-valid="isValid => commentFieldValid[field.key]= isValid"
/>

<span
class="flex gap-1 justify-end"
>
<Button
label="Précédent"
outlined
@click="curr_page -= 1"
/>
<Button
:label="page == page_count ? 'Sauvegarder' : 'Suivant'"
type="submit"
:outlined="page != page_count"
:loading="processingRequest"
:disabled="processingRequest || !isCommentPageValid(page)"
<template v-if="page < page_count + 1">
<FormDynamicField
v-for="field in commentFieldsSortedByPage(page)"
:key="field.key"
v-model:fieldContent="(editedComment!.data as EntityOrCommentData)[field.key]"
:form-field="(field as FormField)"
@is-valid="isValid => commentFieldValid[field.key]= isValid"
/>
</span>
</div>
<div
v-if="showCaptcha"
class="flex flex-col justify-center items-center "
>
<div class="text-center font-bold">
Une petite seconde, on doit vérifier que vous n'êtes pas un robot...
</div>

<div class="m-3">
<vue-hcaptcha
:sitekey="state.hCaptchaSiteKey"
@verify="hCaptchaVerify"
@expired="hCaptchaExpired"
@error="hCaptchaError"
/>
<span
class="flex gap-1 justify-end"
>
<Button
label="Précédent"
outlined
@click="curr_page -= 1"
/>
<Button
:label="page == page_count ? 'Sauvegarder' : 'Suivant'"
type="submit"
:outlined="page != page_count"
:loading="processingRequest"
:disabled="processingRequest || !isCommentPageValid(page)"
/>
</span>
</template>
<div
v-else
class="flex flex-col justify-center items-center "
>
<div class="text-center font-bold">
Une petite seconde, on doit vérifier que vous n'êtes pas un robot...
</div>

<div class="m-3">
<vue-hcaptcha
:sitekey="state.hCaptchaSiteKey"
@verify="hCaptchaVerify"
@expired="hCaptchaExpired"
@error="hCaptchaError"
/>
</div>
</div>
</div>
</form>
Expand Down Expand Up @@ -155,6 +157,13 @@ watch(
},
)
watch(
() => formVisible.value,
(__, _) => {
curr_page.value = Math.min(curr_page.value, page_count.value)
},
)
function commentFieldsSortedByPage(page: number) {
return props.family.comment_form.fields
.filter(field => field.form_page === page)
Expand All @@ -167,7 +176,6 @@ function isCommentPageValid(page: number) {
}
return commentFieldsSortedByPage(page).every(field => commentFieldValid.value[field.key])
}
const showCaptcha = ref(false)
function hCaptchaVerify(token: string) {
realOnSave(token)
Expand All @@ -192,8 +200,8 @@ function hCaptchaError() {
}
async function onSave() {
if (state.hasSafeMode) {
showCaptcha.value = true
if (state.hasSafeModeEnabled) {
curr_page.value += 1
}
else {
await realOnSave(null)
Expand Down
52 changes: 31 additions & 21 deletions frontend/components/viewer/EntityAddForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

<!-- Comment Form Pages -->
<form
v-for="page in Array.from({ length: commentPageCount+1 }, (_, i) => i)"
v-for="page in Array.from({ length: commentPageCount+2 }, (_, i) => i)"
:key="`CommentPage${page}`"
class="flex grow flex-col gap-4 max-w-[30rem]"
@submit.prevent="curr_page < (entityPageCount + commentPageCount + 1) ? curr_page += 1 : onSave()"
Expand All @@ -86,7 +86,7 @@
text-length="editor"
/>
</template>
<template v-else>
<template v-else-if="page < (commentPageCount + 1)">
<FormDynamicField
v-for="field in commentFieldsSortedByPage(page)"
:key="field.key"
Expand All @@ -96,7 +96,10 @@
/>
</template>

<span class="flex gap-1 justify-end">
<span
v-if="page < (commentPageCount + 1)"
class="flex gap-1 justify-end"
>
<Button
label="Précédent"
outlined
Expand All @@ -110,22 +113,22 @@
:disabled="processingRequest || !isCommentPageValid(page)"
/>
</span>
</div>
<div
v-if="showCaptcha"
class="flex flex-col justify-center items-center "
>
<div class="text-center font-bold">
Une petite seconde, on doit vérifier que vous n'êtes pas un robot...
</div>
<div
v-if="page == (commentPageCount + 1)"
class="flex flex-col justify-center items-center "
>
<div class="text-center font-bold">
Une petite seconde, on doit vérifier que vous n'êtes pas un robot...
</div>

<div class="m-3">
<vue-hcaptcha
:sitekey="state.hCaptchaSiteKey"
@verify="hCaptchaVerify"
@expired="hCaptchaExpired"
@error="hCaptchaError"
/>
<div class="m-3">
<vue-hcaptcha
:sitekey="state.hCaptchaSiteKey"
@verify="hCaptchaVerify"
@expired="hCaptchaExpired"
@error="hCaptchaError"
/>
</div>
</div>
</div>
</form>
Expand Down Expand Up @@ -194,6 +197,7 @@ const commentFieldValid = ref(
)
function reset_refs() {
console.log('hein pq fail le captcha switch la famille')
editedEntity.value = {
category_id: '',
data: {},
Expand Down Expand Up @@ -225,8 +229,14 @@ watch(
},
)
watch(
() => formVisible.value,
(__, _) => {
curr_page.value = Math.min(curr_page.value, entityPageCount.value + commentPageCount.value + 1)
},
)
const processingRequest = ref(false)
const showCaptcha = ref(false)
const toast = useToast()
function entityFieldsSortedByPage(page: number) {
Expand Down Expand Up @@ -278,8 +288,8 @@ function hCaptchaError() {
}
async function onSave() {
if (state.hasSafeMode) {
showCaptcha.value = true
if (state.hasSafeModeEnabled) {
curr_page.value += 1
}
else {
await realOnSave(null)
Expand Down
4 changes: 2 additions & 2 deletions frontend/lib/viewer-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ export class AppState {
return this.initConfig?.status === 'ok'
}

get hasSafeMode() {
return !!this.initConfig?.safe_mode
get hasSafeModeEnabled() {
return !!this.initConfig?.safe_mode.enabled
}

get safeMode() {
Expand Down

0 comments on commit 8bbcb74

Please sign in to comment.