const showModal = ref(false);
const selectedBook = ref(null);
const isBookDeleted = ref(false);
const showBookDetails = ref(true);
<button
class="bg-blue-500 text-white p-space-xs pl-space-m pr-space-m rounded-medium hover:bg-blue-600"
@click="deleteBook"
>
Удалить
</button>
<div v-if="isBookDeleted">
<p>Книга успешно удалена!</p>
</div>
<div v-if="showBookDetails">
становится false и скрывается. const deleteBook = async () => {
const bookId = selectedBook.value.id;
const token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIwY2Y5Nzk0Yi1jYjczLTcyQ";
const { data, error, refresh } = await useFetch<{ results: Book[] }>(
`https://api.storage/${bookId}`,
{
headers: {
Authorization: `Bearer ${token}`,
},
method: "delete",
server: false,
}
);
if (error.value){
console.error(error.value)
} else {
isBookDeleted.value = true;
showBookDetails.value = false;
}
refresh();
};
<template>
<!-- Overlay -->
<div
class="fixed z-[99] inset-0 bg-black bg-opacity-70 flex items-center justify-center"
>
<!-- Modal -->
<OnClickOutside @trigger="$emit('close')" class="relative bg-white w-[336px] py-space-xl lg:w-[528px] rounded-medium">
<slot />
<!-- Close button -->
<button
class="absolute top-0 right-0 p-4"
@click="$emit('close')"
>
<img :src="close" alt="icon" />
</button>
</OnClickOutside>
</div>
</template>
<script setup lang="ts">
import { OnClickOutside } from "@vueuse/components";
import close from "~/assets/images/close.svg";
</script>
const showModal = ref(false);
const selectedBook = ref(null);
const isBookDeleted = ref(false);
const showBookDetails = ref(true);
const openModal = (book: any) => {
selectedBook.value = book;
showModal.value = true;
};
const closeModal = () => {
showModal.value = false;
selectedBook.value = null;
};
const { apiErrorHandler } = useErrorService();
const deleteBook = async () => {
const bookId = selectedBook.value.id;
const { error } = await useFetch(
`https://api.storage/${bookId}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${token}`,
},
}
);
if (error.value) {
apiErrorHandler(error.value);
} else {
isBookDeleted.value = true;
showBookDetails.value = false;
}
};
const showModal = ref(false);
const selectedBook = ref(null);
const isBookDeleted = ref(false);
const showBookDetails = ref(true);
function toggleForm(formName: Component) {
if (currentForm.value === formName) {
closeForm();
} else {
currentForm.value = formName;
}
}
function closeForm() {
console.log('closeForm: Закрытие формы');
currentForm.value = null;
}
function toggleForm(formComponent: Component) {
if (currentForm.value === formComponent) {
closeForm();
} else {
currentForm.value = formComponent;
}
}
<component :is="currentForm" />
function toggleForm(formName: any) {
console.log(`toggleForm: Изменение текущей формы на ${formName}`);
if (currentForm.value === formName) {
closeForm();
} else {
currentForm.value = formName;
}
}
function closeForm() {
console.log('closeForm: Закрытие формы');
currentForm.value = null;
}