Index.vue 3.51 KB
<template>
  <div>
    <h1 class="mb-8 font-bold text-3xl">Tokens</h1>
    <div class="mb-6 flex justify-between items-center">

      <search-filter v-model="form.login" class="w-full max-w-md mr-4" @reset="reset">
        <label class="block text-gray-700">Тип:</label>
        <select v-model="form.type" class="mt-1 w-full form-select">
          <option :value="null" />
          <option v-for="type,key in types" :value="key">{{ type }}</option>
        </select>
      </search-filter>

      <inertia-link class="btn-indigo" :href="route('token.get', {'api': 'yd'})">
        <span>Добавить </span>
        <span class="hidden md:inline">токен</span>
      </inertia-link>
    </div>
    <div class="bg-white rounded-md shadow overflow-x-auto">
      <table class="w-full whitespace-nowrap">
        <tr class="text-left font-bold">
          <th class="px-6 pt-6 pb-4">Login</th>
          <th class="px-6 pt-6 pb-4">АПИ</th>
          <th class="px-6 pt-6 pb-4">Баллы</th>
          <th class="px-6 pt-6 pb-4" colspan="2">Тип</th>
        </tr>
        <tr v-for="token in tokens.data" :key="token.id" class="hover:bg-gray-100 focus-within:bg-gray-100">
          <td class="border-t">
            <inertia-link class="px-6 py-4 flex items-center focus:text-indigo-500" :href="route('token.edit', token.id)">
              {{ token.login }}
            </inertia-link>
          </td>
          <td class="border-t">
            <inertia-link class="px-6 py-4 flex items-center" :href="route('token.edit', token.id)" tabindex="-1">
              {{ token.api }}
            </inertia-link>
          </td>
          <td class="border-t">
            <inertia-link class="px-6 py-4 flex items-center" :href="route('token.edit', token.id)" tabindex="-1">
              {{ token.limit }}
            </inertia-link>
          </td>
          <td class="border-t">
            <inertia-link class="px-6 py-4 flex items-center" :href="route('token.edit', token.id)" tabindex="-1">
              {{ types[token.type] }}
            </inertia-link>
          </td>
          <td class="border-t w-px">
            <inertia-link class="px-4 flex items-center" :href="route('token.edit', token.id)" tabindex="-1">
              <icon name="cheveron-right" class="block w-6 h-6 fill-gray-400" />
            </inertia-link>
          </td>
        </tr>
        <tr v-if="tokens.data.length === 0">
          <td class="border-t px-6 py-4" colspan="4">No tokens found.</td>
        </tr>
      </table>
    </div>
    <pagination class="mt-6" :links="tokens.links" />
  </div>
</template>

<script>
import Icon from '@/Shared/Icon'
import pickBy from 'lodash/pickBy'
import Layout from '@/Shared/Layout'
import throttle from 'lodash/throttle'
import mapValues from 'lodash/mapValues'
import Pagination from '@/Shared/Pagination'
import SearchFilter from '@/Shared/SearchFilter'

export default {
  metaInfo: { title: 'Tokens' },
  components: {
    Icon,
    Pagination,
    SearchFilter,
  },
  layout: Layout,
  props: {
    tokens: Object,
    filters: Object,
    types: Object,
  },
  data() {
    return {
      form: {
        login: this.filters.login,
        type: this.filters.type,
      },
    }
  },
  watch: {
    form: {
      handler: throttle(function() {
        let query = pickBy(this.form)
        this.$inertia.replace(this.route('tokens', Object.keys(query).length ? query : { remember: 'forget' }))
      }, 150),
      deep: true,
    },
  },
  methods: {
    reset() {
      this.form = mapValues(this.form, () => null)
    },
  },
}
</script>