SubdomainNew.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <!-- Component d'un formulaire pour la saisie d'un nouveau subdomain -->
  2. <template>
  3. <main>
  4. <LayoutContainer>
  5. <v-card class="mb-5 mt-4">
  6. <FormToolbar title="subdomain" icon="fa-at"/>
  7. <UiForm
  8. :id="id"
  9. ref="form"
  10. :model="model"
  11. :query="query()"
  12. :submitActions="submitActions"
  13. >
  14. <template #form.input="{entry, updateRepository}">
  15. <v-container fluid class="container">
  16. <v-row>
  17. <v-col cols="12" sm="6">
  18. <div>{{ $t('pleaseEnterYourNewSubdomain')}} :</div>
  19. </v-col>
  20. </v-row>
  21. <v-row>
  22. <v-col cols="12" sm="6">
  23. <UiInputText
  24. field="subdomain"
  25. label="subdomain"
  26. :data="entry['subdomain']"
  27. type="string"
  28. :rules="rules()"
  29. @change="checkSubdomainHook($event); updateRepository(entry['subdomain'], 'subdomain')"
  30. />
  31. <span v-if="entry['subdomain'] && subdomainAvailable">Ce sous-domaine est disponible</span>
  32. </v-col>
  33. </v-row>
  34. </v-container>
  35. </template>
  36. <template #form.button>
  37. <NuxtLink :to="{ path: '/parameters/communication'}" class="no-decoration">
  38. <v-btn class="mr-4 ot_light_grey ot_grey--text">
  39. {{ $t('back') }}
  40. </v-btn>
  41. </NuxtLink>
  42. </template>
  43. </UiForm>
  44. </v-card>
  45. </LayoutContainer>
  46. </main>
  47. </template>
  48. <script lang="ts">
  49. import {defineComponent, computed, useContext, Ref, ref} from '@nuxtjs/composition-api'
  50. import { Repository as VuexRepository } from '@vuex-orm/core/dist/src/repository/Repository'
  51. import { Query, Model } from '@vuex-orm/core'
  52. import {SUBMIT_TYPE} from '~/types/enums'
  53. import { repositoryHelper } from '~/services/store/repository'
  54. import {AnyJson} from "~/types/interfaces";
  55. import {Subdomain} from "~/models/Organization/Subdomain";
  56. import {useValidator} from "~/composables/form/useValidator";
  57. export default defineComponent({
  58. props: {
  59. id:{
  60. type: [Number, String],
  61. required: true
  62. }
  63. },
  64. setup () {
  65. const {$dataProvider, app:{i18n}} = useContext()
  66. const repository: VuexRepository<Model> = repositoryHelper.getRepository(Subdomain)
  67. const query: Query = repository.query()
  68. const subdomainAvailable: Ref<boolean | null> = ref(null)
  69. const form = ref(null);
  70. const { checkSubdomainAvailability } = useValidator($dataProvider, i18n).useHandleSubdomain()
  71. const submitActions = computed(() => {
  72. let actions:AnyJson = {}
  73. actions[SUBMIT_TYPE.SAVE_AND_BACK] = { path: `/parameters/communication` }
  74. return actions
  75. })
  76. const checkSubdomainHook = async (subdomain: string | null) => {
  77. subdomainAvailable.value = null
  78. if (subdomain !== null && subdomain.match(/^[\w\-]{2,60}$/) !== null) {
  79. subdomainAvailable.value = await checkSubdomainAvailability(subdomain);
  80. (form.value as any).validate()
  81. }
  82. }
  83. /** todo Computed properties needs to be returned as functions until nuxt3 : https://github.com/nuxt-community/composition-api/issues/207 **/
  84. return {
  85. model: Subdomain,
  86. query: () => query,
  87. submitActions,
  88. form,
  89. checkSubdomainHook,
  90. subdomainAvailable,
  91. rules: () => [
  92. (subdomain: string | null) => (subdomain !== null) || i18n.t('please_enter_a_value_for_the_subdomain'),
  93. (subdomain: string | null) => (subdomain !== null && subdomain.length >= 2 && subdomain.length <=60) || i18n.t('subdomain_need_to_have_0_to_60_cars'),
  94. (subdomain: string | null) => (subdomain !== null && subdomain.match(/^[\w\-]+$/) !== null) || i18n.t('subdomain_can_not_contain_spaces_or_special_cars'),
  95. () => (subdomainAvailable.value === true) || i18n.t('this_subdomain_is_already_in_use')
  96. ]
  97. }
  98. },
  99. beforeDestroy() {
  100. repositoryHelper.cleanRepository(Subdomain)
  101. }
  102. })
  103. </script>