Skip to content

Commit 1e67543

Browse files
committed
Toast
1 parent b56198b commit 1e67543

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

components/Products/ProductsSingleProduct.vue

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@
5757
ADD TO CART
5858
</CommonButton>
5959
</div>
60-
<p v-if="successMessage" class="mt-4 text-green-600">{{ successMessage }}</p>
6160
</div>
6261
</div>
6362
</div>
6463
</section>
64+
<Toast ref="toast" />
6565
</template>
6666
</template>
6767

@@ -82,6 +82,7 @@ import GET_SINGLE_PRODUCT_QUERY from "@/apollo/queries/GET_SINGLE_PRODUCT_QUERY.
8282
8383
import ProductImage from "@/components/Products/ProductImage.vue";
8484
import ProductPrice from "@/components/Products/ProductPrice.vue";
85+
import Toast from "@/components/common/Toast.vue";
8586
8687
import { stripHTML, filteredVariantName } from "@/utils/functions";
8788
@@ -92,7 +93,7 @@ const cart = useCart();
9293
const isLoading = computed(() => cart.loading);
9394
9495
const selectedVariation = ref(); // TODO Pass this value to addProductToCart()
95-
const successMessage = ref('');
96+
const toast = ref(null);
9697
9798
const props = defineProps({
9899
id: { type: String, required: true },
@@ -122,10 +123,7 @@ watch(
122123
const addProductToCart = async (product) => {
123124
try {
124125
await cart.addToCart(product);
125-
successMessage.value = 'Product added to cart successfully!';
126-
setTimeout(() => {
127-
successMessage.value = '';
128-
}, 3000);
126+
toast.value.show();
129127
} catch (error) {
130128
console.error('Error adding product to cart:', error);
131129
// You might want to show an error message to the user here

components/common/Toast.vue

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<template>
2+
<Teleport to="body">
3+
<transition name="toast">
4+
<div v-if="isVisible" class="toast-container">
5+
<div class="toast-content">
6+
{{ message }}
7+
</div>
8+
</div>
9+
</transition>
10+
</Teleport>
11+
</template>
12+
13+
<script setup>
14+
import { ref, watch } from 'vue';
15+
16+
const props = defineProps({
17+
message: {
18+
type: String,
19+
default: 'Product added to cart'
20+
},
21+
duration: {
22+
type: Number,
23+
default: 3000
24+
}
25+
});
26+
27+
const isVisible = ref(false);
28+
29+
const show = () => {
30+
isVisible.value = true;
31+
setTimeout(() => {
32+
isVisible.value = false;
33+
}, props.duration);
34+
};
35+
36+
defineExpose({ show });
37+
</script>
38+
39+
<style scoped>
40+
.toast-container {
41+
position: fixed;
42+
top: 20px;
43+
right: 20px;
44+
z-index: 9999;
45+
}
46+
47+
.toast-content {
48+
background-color: #4CAF50;
49+
color: white;
50+
padding: 16px;
51+
border-radius: 4px;
52+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
53+
}
54+
55+
.toast-enter-active,
56+
.toast-leave-active {
57+
transition: all 0.5s ease;
58+
}
59+
60+
.toast-enter-from,
61+
.toast-leave-to {
62+
opacity: 0;
63+
transform: translateX(30px);
64+
}
65+
66+
.toast-enter-to,
67+
.toast-leave-from {
68+
opacity: 1;
69+
transform: translateX(0);
70+
}
71+
</style>

0 commit comments

Comments
 (0)