|
@@ -1,27 +1,13 @@
|
|
|
-import { AnyJson } from '~/types/interfaces'
|
|
|
|
|
|
|
+import {AnyJson} from '~/types/interfaces'
|
|
|
import {QUERY_TYPE} from "~/types/enums";
|
|
import {QUERY_TYPE} from "~/types/enums";
|
|
|
-import { useContext } from '@nuxtjs/composition-api'
|
|
|
|
|
import DataProvider from "~/services/data/dataProvider";
|
|
import DataProvider from "~/services/data/dataProvider";
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @category composables/data
|
|
* @category composables/data
|
|
|
- * @class UseAddressPostal
|
|
|
|
|
- * Use Classe pour gérer les deux champs postal code et adresseCity
|
|
|
|
|
|
|
+ * @param $dataProvider
|
|
|
|
|
+ * Composable class qui regroupe les utils des adresses postales
|
|
|
*/
|
|
*/
|
|
|
-export class UseAddressPostal {
|
|
|
|
|
- private $dataProvider!: DataProvider
|
|
|
|
|
-
|
|
|
|
|
- constructor() {
|
|
|
|
|
- const {$dataProvider} = useContext()
|
|
|
|
|
- this.$dataProvider = $dataProvider
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public invoke(): AnyJson{
|
|
|
|
|
- return {
|
|
|
|
|
- searchFunction: (research: string, field: string) => this.searchFunction(research, field),
|
|
|
|
|
- updateCpAddress: (value:AnyJson, updateRepository: Function) => this.updateCpAddress(value, updateRepository),
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+export function useAddressPostalUtils($dataProvider: DataProvider) {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Fonction de recherche qui utilise l'API gouvernematal pour autocompléter les CP et villes.
|
|
* Fonction de recherche qui utilise l'API gouvernematal pour autocompléter les CP et villes.
|
|
@@ -29,23 +15,23 @@ export class UseAddressPostal {
|
|
|
* @param field
|
|
* @param field
|
|
|
* @private
|
|
* @private
|
|
|
*/
|
|
*/
|
|
|
- private async searchFunction (research: string, field: string): Promise<Array<AnyJson>>{
|
|
|
|
|
- if(research){
|
|
|
|
|
- const response = await this.$dataProvider.invoke({
|
|
|
|
|
|
|
+ async function searchFunction(research: string, field: string): Promise<Array<AnyJson>> {
|
|
|
|
|
+ if (research) {
|
|
|
|
|
+ const response = await $dataProvider.invoke({
|
|
|
type: QUERY_TYPE.DEFAULT,
|
|
type: QUERY_TYPE.DEFAULT,
|
|
|
url: `https://api-adresse.data.gouv.fr/search/?q=${research}&type=municipality&autocomplete=1&limit=20`,
|
|
url: `https://api-adresse.data.gouv.fr/search/?q=${research}&type=municipality&autocomplete=1&limit=20`,
|
|
|
params: {
|
|
params: {
|
|
|
noXaccessId: true
|
|
noXaccessId: true
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
- const apiResponse = response.data.features.map((data:AnyJson)=>data.properties)
|
|
|
|
|
|
|
+ const apiResponse = response.data.features.map((data: AnyJson) => data.properties)
|
|
|
|
|
|
|
|
// Par défaut on insère les valeurs que l'utilisateur a écrit, car un nom de ville ou de CP peut être absent de l'API
|
|
// Par défaut on insère les valeurs que l'utilisateur a écrit, car un nom de ville ou de CP peut être absent de l'API
|
|
|
const defaultResponse = []
|
|
const defaultResponse = []
|
|
|
- if(field === 'addressPostal.addressCity'){
|
|
|
|
|
- defaultResponse.push({id:0, postcode: null, city: research})
|
|
|
|
|
- }else{
|
|
|
|
|
- defaultResponse.push({id:0, postcode: research, city: null})
|
|
|
|
|
|
|
+ if (field === 'addressPostal.addressCity') {
|
|
|
|
|
+ defaultResponse.push({id: 0, postcode: null, city: research})
|
|
|
|
|
+ } else {
|
|
|
|
|
+ defaultResponse.push({id: 0, postcode: research, city: null})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return defaultResponse.concat(apiResponse)
|
|
return defaultResponse.concat(apiResponse)
|
|
@@ -59,17 +45,22 @@ export class UseAddressPostal {
|
|
|
* @param updateRepository
|
|
* @param updateRepository
|
|
|
* @private
|
|
* @private
|
|
|
*/
|
|
*/
|
|
|
- private updateCpAddress(value:AnyJson, updateRepository: Function): void{
|
|
|
|
|
|
|
+ function updateCpAddress(value: AnyJson, updateRepository: Function): void {
|
|
|
//Si une valeur est présente
|
|
//Si une valeur est présente
|
|
|
- if(value){
|
|
|
|
|
- if(value.city)
|
|
|
|
|
|
|
+ if (value) {
|
|
|
|
|
+ if (value.city)
|
|
|
updateRepository(value.city, 'addressPostal.addressCity')
|
|
updateRepository(value.city, 'addressPostal.addressCity')
|
|
|
- if(value.postcode)
|
|
|
|
|
|
|
+ if (value.postcode)
|
|
|
updateRepository(value.postcode, 'addressPostal.postalCode')
|
|
updateRepository(value.postcode, 'addressPostal.postalCode')
|
|
|
- }else{
|
|
|
|
|
|
|
+ } else {
|
|
|
//Cas où on efface les valeurs des champs
|
|
//Cas où on efface les valeurs des champs
|
|
|
updateRepository(null, 'addressPostal.addressCity')
|
|
updateRepository(null, 'addressPostal.addressCity')
|
|
|
updateRepository(null, 'addressPostal.postalCode')
|
|
updateRepository(null, 'addressPostal.postalCode')
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ return {
|
|
|
|
|
+ searchFunction,
|
|
|
|
|
+ updateCpAddress
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|