All files / src/components/shared PositionPicker.vue

70.11% Statements 61/87
100% Branches 0/0
0% Functions 0/2
70.11% Lines 61/87

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 1081x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x   1x 1x 1x 1x 1x 1x       1x         1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                         1x 1x    
<template>
  <div>
    <!-- Selected positions, in priority order -->
    <div v-if="selected.length > 0" class="flex flex-wrap gap-1 mb-2">
      <span
        v-for="(code, index) in selected"
        :key="code"
        class="inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs font-medium bg-brand-100 text-brand-800"
      >
        {{ code }}
        <span
          v-if="index === 0"
          class="px-1 rounded bg-brand-600 text-white text-[10px] uppercase"
          >Primary</span
        >
        <button
          type="button"
          class="ml-0.5 text-brand-600 hover:text-brand-900"
          :aria-label="`Remove ${code}`"
          @click="toggle(code)"
        >
          &times;
        </button>
      </span>
    </div>
    <p v-else class="text-xs text-fg-muted mb-2">
      No positions selected. First pick becomes the primary position.
    </p>
 
    <!-- Grouped picker -->
    <div class="space-y-2">
      <div v-for="(codes, group) in positionGroups" :key="group">
        <div class="text-xs font-semibold text-fg-muted uppercase mb-1">
          {{ groupNames[group] }}
        </div>
        <div class="flex flex-wrap gap-1">
          <button
            v-for="code in codes"
            :key="code"
            type="button"
            class="px-2 py-1 rounded text-xs border"
            :class="
              selected.includes(code)
                ? 'bg-brand-600 text-white border-brand-600'
                : 'bg-card text-fg border-line hover:bg-surface-alt'
            "
            @click="toggle(code)"
          >
            {{ code }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script>
import { ref, watch } from 'vue';
import {
  GROUP_NAMES,
  POSITION_GROUPS,
  parsePositions,
} from '@/constants/positions';
 
export default {
  name: 'PositionPicker',
  props: {
    // Ordered array of position codes; index 0 = primary. A JSON string is
    // tolerated (user_profiles.positions is a TEXT column) and parsed.
    modelValue: {
      type: [Array, String],
      default: () => [],
    },
  },
  emits: ['update:modelValue'],
  setup(props, { emit }) {
    // Internal copy so rapid toggles don't read a stale prop (the parent's
    // v-model update lands asynchronously). parsePositions normalizes stale
    // data on the way in: JSON strings from user_profiles and legacy codes
    // (LCB -> CB) that predate the taxonomy migration.
    const selected = ref(parsePositions(props.modelValue));
    watch(
      () => props.modelValue,
      value => {
        const parsed = parsePositions(value);
        if (JSON.stringify(parsed) !== JSON.stringify(selected.value)) {
          selected.value = parsed;
        }
      }
    );
 
    const toggle = code => {
      selected.value = selected.value.includes(code)
        ? selected.value.filter(c => c !== code)
        : [...selected.value, code];
      emit('update:modelValue', [...selected.value]);
    };
 
    return {
      positionGroups: POSITION_GROUPS,
      groupNames: GROUP_NAMES,
      selected,
      toggle,
    };
  },
};
</script>