All files / src/components ReportModal.vue

100% Statements 36/36
96.15% Branches 25/26
100% Functions 10/10
100% Lines 35/35

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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142    194x             1x   8x   8x                   1x     8x 8x 8x 8x 165x     5x                             3x                 8x         1x                                                                               168x   168x   168x 168x   168x 168x 168x   168x 17x         168x 178x   9x 9x         5x 4x 4x 4x 2x 5x   2x   4x        
<template>
  <div
    v-if="target"
    class="modal fade show d-block report-modal"
    role="dialog"
    aria-modal="true"
    aria-labelledby="report-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="report-modal-title" class="modal-title">
            <font-awesome-icon icon="fa-solid fa-flag" class="me-2" />
            {{ 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('report.intro') }}</p>
          <fieldset class="mb-3">
            <legend class="form-label fs-6">{{ t('report.reasonLabel') }}</legend>
            <div v-for="option in REPORT_REASONS" :key="option" class="form-check">
              <input
                :id="`report-reason-${option}`"
                v-model="reason"
                class="form-check-input"
                type="radio"
                name="report-reason"
                :value="option"
                :disabled="submitting"
              />
              <label class="form-check-label" :for="`report-reason-${option}`">
                {{ t(`report.reasons.${option}`) }}
              </label>
            </div>
          </fieldset>
          <label for="report-details" class="form-label">{{ t('report.detailsLabel') }}</label>
          <textarea
            id="report-details"
            v-model="details"
            class="form-control"
            rows="3"
            name="report-details"
            :maxlength="MAX_REPORT_DETAILS_LENGTH"
            :placeholder="t('report.detailsPlaceholder')"
            :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-danger report-submit"
            :disabled="!reason || submitting"
          >
            <span
              v-if="submitting"
              class="spinner-border spinner-border-sm me-2"
              role="status"
              aria-hidden="true"
            />
            <font-awesome-icon v-else icon="fa-solid fa-flag" class="me-2" />
            {{ t('report.submit') }}
          </button>
        </div>
      </form>
    </div>
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-REPORT-001.1, UC-REPORT-002.1, UC-REPORT-003.1
import { computed, ref, watch } from 'vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { useToastStore } from '@/stores/toast'
import { getErrorMessage } from '@/utils/errors'
import {
  MAX_REPORT_DETAILS_LENGTH,
  REPORT_REASONS,
  submitReport,
  type ReportReason,
  type ReportTarget
} from '@/services/contentReports'
 
// Open while a target is set: what the user is reporting
const props = defineProps<{ target: ReportTarget | null }>()
 
const emit = defineEmits<{ close: [] }>()
 
const { t } = useUILanguage()
const toastStore = useToastStore()
 
const reason = ref<ReportReason | null>(null)
const details = ref('')
const submitting = ref(false)
 
const title = computed(() =>
  props.target ? t(`report.title.${props.target.type}`) : t('report.action')
)
 
// Every report starts blank. Watched as open/closed, not by identity: a parent
// passing an inline target object re-creates it on each of its renders.
watch(
  () => !!props.target,
  () => {
    reason.value = null
    details.value = ''
  }
)
 
async function submit() {
  if (!props.target || !reason.value || submitting.value) return
  submitting.value = true
  try {
    const { alreadyReported } = await submitReport(props.target, reason.value, details.value)
    toastStore.success(t(alreadyReported ? 'report.alreadyReported' : 'report.success'))
    emit('close')
  } catch (err) {
    toastStore.error(getErrorMessage(err, t('report.error'), t))
  } finally {
    submitting.value = false
  }
}
</script>