frontend/src/views/DetailsView/EditLink.vue

159 lines
3.2 KiB
Vue
Raw Normal View History

2025-10-01 11:03:10 +03:00
<script setup>
2025-10-07 20:39:49 +03:00
import { ref, nextTick } from 'vue'
2025-10-01 11:03:10 +03:00
import { useMerchApi } from '@/api/merch.js'
import { BASE_MANDARAKE_LINK } from '@/main.js'
2025-10-01 11:03:10 +03:00
const { updateMerch } = useMerchApi()
const props = defineProps({
modelValue: {
type: String,
2025-10-07 20:39:49 +03:00
required: true,
2025-10-01 11:03:10 +03:00
},
placeholder: {
type: String,
2025-10-07 20:39:49 +03:00
default: 'Enter link here...',
2025-10-01 11:03:10 +03:00
},
merchUuid: {
type: String,
2025-10-07 20:39:49 +03:00
required: true,
2025-10-01 11:03:10 +03:00
},
name: {
type: String,
2025-10-07 20:39:49 +03:00
required: true,
2025-10-01 11:03:10 +03:00
},
origin: {
type: String,
required: true,
},
})
2025-10-07 20:39:49 +03:00
const emit = defineEmits(['update:modelValue', 'cancel-edit'])
2025-10-01 11:03:10 +03:00
const tempValue = ref('')
const inputRef = ref(null)
const loading = ref(false)
2025-10-07 20:39:49 +03:00
tempValue.value = props.modelValue
2025-10-01 11:03:10 +03:00
2025-10-07 20:39:49 +03:00
nextTick(() => {
inputRef.value?.focus?.()
})
2025-10-01 11:03:10 +03:00
const save = async () => {
const newValue = tempValue.value.trim()
2025-10-07 20:39:49 +03:00
if (newValue === '') {
emit('cancel-edit')
return
}
if (newValue === props.modelValue.trim()) {
emit('cancel-edit')
return
2025-10-01 11:03:10 +03:00
}
2025-10-07 20:39:49 +03:00
2025-10-01 11:03:10 +03:00
loading.value = true
try {
await updateMerch({
merch_uuid: props.merchUuid,
name: props.name,
origin: props.origin,
2025-10-07 20:39:49 +03:00
link: newValue,
2025-10-01 11:03:10 +03:00
})
emit('update:modelValue', newValue)
} catch (error) {
console.error('Update link error:', error)
} finally {
loading.value = false
}
}
2025-10-07 20:39:49 +03:00
const showModal = ref(false)
const clearLink = async () => {
showModal.value = true
2025-10-01 11:03:10 +03:00
}
2025-10-07 20:39:49 +03:00
const confirmClearLink = async () => {
loading.value = true
try {
await updateMerch({
merch_uuid: props.merchUuid,
name: props.name,
origin: props.origin,
link: '',
})
emit('update:modelValue', '')
} catch (error) {
console.error('Update link error:', error)
} finally {
loading.value = false
2025-10-01 11:03:10 +03:00
}
2025-10-07 20:39:49 +03:00
showModal.value = false
cancel()
}
const cancel = () => {
emit('cancel-edit')
}
const insertAutoCompletedLink = () => {
tempValue.value = BASE_MANDARAKE_LINK+props.name
}
2025-10-01 11:03:10 +03:00
</script>
<template>
<div v-if="props.origin === 'mandarake'" class="center-button-container mb-10">
<n-button type="warning" class="center-button" @click="insertAutoCompletedLink"
>Insert auto-completed link</n-button
>
</div>
2025-10-07 20:39:49 +03:00
<div class="editing-area">
2025-10-01 11:03:10 +03:00
<n-input
v-model:value="tempValue"
2025-10-07 20:39:49 +03:00
type="textarea"
size="large"
2025-10-01 11:03:10 +03:00
ref="inputRef"
@keyup.enter="save"
@keyup.esc="cancel"
2025-10-07 20:39:49 +03:00
:placeholder="placeholder"
:loading="loading"
2025-10-01 11:03:10 +03:00
/>
2025-10-07 20:39:49 +03:00
<n-button size="small" type="primary" :loading="loading" @click="save">Save</n-button>
<n-button size="small" @click="cancel">Cancel</n-button>
</div>
<div class="center-button-container">
<n-button type="error" class="center-button" @click="clearLink">Clear link</n-button>
2025-10-01 11:03:10 +03:00
</div>
2025-10-07 20:39:49 +03:00
<n-modal v-model:show="showModal" preset="dialog" title="Confirmation">
<template #default>
<p>Confirm clear link</p>
</template>
<template #action>
<n-button @click="confirmClearLink" type="error">Clear</n-button>
<n-button @click="showModal = false">Cancel</n-button>
</template>
</n-modal>
2025-10-01 11:03:10 +03:00
</template>
<style scoped>
.editing-area {
2025-10-07 20:39:49 +03:00
display: flex;
2025-10-01 11:03:10 +03:00
gap: 6px;
align-items: center;
width: 100%;
2025-10-07 20:39:49 +03:00
flex-wrap: wrap;
}
.editing-area :deep(.n-input) {
flex: 1;
min-width: 200px;
2025-10-01 11:03:10 +03:00
}
</style>