All checks were successful
Build and Deploy Nuxt / build (push) Successful in 1m6s
154 lines
7.9 KiB
Vue
154 lines
7.9 KiB
Vue
<script setup>
|
|
import { onBeforeUnmount, onMounted, ref } from 'vue';
|
|
import { SetupHandle, SetSize, ResetPosition, ClearWindow } from '@/services/Windows';
|
|
import WindowHandle from './partials/WindowHandle.vue';
|
|
import Server from '~/services/Server';
|
|
import { emitter } from '~/services/Emitter';
|
|
|
|
const props = defineProps(['data']);
|
|
const data = props.data;
|
|
const id = data.id;
|
|
const handle = ref(null);
|
|
const wrapper = ref(null);
|
|
const saving = ref(false);
|
|
// The window edits items when the sidebar passes one in; actors have their sheet.
|
|
const editing = data.entry || null;
|
|
const isActor = data.entityType === 'actor';
|
|
const endpoint = isActor ? '/actor/create' : (editing ? '/item/update' : '/item/create');
|
|
|
|
function initialForm() {
|
|
if (!editing) {
|
|
return {
|
|
name: '', type: 'character', species: '', className: '', level: '', background: '', challengeRating: '',
|
|
category: 'adventuring-gear', rarity: 'common', quantity: 1, weight: '', properties: '', description: ''
|
|
};
|
|
}
|
|
return {
|
|
name: editing.name ?? '', type: 'character', species: '', className: '', level: '', background: '', challengeRating: '',
|
|
category: editing.category ?? 'adventuring-gear', rarity: editing.rarity ?? 'common',
|
|
quantity: editing.quantity ?? 1, weight: editing.weight ?? '', properties: editing.properties ?? '',
|
|
description: editing.description ?? ''
|
|
};
|
|
}
|
|
|
|
const form = ref(initialForm());
|
|
|
|
onMounted(() => {
|
|
SetupHandle(id, handle);
|
|
SetSize(id, isActor ? { width: 420, height: 220 } : { width: 500, height: 560 });
|
|
ResetPosition(id, 'center');
|
|
});
|
|
|
|
onBeforeUnmount(() => ClearWindow({ type: 'create_campaign_entry' }));
|
|
|
|
async function saveEntry() {
|
|
if (!form.value.name.trim() || saving.value) return;
|
|
saving.value = true;
|
|
try {
|
|
const payload = isActor
|
|
? { name: form.value.name.trim(), campaign: data.campaign }
|
|
: { name: form.value.name.trim(), ...(editing ? { id: editing._id } : { campaign: data.campaign }) };
|
|
if (!isActor) {
|
|
for (const field of ['category', 'rarity', 'quantity', 'weight', 'properties', 'description']) {
|
|
payload[field] = form.value[field];
|
|
}
|
|
}
|
|
const response = await Server().post(endpoint, payload);
|
|
if (response.data.status === 'ok') {
|
|
emitter.emit(editing ? 'campaign-entry-updated' : 'campaign-entry-created', { entityType: data.entityType, entry: response.data[isActor ? 'actor' : 'item'] });
|
|
ClearWindow({ id });
|
|
}
|
|
} catch (error) {
|
|
// Keep the form open so the user can retry.
|
|
} finally {
|
|
saving.value = false;
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div class="window-wrapper" :id="'window-wrapper-' + id" ref="wrapper">
|
|
<WindowHandle :window="id" ref="handle" />
|
|
<div class="body">
|
|
<h3>{{ $t(editing ? 'campaigns.item.edit' : (isActor ? 'campaigns.actor.new' : 'campaigns.item.new')) }}</h3>
|
|
<p v-if="!isActor && editing?.foundryItemId" class="sync-note">{{ $t('campaigns.item.foundry-sync-note') }}</p>
|
|
<form @submit.prevent="saveEntry">
|
|
<label class="form-field">
|
|
<span>{{ $t('campaigns.fields.name') }}</span>
|
|
<input v-model="form.name" required autofocus autocomplete="off">
|
|
</label>
|
|
|
|
<template v-if="!isActor">
|
|
<div class="form-grid">
|
|
<label class="form-field">
|
|
<span>{{ $t('campaigns.item.category') }}</span>
|
|
<select v-model="form.category">
|
|
<option value="weapon">{{ $t('campaigns.item.categories.weapon') }}</option>
|
|
<option value="armor">{{ $t('campaigns.item.categories.armor') }}</option>
|
|
<option value="shield">{{ $t('campaigns.item.categories.shield') }}</option>
|
|
<option value="adventuring-gear">{{ $t('campaigns.item.categories.adventuring-gear') }}</option>
|
|
<option value="magic-item">{{ $t('campaigns.item.categories.magic-item') }}</option>
|
|
<option value="consumable">{{ $t('campaigns.item.categories.consumable') }}</option>
|
|
<option value="tool">{{ $t('campaigns.item.categories.tool') }}</option>
|
|
<option value="treasure">{{ $t('campaigns.item.categories.treasure') }}</option>
|
|
<option value="other">{{ $t('campaigns.item.categories.other') }}</option>
|
|
</select>
|
|
</label>
|
|
<label class="form-field">
|
|
<span>{{ $t('campaigns.item.rarity') }}</span>
|
|
<select v-model="form.rarity">
|
|
<option value="common">{{ $t('campaigns.item.rarities.common') }}</option>
|
|
<option value="uncommon">{{ $t('campaigns.item.rarities.uncommon') }}</option>
|
|
<option value="rare">{{ $t('campaigns.item.rarities.rare') }}</option>
|
|
<option value="very-rare">{{ $t('campaigns.item.rarities.very-rare') }}</option>
|
|
<option value="legendary">{{ $t('campaigns.item.rarities.legendary') }}</option>
|
|
<option value="artifact">{{ $t('campaigns.item.rarities.artifact') }}</option>
|
|
</select>
|
|
</label>
|
|
<label class="form-field">
|
|
<span>{{ $t('campaigns.item.quantity') }}</span>
|
|
<input v-model.number="form.quantity" type="number" min="1" required>
|
|
</label>
|
|
<label class="form-field">
|
|
<span>{{ $t('campaigns.item.weight') }}</span>
|
|
<input v-model="form.weight" type="number" min="0" step="0.1">
|
|
</label>
|
|
</div>
|
|
<label class="form-field">
|
|
<span>{{ $t('campaigns.item.properties') }}</span>
|
|
<input v-model="form.properties" :placeholder="$t('campaigns.item.properties-placeholder')">
|
|
</label>
|
|
</template>
|
|
|
|
<label v-if="!isActor" class="form-field">
|
|
<span>{{ $t('campaigns.fields.description') }}</span>
|
|
<textarea v-model="form.description" rows="3" />
|
|
</label>
|
|
<div class="form-actions">
|
|
<button class="btn-primary sound-click" type="submit" :disabled="saving">
|
|
{{ saving ? $t('campaigns.actions.saving') : $t(editing ? 'campaigns.actions.save' : 'campaigns.actions.submit') }}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.window-wrapper { display: flex; align-items: center; flex-direction: column; }
|
|
.body { width: 100%; padding: 10px 18px 16px; overflow-y: auto; }
|
|
h3 { margin: 0 0 12px; font-family: MrEavesRemake; }
|
|
.sync-note { margin: -8px 0 12px; font-size: 12px; opacity: 0.72; }
|
|
form { display: flex; flex-direction: column; gap: 10px; }
|
|
.form-field { min-width: 0; display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
|
|
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
|
|
input, select, textarea {
|
|
box-sizing: border-box; width: 100%; padding: 7px 8px; border: 1px solid var(--color-border);
|
|
border-radius: 6px; background: var(--color-background-soft); color: var(--color-text); font: inherit;
|
|
}
|
|
textarea { resize: vertical; }
|
|
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--color-link); }
|
|
.form-actions { display: flex; justify-content: center; margin-top: 2px; }
|
|
.form-actions button { width: 100%; max-width: 220px; }
|
|
</style>
|