-
+
+
+ Localisation
+
-
+
Envoyer localisation
+
+ Posts
+
-
+
Créer
@@ -57,11 +64,13 @@ onMounted(() => {
{{ post.country }}
@@ -72,6 +81,4 @@ onMounted(() => {
-
\ No newline at end of file
+
diff --git a/summer2024-frontend/src/views/CreatePostView.vue b/summer2024-frontend/src/views/CreatePostView.vue
index d41a03c..5fcd86b 100644
--- a/summer2024-frontend/src/views/CreatePostView.vue
+++ b/summer2024-frontend/src/views/CreatePostView.vue
@@ -8,7 +8,14 @@ import { Label } from '@/components/ui/label/index.js'
import { toTypedSchema } from '@vee-validate/zod'
import { z } from 'zod'
import { onMounted, onUnmounted, ref } from 'vue'
-import { AlertCircle, ArrowLeft, ArrowRight, CircleCheckBig, CirclePlus, RefreshCcw } from 'lucide-vue-next'
+import {
+ AlertCircle,
+ ArrowLeft,
+ ArrowRight,
+ CircleCheckBig,
+ CirclePlus,
+ RefreshCcw
+} from 'lucide-vue-next'
import {
Carousel,
CarouselContent,
@@ -18,18 +25,20 @@ import {
} from '@/components/ui/carousel/index.js'
import { Card, CardContent } from '@/components/ui/card/index.js'
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert/index.js'
-import { useAuthStore } from '@/stores/auth.js'
-import { API_BASE_URL, S3_BUCKET, S3_ENDPOINT } from '@/config.js'
+import { getCityAndCountry } from '@/lib/utils.js'
+import { useAdminPostsStore } from '@/stores/adminPosts.js'
-const authStore = useAuthStore()
+const adminPostStore = useAdminPostsStore()
-const formSchema = toTypedSchema(z.object({
- description: z.string().min(1),
- latitude: z.number(),
- longitude: z.number(),
- city: z.string().min(1),
- country: z.string().min(1)
-}))
+const formSchema = toTypedSchema(
+ z.object({
+ description: z.string().min(1),
+ latitude: z.number(),
+ longitude: z.number(),
+ city: z.string().min(1),
+ country: z.string().min(1)
+ })
+)
const form = useForm({
validationSchema: formSchema
})
@@ -46,96 +55,18 @@ const formStatus = ref({
errorMsg: ''
})
-function formError(message) {
- formStatus.value.sending = false
- formStatus.value.error = true
- formStatus.value.errorMsg = message
- formContainer.value.classList.remove('invisible')
-}
-
const onSubmit = form.handleSubmit(async (values) => {
- if (!authStore.isAuth)
- return
- console.log('Envoi du post...')
- if (selectedFiles.value.length === 0)
- return
formContainer.value.classList.add('hidden')
formStatus.value.sending = true
+ try {
+ await adminPostStore.createPost(values, selectedFiles.value)
+ } catch (e) {
+ formStatus.value.sending = false
+ formStatus.value.error = true
+ formStatus.value.errorMsg = e.message
+ formContainer.value.classList.remove('hidden')
- const assets = []
- for (const file of selectedFiles.value) {
- console.log('Contact API asset')
- const response = await fetch(API_BASE_URL + '/admin/assets', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'X-admin-token': authStore.adminToken
- },
- body: JSON.stringify({ filename: file.file.name })
- }).catch((e) => {
- console.log('Contact API asset failed: ' + e)
- formError('Erreur à la préparation de l\'envoi d\'un média : ' + e)
- })
- if (!response)
- return
-
- if (!response.ok) {
- console.log('Contact API asset failed: ' + response.statusText + '\n\n' + response.body)
- formError('Erreur à la préparation de l\'envoi d\'un média : ' + response.statusText)
- return
- }
- const responseBody = await response.json()
- const mediaUploadFormData = new FormData()
- for (const [key, value] of Object.entries(responseBody.formData)) {
- mediaUploadFormData.append(key, value)
- }
- mediaUploadFormData.append('key', '${filename}')
- mediaUploadFormData.append('Content-Type', file.file.type)
- mediaUploadFormData.append('file', file.file, responseBody.filename)
-
- console.log('Envoi image sur s3')
- const s3Response = await fetch(`${S3_ENDPOINT}/${S3_BUCKET}/`, {
- method: 'POST',
- body: mediaUploadFormData
- }).catch((e) => {
- console.log('Erreur envoi S3: ' + e)
- formError('Une erreur est survenue pendant l\'envoi d\'un média : ' + e)
- })
- if (!s3Response)
- return
- if (!s3Response.ok) {
- console.log('Envoi media S3 failed: ' + s3Response.statusText + '\n\n' + s3Response.body)
- formError('Une erreur est survenue pendant l\'envoi d\'un média : ' + s3Response.statusText)
- return
- }
-
- assets.push(responseBody.id)
- }
-
- const response = await fetch(API_BASE_URL + '/admin/posts', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'X-admin-token': authStore.adminToken
- },
- body: JSON.stringify({
- description: values.description,
- latitude: values.latitude,
- longitude: values.longitude,
- city: values.city,
- country: values.country,
- assets: assets
- })
- }).catch((e) => {
- console.log('Erreur envoi post : ' + e)
- formError('Une erreur est survenue pendant l\'envoi du post : ' + e)
- })
- if (!response)
- return
- if (!response.ok) {
- console.log('POST post API failed: ' + response.statusText + '\n\n' + response.body)
- formError('Une erreur est survenue lors de l\'envoi du poste : ' + response.statusText)
- return
+ throw e
}
formStatus.value.sending = false
@@ -172,31 +103,17 @@ function mediaReorder(index, diff) {
selectedFiles.value[index + diff] = tmp
}
-function getCityAndCountry() {
+function updateCityAndCountry() {
const lat = latitudeInput.value.value
const lon = longitudeInput.value.value
- if (!lat || !lon)
- return
- fetch(`https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=jsonv2`)
- .then((resp) => resp.json()).then((resp) => {
- const cityFieldOrder = ['city', 'town', 'borough', 'village', 'suburb', 'municipality', 'county', 'state']
- let found = false
- for (const field of cityFieldOrder) {
- if (Object.hasOwn(resp.address, field)) {
- found = true
- form.setFieldValue('city', resp.address[field])
- break
- }
- }
- if (!found) {
- form.setFieldValue('city', 'endroit perdu')
- }
- form.setFieldValue('country', resp.address.country)
+ if (!lat || !lon) return
+
+ getCityAndCountry(lat, lon).then((cityAndCountry) => {
+ form.setFieldValue('city', cityAndCountry[0])
+ form.setFieldValue('country', cityAndCountry[1])
})
-
}
-
@@ -266,28 +183,41 @@ function getCityAndCountry() {
-
+