Selaa lähdekoodia

add unique subdomain validation function

Olivier Massot 3 vuotta sitten
vanhempi
commit
34e959e200
2 muutettua tiedostoa jossa 31 lisäystä ja 1 poistoa
  1. 30 1
      composables/form/useValidator.ts
  2. 1 0
      lang/rulesAndErrors/fr-FR.js

+ 30 - 1
composables/form/useValidator.ts

@@ -2,6 +2,7 @@ import { ref, Ref } from '@nuxtjs/composition-api'
 import VueI18n from 'vue-i18n'
 import { QUERY_TYPE } from '~/types/enums'
 import DataProvider from "~/services/data/dataProvider";
+import {ApiResponse} from "~/types/interfaces";
 
 /**
  * @category composables/form
@@ -38,7 +39,35 @@ export function useValidator($dataProvider: DataProvider, i18n: VueI18n) {
     }
   }
 
+  function useHandleSubdomain() {
+    const subdomainError: Ref<boolean> = ref(false)
+    const subdomainErrorMessage: Ref<string> = ref('')
+
+    const checkSubdomainUnity = async (subdomain: string) => {
+      const response: ApiResponse = await $dataProvider.invoke({
+        type: QUERY_TYPE.DEFAULT,
+        url: '/api/subdomains/?subdomain[]=' + subdomain
+      })
+      if (typeof response !== 'undefined') {
+        // @ts-ignore
+        if (response.metadata.totalItems > 0) {
+          subdomainError.value = true
+          subdomainErrorMessage.value = i18n.t('this_subdomain_is_already_in_use') as string
+        }
+      } else {
+        subdomainError.value = true
+        subdomainErrorMessage.value = i18n.t('could_not_contact_server_please_try_again') as string
+      }
+    }
+    return {
+      checkSubdomainUnity,
+      subdomainError,
+      subdomainErrorMessage
+    }
+  }
+
   return {
-    useHandleSiret
+    useHandleSiret,
+    useHandleSubdomain
   }
 }

+ 1 - 0
lang/rulesAndErrors/fr-FR.js

@@ -19,5 +19,6 @@ export default (context, locale) => {
     PRINCIPAL_non_unique: 'Vous ne pouvez pas avoir 2 points de contact principaux',
     BILL_non_unique: 'Vous ne pouvez pas avoir 2 points de contact de facturation',
     CONTACT_non_unique: 'Vous ne pouvez pas avoir 2 points de contact',
+    could_not_contact_server_please_try_again: 'Le serveur n\'a pas pu être contacté, veuillez réessayer un peu plus tard',
   })
 }