File tree Expand file tree Collapse file tree 2 files changed +75
-6
lines changed Expand file tree Collapse file tree 2 files changed +75
-6
lines changed Original file line number Diff line number Diff line change 57
57
ADD TO CART
58
58
</CommonButton >
59
59
</div >
60
- <p v-if =" successMessage" class =" mt-4 text-green-600" >{{ successMessage }}</p >
61
60
</div >
62
61
</div >
63
62
</div >
64
63
</section >
64
+ <Toast ref =" toast" />
65
65
</template >
66
66
</template >
67
67
@@ -82,6 +82,7 @@ import GET_SINGLE_PRODUCT_QUERY from "@/apollo/queries/GET_SINGLE_PRODUCT_QUERY.
82
82
83
83
import ProductImage from " @/components/Products/ProductImage.vue" ;
84
84
import ProductPrice from " @/components/Products/ProductPrice.vue" ;
85
+ import Toast from " @/components/common/Toast.vue" ;
85
86
86
87
import { stripHTML , filteredVariantName } from " @/utils/functions" ;
87
88
@@ -92,7 +93,7 @@ const cart = useCart();
92
93
const isLoading = computed (() => cart .loading );
93
94
94
95
const selectedVariation = ref (); // TODO Pass this value to addProductToCart()
95
- const successMessage = ref (' ' );
96
+ const toast = ref (null );
96
97
97
98
const props = defineProps ({
98
99
id: { type: String , required: true },
@@ -122,10 +123,7 @@ watch(
122
123
const addProductToCart = async (product ) => {
123
124
try {
124
125
await cart .addToCart (product);
125
- successMessage .value = ' Product added to cart successfully!' ;
126
- setTimeout (() => {
127
- successMessage .value = ' ' ;
128
- }, 3000 );
126
+ toast .value .show ();
129
127
} catch (error) {
130
128
console .error (' Error adding product to cart:' , error);
131
129
// You might want to show an error message to the user here
Original file line number Diff line number Diff line change
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 >
You can’t perform that action at this time.
0 commit comments