All files / src/components ReportingActionModal.vue

100% Statements 39/39
100% Branches 36/36
100% Functions 9/9
100% Lines 38/38

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 143 144 145 146 147 148 149 150 151 152 153 154    44x             1x   3x   3x                         1x     3x 3x             3x                           2x             3x         1x                                                               3x 3x 3x     28x   28x               28x 28x 28x   28x 28x 28x   28x   10x         28x 33x   5x 5x         5x 5x 4x 4x 4x 4x         3x 3x   1x   4x        
<template>
  <div
    v-if="userId && action"
    class="modal fade show d-block reporting-action-modal"
    role="dialog"
    aria-modal="true"
    aria-labelledby="reporting-action-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="reporting-action-title" class="modal-title">
            <font-awesome-icon
              :icon="action === 'warn' ? 'fa-solid fa-triangle-exclamation' : 'fa-solid fa-ban'"
              class="me-2"
            />
            {{ t(`report.admin.reporting.${action}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.admin.reporting.${action}Intro`) }}</p>
          <div v-if="action === 'suspend'" class="mb-3">
            <label for="reporting-days" class="form-label">
              {{ t('report.decision.daysLabel') }}
            </label>
            <input
              id="reporting-days"
              v-model.number="days"
              class="form-control w-auto"
              type="number"
              min="1"
              :max="MAX_DAYS"
              required
              :disabled="submitting"
            />
          </div>
          <label for="reporting-note" class="form-label">
            {{ t('report.admin.reporting.noteLabel') }}
          </label>
          <textarea
            id="reporting-note"
            v-model="note"
            class="form-control"
            rows="3"
            :maxlength="MAX_NOTE_LENGTH"
            :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-warning reporting-submit"
            :disabled="!canSubmit || submitting"
          >
            <span
              v-if="submitting"
              class="spinner-border spinner-border-sm me-2"
              role="status"
              aria-hidden="true"
            />
            {{ t(`report.admin.reporting.${action}`) }}
          </button>
        </div>
      </form>
    </div>
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-REPORT-011.1
import { computed, ref, watch } from 'vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { useToastStore } from '@/stores/toast'
import { useAdminStore, type AdminReporterStanding, type ReportingAction } from '@/stores/admin'
import { getErrorMessage } from '@/utils/errors'
 
const MAX_DAYS = 365
const MAX_NOTE_LENGTH = 1000
const DEFAULT_DAYS = 30
 
// Open while a user and an action are set: warn a reporter, or suspend their reporting
const props = defineProps<{ userId: string | null; action: ReportingAction | null }>()
 
const emit = defineEmits<{
  close: []
  done: [
    userId: string,
    standing: Pick<AdminReporterStanding, 'reportingWarnedAt' | 'reportingSuspendedUntil'>
  ]
}>()
 
const { t } = useUILanguage()
const toastStore = useToastStore()
const adminStore = useAdminStore()
 
const days = ref(DEFAULT_DAYS)
const note = ref('')
const submitting = ref(false)
 
const canSubmit = computed(
  () =>
    props.action === 'warn' ||
    (Number.isInteger(days.value) && days.value >= 1 && days.value <= MAX_DAYS)
)
 
// Every action starts blank
watch(
  () => `${props.userId}:${props.action}`,
  () => {
    days.value = DEFAULT_DAYS
    note.value = ''
  }
)
 
async function submit() {
  const { userId, action } = props
  if (!userId || !action || !canSubmit.value || submitting.value) return
  submitting.value = true
  try {
    const trimmed = note.value.trim()
    const standing = await adminStore.restrictReporting(userId, {
      action,
      ...(action === 'suspend' && { days: days.value }),
      ...(trimmed && { note: trimmed })
    })
    toastStore.success(t(`report.admin.reporting.${action}Success`))
    emit('done', userId, standing)
  } catch (err) {
    toastStore.error(getErrorMessage(err, t('report.admin.reporting.error'), t))
  } finally {
    submitting.value = false
  }
}
</script>