All files / src/components AppealModal.vue

96.55% Statements 28/29
95.45% Branches 21/22
100% Functions 7/7
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112    48x             1x   2x   2x                   1x     2x 2x       5x                 2x         2x                                                                 31x   31x   31x 31x   31x 31x     31x 36x   5x         4x 4x 4x 4x 4x 3x 4x   1x   4x        
<template>
  <div
    v-if="appealRef"
    class="modal fade show d-block appeal-modal"
    role="dialog"
    aria-modal="true"
    aria-labelledby="appeal-modal-title"
    tabindex="-1"
    style="background-color: rgba(0, 0, 0, 0.5)"
    @click.self="!submitting && emit('close')"
  >
    <div class="modal-dialog modal-dialog-centered">
      <form class="modal-content" @submit.prevent="submit">
        <div class="modal-header">
          <h5 id="appeal-modal-title" class="modal-title">
            <font-awesome-icon icon="fa-solid fa-scale-balanced" class="me-2" />
            {{ t('appeal.title') }}
          </h5>
          <button
            type="button"
            class="btn-close"
            :aria-label="t('common.close')"
            :disabled="submitting"
            @click="emit('close')"
          ></button>
        </div>
        <div class="modal-body">
          <p class="text-muted small">{{ t('appeal.intro') }}</p>
          <label for="appeal-message" class="form-label">{{ t('appeal.messageLabel') }}</label>
          <textarea
            id="appeal-message"
            v-model="message"
            class="form-control"
            rows="5"
            required
            :maxlength="MAX_APPEAL_MESSAGE_LENGTH"
            :placeholder="t('appeal.messagePlaceholder')"
            :disabled="submitting"
          />
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary"
            :disabled="submitting"
            @click="emit('close')"
          >
            <font-awesome-icon icon="fa-solid fa-xmark" class="me-2" />
            {{ t('common.cancel') }}
          </button>
          <button
            type="submit"
            class="btn btn-primary appeal-send"
            :disabled="!message.trim() || submitting"
          >
            <span
              v-if="submitting"
              class="spinner-border spinner-border-sm me-2"
              role="status"
              aria-hidden="true"
            />
            {{ t('appeal.submit') }}
          </button>
        </div>
      </form>
    </div>
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-REPORT-009.1
import { ref, watch } from 'vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { useToastStore } from '@/stores/toast'
import { getErrorMessage } from '@/utils/errors'
import { MAX_APPEAL_MESSAGE_LENGTH, submitAppeal, type AppealRef } from '@/services/contentReports'
 
// Open while a decision is set: the one the user contests
const props = defineProps<{ appealRef: AppealRef | null }>()
 
const emit = defineEmits<{ close: []; appealed: [ref: AppealRef] }>()
 
const { t } = useUILanguage()
const toastStore = useToastStore()
 
const message = ref('')
const submitting = ref(false)
 
// Every appeal starts blank
watch(
  () => props.appealRef?.decisionId,
  () => {
    message.value = ''
  }
)
 
async function submit() {
  const appealRef = props.appealRef
  Iif (!appealRef || !message.value.trim() || submitting.value) return
  submitting.value = true
  try {
    const { alreadyAppealed } = await submitAppeal(appealRef, message.value)
    toastStore.success(t(alreadyAppealed ? 'appeal.alreadyAppealed' : 'appeal.success'))
    emit('appealed', appealRef)
  } catch (err) {
    toastStore.error(getErrorMessage(err, t('appeal.error'), t))
  } finally {
    submitting.value = false
  }
}
</script>