358da6a8ad
| | Layout-Admin | |------------------|-----------------| | | | ## Description de la PR ## Modification du .env ## Check list - [ ] Pas de régression - [ ] TU/TI/TF rédigée - [ ] TU/TI/TF OK - [ ] CHANGELOG modifié Reviewed-on: https://gitea.malio.fr/MALIO-DEV/Ferme/pulls/28 Reviewed-by: Autin <tristan@yuno.malio.fr> Co-authored-by: Matteo <matteo@yuno.malio.fr> Co-committed-by: Matteo <matteo@yuno.malio.fr>
50 lines
1.8 KiB
Vue
50 lines
1.8 KiB
Vue
<template>
|
|
<Address type="customer" :address="address" @validate="validate"/>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import type { AddressData, AddressPayload } from "~/services/address"
|
|
import { createAddress, getAddress, updateAddress } from "~/services/address"
|
|
import { getCustomer, updateCustomer } from "~/services/customer"
|
|
import type { CustomerData } from "~/services/dto/customer-data"
|
|
|
|
definePageMeta({ layout: "default" })
|
|
|
|
const route = useRoute()
|
|
const router = useRouter()
|
|
const customerId = computed(() => Number(route.query.customerId))
|
|
const customer = ref<CustomerData | null>(null)
|
|
const addressId = computed(() => (route.query.addressId !== undefined ? Number(route.query.addressId) : null))
|
|
const address = ref<AddressData | null>(null)
|
|
|
|
const validate = async (payload: AddressPayload) => {
|
|
try {
|
|
if (addressId.value !== null) {
|
|
await updateAddress(addressId.value, payload)
|
|
} else {
|
|
await addAddress(payload)
|
|
}
|
|
} finally {
|
|
await router.push("/admin/customer/" + customerId.value)
|
|
}
|
|
}
|
|
|
|
const addAddress = async (payload: AddressPayload) => {
|
|
const response: AddressData = await createAddress(payload)
|
|
const addressIRI = `/api/addresses/${response.id}`
|
|
const existingIris = (customer.value?.addresses ?? [])
|
|
.map((item: any) => (typeof item === "string" ? item : `/api/addresses/${item.id}`))
|
|
.filter((iri: string | null) => Boolean(iri)) as string[]
|
|
const next = [...new Set([...existingIris, addressIRI])]
|
|
|
|
return await updateCustomer(customerId.value, { addresses: next })
|
|
}
|
|
|
|
onMounted(async () => {
|
|
customer.value = await getCustomer(customerId.value)
|
|
if (addressId.value !== null) {
|
|
address.value = await getAddress(addressId.value)
|
|
}
|
|
})
|
|
</script>
|