All files / apps/wp-editor/app/pages settings.vue

0% Statements 0/43
0% Branches 0/14
0% Functions 0/24
0% Lines 0/35

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                                                                                                                                                                                                                                                                                                   
<template>
    <UDashboardPanel id="settings">
        <template #header>
            <UDashboardNavbar title="Settings" icon="i-lucide-settings-2">
                <template #right>
                    <UButton
                        v-if="Idraft"
                        icon="i-lucide-save"
                        label="Save"
                        :disabled="!dirty"
                        :loading="saving"
                        @click="save"
                    />
                </template>
            </UDashboardNavbar>
        </template>
 
        <template #body>
            <UAlert
                v-if="error"
                color="error"
                variant="subtle"
                icon="i-lucide-triangle-alert"
                title="The store can't be read"
                :description="errorMessage"
            />
 
            <div v-else-if="Idraft" class="flex w-full max-w-2xl flex-col gap-8">
                <section class="flex flex-col gap-3">
                    <h2 class="font-semibold">WordPress</h2>
                    <UPageCard
                        variant="subtle"
                        :ui="{ container: 'gap-y-0 divide-y divide-default' }"
                    >
                        <UFormField
                            label="Dry-run mode"
                            description="Check every write against WordPress without saving it."
                            orientation="horizontal"
                            class="pb-4"
                        >
                            <USwitch v-model="draft.dryRun" />
                        </UFormField>
 
                        <UFormField
                            label="Pause between articles"
                            :description="`Milliseconds a bulk run waits before the next article, so the host doesn't block us. ${DEFAULT_SETTINGS.pauseMs} is the recommended value; raise it after a block.`"
                            orientation="horizontal"
                            class="pt-4"
                        >
                            <UInputNumber
                                v-model="draft.pauseMs"
                                :min="0"
                                :max="MAX_PAUSE_MS"
                                :step="100"
                                class="w-48"
                            />
                        </UFormField>
                    </UPageCard>
                </section>
 
                <section class="flex flex-col gap-3">
                    <h2 class="font-semibold">Bulk apply</h2>
                    <UPageCard
                        variant="subtle"
                        :ui="{ container: 'gap-y-0 divide-y divide-default' }"
                    >
                        <UFormField
                            label="Require review"
                            description="Which pending articles must be reviewed first."
                            orientation="horizontal"
                            class="pb-4"
                        >
                            <USelect v-model="draft.reviewGate" :items="GATES" class="w-48" />
                        </UFormField>
 
                        <UFormField
                            v-if="Idraft.reviewGate !== 'none'"
                            label="Reviewed means"
                            description="Opened at least once, or explicitly confirmed."
                            orientation="horizontal"
                            class="py-4"
                        >
                            <USelect
                                v-model="draft.bulkApplyMinLevel"
                                :items="LEVELS"
                                class="w-48"
                            />
                        </UFormField>
 
                        <UFormField
                            label="Require a dry run"
                            description="A dry run must clear the current proposals first."
                            orientation="horizontal"
                            class="pt-4"
                        >
                            <USwitch v-model="draft.requireDryRun" />
                        </UFormField>
                    </UPageCard>
                </section>
            </div>
        </template>
    </UDashboardPanel>
</template>
 
<script setup lang="ts">
import type { SelectItem } from "@nuxt/ui";
import type { Settings } from "#shared/types/Settings.ts";
import { DEFAULT_SETTINGS, MAX_PAUSE_MS } from "#shared/constants.ts";
 
const toast = useToast();
const { data: settings, error } = await useFetch<Settings>("/api/settings");
const errorMessage = computed(() => (error.value ? messageOf(error.value) : ""));
 
const draft = ref<Settings | null>(settings.value ? { ...settings.value } : null);
const dirty = computed(() => JSON.stringify(draft.value) !== JSON.stringify(settings.value));
const saving = ref(false);
 
const GATES = [
    { value: "all", label: "All articles" },
    { value: "requested", label: "Flagged for review" },
    { value: "none", label: "None" },
] satisfies SelectItem[];
 
const LEVELS = [
    { value: "seen", label: "Seen" },
    { value: "confirmed", label: "Confirmed" },
] satisfies SelectItem[];
 
const save = async () => {
    saving.value = true;
 
    try {
        settings.value = await $fetch<Settings>("/api/settings", {
            method: "PUT",
            body: draft.value,
        });
        draft.value = { ...settings.value };
        toast.add({ title: "Saved", color: "success", icon: "i-lucide-check" });
    } catch (saveError) {
        toast.add({ title: "Not saved", description: messageOf(saveError), color: "error" });
    } finally {
        saving.value = false;
    }
};
</script>