Skip to content

Commit bafa53d

Browse files
committed
:start: feat(images): store and retrieve
1 parent 11e5822 commit bafa53d

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

components/Car/Card.vue

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import heartFilled from "@/assets/heartFilled.png";
33
import heartOutline from "@/assets/heartOutline.png";
44
5+
const config = useRuntimeConfig();
56
const props = defineProps({ car: Object, favored: Boolean });
67
78
const emit = defineEmits(["favor"]);
@@ -18,7 +19,11 @@ const emit = defineEmits(["favor"]);
1819
@click="emit('favor', car.id)"
1920
/>
2021
<div class="flex h-full" @click="navigateTo(`/car/${car.name}-${car.id}`)">
21-
<NuxtImg :src="car.image" alt="" class="w-[300px] h-full" />
22+
<NuxtImg
23+
:src="`${config.public.supabase.url}/storage/v1/object/public/images/${car.image}`"
24+
alt=""
25+
class="w-[300px] h-full"
26+
/>
2227
<div class="p-4 flex flex-col">
2328
<div>
2429
<h1 class="text-2xl text-blue-700">{{ car.name }}</h1>

components/Car/Detail/Hero.vue

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
<script setup>
22
const props = defineProps({ car: Object });
3+
4+
const config = useRuntimeConfig();
35
</script>
46

57
<template>
68
<div class="mt-10">
7-
<NuxtImg :src="car.image" class="w-full" alt="" />
9+
<NuxtImg
10+
:src="`${config.public.supabase.url}/storage/v1/object/public/images/${car.image}`"
11+
class="w-full"
12+
alt=""
13+
/>
814

915
<h1 class="mt-10 text-4xl">{{ car.name }}</h1>
1016

components/Car/ListingCard.vue

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ const props = defineProps({
33
listing: Object,
44
});
55
6+
const config = useRuntimeConfig();
7+
68
const emits = defineEmits(["deleteClick"]);
79
</script>
810

911
<template>
1012
<div class="shadow rounded overflow-hidden flex justify-between mb-4">
1113
<div class="flex">
12-
<img :src="listing.url" class="w-80 mr-3 h-44" alt="" />
14+
<img
15+
:src="`${config.public.supabase.url}/storage/v1/object/public/images/${listing.image}`"
16+
class="w-80 mr-3 h-44"
17+
alt=""
18+
/>
1319

1420
<div class="p-3">
1521
<h1 class="text-2xl">{{ listing.name }}</h1>

pages/profile/listings/create.vue

+13-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ definePageMeta({
55
66
const { makes } = useCars();
77
const user = useSupabaseUser();
8+
const supabase = useSupabaseClient();
89
910
const info = useState("adInfo", () => {
1011
return {
@@ -17,7 +18,7 @@ const info = useState("adInfo", () => {
1718
seats: "",
1819
features: "",
1920
description: "",
20-
image: "nullun",
21+
image: null,
2122
};
2223
});
2324
@@ -81,6 +82,15 @@ const isButtonDisabled = computed(() => {
8182
});
8283
8384
const handleSubmit = async () => {
85+
const filename = Math.floor(Math.random() * 10000000000000);
86+
const { data, error } = await supabase.storage
87+
.from("images")
88+
.upload(`public/${filename}`, info.value.image);
89+
90+
if (error) {
91+
return (errorMessage.value = "Cannot upload image");
92+
}
93+
8494
const body = {
8595
...info.value,
8696
features: info.value.features.split(", "),
@@ -91,6 +101,7 @@ const handleSubmit = async () => {
91101
year: parseInt(info.value.year),
92102
name: `${info.value.make} ${info.value.model}`,
93103
listerId: user.value.id,
104+
image: data.path,
94105
};
95106
96107
delete body.seats;
@@ -103,6 +114,7 @@ const handleSubmit = async () => {
103114
navigateTo("/profile/listings");
104115
} catch (error) {
105116
errorMessage.value = error.statusMessage;
117+
await supabase.storage.from("images").remove(data.path);
106118
}
107119
};
108120
</script>

0 commit comments

Comments
 (0)