Edit.vue 8.26 KB
<template>
    <div>
        <h1 class="mb-8 font-bold text-3xl">
            <inertia-link class="text-indigo-400 hover:text-indigo-600" :href="route('tokens')">Tokens</inertia-link>
            <span class="text-indigo-400 font-medium">/</span>
            {{ form.login }}
        </h1>
        <div class="bg-white rounded-md shadow overflow-hidden max-w-3xl">
            <form @submit.prevent="tokenUpdate">
                <div class="p-8 -mr-6 -mb-8 flex flex-wrap">
                    <text-input v-model="form.login" :error="form.errors.login" :readonly="true" class="pr-6 pb-8 w-full lg:w-1/2"
                                label="Login"/>
                    <text-input v-model="form.token" :error="form.errors.token" :readonly="true" class="pr-6 pb-8 w-full lg:w-1/2"
                                label="Token"/>
                    <text-input v-model="form.api" :error="form.errors.api" :readonly="true" class="pr-6 pb-8 w-full lg:w-1/2"
                                label="API"/>
                    <select-input v-model="form.type" :error="form.errors.type"
                                  :readonly="form.type"
                                  class="pr-6 pb-8 w-full lg:w-1/2"
                                  label="Type">
                        <option :value="null"/>
                        <option v-for="type,key in types" :value="key">{{ type }}</option>
                    </select-input>
                </div>
                <div class="px-8 py-4 bg-gray-50 border-t border-gray-100 flex items-center">
                    <button class="text-red-600 hover:underline"
                            tabindex="-1"
                            type="button"
                            @click="tokenDestroy"
                    >
                        Delete Token
                    </button>
                    <loading-button :loading="form.processing" v-if="!this.token.type" class="btn-indigo ml-auto" type="submit">
                        Update Token
                    </loading-button>
                </div>
            </form>
        </div>
        <div v-if="campaigns.length || token.campaigns.length">
            <h2 class="mt-12 font-bold text-2xl">Campaigns</h2>
            <div class="mt-4 flex flex-wrap">
                <form v-if="campaigns.length" @submit.prevent="campaignAdd" class="flex flex-wrap">
                    <loading-button :loading="campaign_add_form.processing"
                                    class="btn-indigo hover:underline"
                                    tabindex="-1"
                                    type="submit"
                    >
                        Add
                    </loading-button>
                    <select-input v-model="campaign_add_form.campaign_id"
                                  :error="campaign_add_form.errors.campaign_id"
                                  :readonly="campaign_add_form.campaign_id"
                                  class="px-6"
                    >
                        <option v-for="campaign in campaigns" :value="campaign.id">
                            {{ campaign.name }}
                        </option>
                    </select-input>
                </form>
            </div>
            <div class="mt-4 flex flex-wrap">
            </div>
            <div class="mt-6 bg-white rounded shadow overflow-x-auto">
                <table class="w-full whitespace-nowrap">
                    <tr class="text-left font-bold">
                        <th class="px-6 pt-6 pb-4">Name</th>
                        <th class="px-6 pt-6 pb-4">Action</th>
                    </tr>
                    <tr v-for="campaign in token.campaigns" :key="campaign.id"
                        class="hover:bg-gray-100 focus-within:bg-gray-100">
                        <td class="border-t py-3">
                            {{ campaign.name }}
                        </td>
                        <td class="border-t py-3">
                            <input :id="'campaign-enabled-' + campaign.id" :checked="campaign.enabled"
                                   @change="campaignEnabled(campaign.id, !campaign.enabled)"
                                   type="checkbox"
                            >
                            <label :for="'campaign-enabled-' + campaign.id">Enabled</label>
                            <button class="text-red-600 hover:underline"
                                    tabindex="-1"
                                    type="button"
                                    @click="campaignDestroy(campaign.id)"
                            >
                                Delete Campaign
                            </button>
                        </td>
                    </tr>
                    <tr v-if="token.campaigns.length === 0">
                        <td class="border-t px-6 py-4" colspan="4">No campaigns found.</td>
                    </tr>
                </table>
            </div>
        </div>

        <div v-if="token.cities">
            <h2 class="mt-12 font-bold text-2xl">Cities</h2>
            <div class="mt-6 bg-white rounded shadow overflow-x-auto">
                <table class="w-full whitespace-nowrap">
                    <tr class="text-left font-bold">
                        <th class="px-6 pt-6 pb-4">Name</th>
                    </tr>
                    <tr v-for="city in token.cities" :key="city.id"
                        class="hover:bg-gray-100 focus-within:bg-gray-100">
                        <td class="border-t">
                            {{ city.name }}
                        </td>
                    </tr>
                    <tr v-if="token.cities.length === 0">
                        <td class="border-t px-6 py-4" colspan="4">No City found.</td>
                    </tr>
                </table>
            </div>
        </div>

    </div>
</template>

<script>
import Icon from '@/Shared/Icon'
import Layout from '@/Shared/Layout'
import TextInput from '@/Shared/TextInput'
import SelectInput from '@/Shared/SelectInput'
import LoadingButton from '@/Shared/LoadingButton'
import TrashedMessage from '@/Shared/TrashedMessage'

export default {
    metaInfo() {
        return {title: this.form.login}
    },
    components: {
        Icon,
        LoadingButton,
        SelectInput,
        TextInput,
        TrashedMessage,
    },
    layout: Layout,
    props: {
        campaigns: Array,
        token: Object,
        types: Object,
    },
    remember: 'form',
    data() {
        return {
            form: this.$inertia.form({
                id: this.token.id,
                login: this.token.login,
                token: this.token.token,
                api: this.token.api,
                type: this.token.type,
            }),
            campaign_add_form: this.$inertia.form({
                campaign_id: null,
            }),
            campaigns_form: this.$inertia.form(),
        }
    },
    methods: {
        tokenUpdate() {
            this.form.post(this.route('token.update', this.token.id))
        },
        tokenDestroy() {
            if (confirm('Are you sure you want to delete this token?')) {
                this.$inertia.delete(this.route('token.destroy', this.token.id))
            }
        },
        campaignEnabled(campaign_id, enabled) {
            this.$inertia.post(this.route('token.campaign.enabled', [
                this.token.id,
                campaign_id,
                enabled ? 1 : 0,
            ]));
        },
        campaignDestroy(campaign_id) {
            if (confirm('Are you sure you want to delete this campaign?')) {
                this.$inertia.post(this.route('token.campaign.managed', [
                    this.token.id,
                    campaign_id,
                    0,
                ]));
            }
        },
        campaignAdd() {
            let campaign_id = this.campaign_add_form.campaign_id;

            if (!campaign_id) {
                return;
            }

            this.campaign_add_form.post(this.route('token.campaign.managed', [
                this.token.id,
                campaign_id,
                1,
            ]));

        },
        filterCampaigns(token_campaigns) {
            return this.campaigns.filter(
                campaign => token_campaigns.map((campaign) => campaign.id).indexOf(campaign.id) === -1
            )
        },
    },
}
</script>