All files / src/components MissingNicknameModal.vue

95.65% Statements 22/23
92% Branches 23/25
100% Functions 3/3
100% Lines 21/21

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    24x               2x   2x           2x     2x                   4x                                 2x                                               13x 13x 13x 13x 13x           14x     2x   2x 2x 2x 1x 1x   1x   2x        
<template>
  <div
    v-if="isOpen"
    class="modal fade show d-block"
    role="dialog"
    aria-modal="true"
    aria-labelledby="missing-nickname-title"
    tabindex="-1"
    style="background-color: rgba(0, 0, 0, 0.5)"
  >
    <div class="modal-dialog modal-dialog-centered">
      <form class="modal-content" @submit.prevent="save">
        <div class="modal-header">
          <h5 id="missing-nickname-title" class="modal-title">
            <font-awesome-icon icon="fa-solid fa-user" class="me-2" />
            {{ t('profile.missingNicknameTitle') }}
          </h5>
        </div>
        <div class="modal-body">
          <p>{{ t('profile.missingNicknameMessage') }}</p>
          <label for="missing-nickname-input" class="form-label">{{ t('auth.nickname') }}</label>
          <div class="input-group">
            <span class="input-group-text" aria-hidden="true">@</span>
            <input
              id="missing-nickname-input"
              class="form-control"
              type="text"
              name="nickname"
              autocomplete="off"
              maxlength="20"
              :value="nickname"
              @input="onNicknameInput"
            />
          </div>
          <div v-if="status === 'checking'" class="form-text">
            {{ t('auth.nicknameChecking') }}
          </div>
          <div v-else-if="status === 'available'" class="form-text text-success">
            {{ t('auth.nicknameAvailable') }}
          </div>
          <div v-else-if="status === 'taken'" class="form-text text-danger">
            {{ t('auth.nicknameTaken') }}
          </div>
          <div v-else-if="status === 'invalid'" class="form-text text-danger">
            {{ t('auth.nicknameInvalid') }}
          </div>
          <div v-else class="form-text">{{ t('auth.nicknameHint') }}</div>
        </div>
        <div class="modal-footer">
          <button
            type="submit"
            class="btn btn-primary"
            :disabled="status !== 'available' || isSaving"
          >
            <font-awesome-icon icon="fa-solid fa-check" class="me-2" />
            {{ t('common.save') }}
          </button>
        </div>
      </form>
    </div>
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-PROFILE-005.1
import { computed, ref } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { useToastStore } from '@/stores/toast'
import { useUILanguage } from '@/composables/useUILanguage'
import { useNicknameAvailability } from '@/composables/useNicknameAvailability'
import api from '@/services/api'
 
const { t } = useUILanguage()
const authStore = useAuthStore()
const toastStore = useToastStore()
const { nickname, status, onNicknameInput } = useNicknameAvailability()
const isSaving = ref(false)
 
// Sign-up asks for a nickname but only claims it once logged in, so it is lost
// whenever that step is skipped (confirmation from another page, failed claim)
// and Google sign-in never asks at all. Strictly null: an API that does not
// return the field yet (undefined) must not lock every user out.
const isOpen = computed(() => !!authStore.profile && authStore.profile.nickname === null)
 
async function save() {
  Iif (status.value !== 'available' || !authStore.profile) return
 
  isSaving.value = true
  try {
    const response = await api.put('/v1/users/me/nickname', { nickname: nickname.value })
    authStore.profile = { ...authStore.profile, nickname: response.data.nickname }
    toastStore.success(t('profile.nicknameSaved'))
  } catch (error: any) {
    toastStore.error(error.response?.data?.error || t('profile.nicknameSaveError'))
  } finally {
    isSaving.value = false
  }
}
</script>