|
@@ -1,75 +0,0 @@
|
|
|
-import { AnyJson } from '~/types/interfaces'
|
|
|
|
|
-import {QUERY_TYPE} from "~/types/enums";
|
|
|
|
|
-import { useContext } from '@nuxtjs/composition-api'
|
|
|
|
|
-import DataProvider from "~/services/data/dataProvider";
|
|
|
|
|
-
|
|
|
|
|
-/**
|
|
|
|
|
- * @category composables/data
|
|
|
|
|
- * @class UseAddressPostal
|
|
|
|
|
- * Use Classe pour gérer les deux champs postal code et adresseCity
|
|
|
|
|
- */
|
|
|
|
|
-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),
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Fonction de recherche qui utilise l'API gouvernematal pour autocompléter les CP et villes.
|
|
|
|
|
- * @param research
|
|
|
|
|
- * @param field
|
|
|
|
|
- * @private
|
|
|
|
|
- */
|
|
|
|
|
- private async searchFunction (research: string, field: string): Promise<Array<AnyJson>>{
|
|
|
|
|
- if(research){
|
|
|
|
|
- const response = await this.$dataProvider.invoke({
|
|
|
|
|
- type: QUERY_TYPE.DEFAULT,
|
|
|
|
|
- url: `https://api-adresse.data.gouv.fr/search/?q=${research}&type=municipality&autocomplete=1&limit=20`,
|
|
|
|
|
- params: {
|
|
|
|
|
- noXaccessId: true
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
- 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
|
|
|
|
|
- const defaultResponse = []
|
|
|
|
|
- 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 []
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Fonction permettant de mettre à jour le repo par rapport à la réponse (objet) de l'autocomplete
|
|
|
|
|
- * @param value
|
|
|
|
|
- * @param updateRepository
|
|
|
|
|
- * @private
|
|
|
|
|
- */
|
|
|
|
|
- private updateCpAddress(value:AnyJson, updateRepository: Function): void{
|
|
|
|
|
- //Si une valeur est présente
|
|
|
|
|
- if(value){
|
|
|
|
|
- if(value.city)
|
|
|
|
|
- updateRepository(value.city, 'addressPostal.addressCity')
|
|
|
|
|
- if(value.postcode)
|
|
|
|
|
- updateRepository(value.postcode, 'addressPostal.postalCode')
|
|
|
|
|
- }else{
|
|
|
|
|
- //Cas où on efface les valeurs des champs
|
|
|
|
|
- updateRepository(null, 'addressPostal.addressCity')
|
|
|
|
|
- updateRepository(null, 'addressPostal.postalCode')
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|