SubdomainNew.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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)"
  30. />
  31. </v-col>
  32. </v-row>
  33. <i v-if="validationPending" class="validation_status">{{ $t('validation_ongoing') }}</i>
  34. <i v-else-if="subdomainAvailable === true" class="validation_status valid">{{ $t('this_subdomain_is_available') }}</i>
  35. </v-container>
  36. </template>
  37. <template #form.button>
  38. <NuxtLink :to="{ path: '/parameters/communication'}" class="no-decoration">
  39. <v-btn class="mr-4 ot_light_grey ot_grey--text">
  40. {{ $t('back') }}
  41. </v-btn>
  42. </NuxtLink>
  43. </template>
  44. </UiForm>
  45. </v-card>
  46. </LayoutContainer>
  47. </main>
  48. </template>
  49. <script lang="ts">
  50. import {defineComponent, computed, useContext, Ref, ref} from '@nuxtjs/composition-api'
  51. import { Repository as VuexRepository } from '@vuex-orm/core/dist/src/repository/Repository'
  52. import { Query, Model } from '@vuex-orm/core'
  53. import {SUBMIT_TYPE} from '~/types/enums'
  54. import { repositoryHelper } from '~/services/store/repository'
  55. import {AnyJson} from "~/types/interfaces";
  56. import {Subdomain} from "~/models/Organization/Subdomain";
  57. import {useValidator} from "~/composables/form/useValidator";
  58. export default defineComponent({
  59. props: {
  60. id:{
  61. type: [Number, String],
  62. required: true
  63. }
  64. },
  65. setup () {
  66. const {$dataProvider, app:{i18n}} = useContext()
  67. const repository: VuexRepository<Model> = repositoryHelper.getRepository(Subdomain)
  68. const query: Query = repository.query()
  69. const subdomainAvailable: Ref<boolean | null> = ref(null)
  70. const form = ref(null);
  71. const validationPending: Ref<boolean> = ref(false)
  72. const { checkSubdomainAvailability } = useValidator($dataProvider, i18n).useHandleSubdomain()
  73. const submitActions = computed(() => {
  74. let actions:AnyJson = {}
  75. actions[SUBMIT_TYPE.SAVE_AND_BACK] = { path: `/parameters/communication` }
  76. return actions
  77. })
  78. const checkSubdomainHook = async (subdomain: string | null, updateRepository: any) => {
  79. subdomainAvailable.value = null
  80. if (subdomain !== null && subdomain.match(/^[\w\-]{2,60}$/) !== null) {
  81. validationPending.value = true
  82. subdomainAvailable.value = await checkSubdomainAvailability(subdomain);
  83. validationPending.value = false
  84. }
  85. updateRepository(subdomain, 'subdomain');
  86. (form.value as any).validate()
  87. }
  88. /** todo Computed properties needs to be returned as functions until nuxt3 : https://github.com/nuxt-community/composition-api/issues/207 **/
  89. return {
  90. model: Subdomain,
  91. query: () => query,
  92. submitActions,
  93. form,
  94. checkSubdomainHook,
  95. validationPending,
  96. subdomainAvailable,
  97. rules: () => [
  98. (subdomain: string | null) => (subdomain !== null) || i18n.t('please_enter_a_value_for_the_subdomain'),
  99. (subdomain: string | null) => (subdomain !== null && subdomain.length >= 2 && subdomain.length <=60) || i18n.t('subdomain_need_to_have_0_to_60_cars'),
  100. (subdomain: string | null) => (subdomain !== null && subdomain.match(/^[\w\-]+$/) !== null) || i18n.t('subdomain_can_not_contain_spaces_or_special_cars'),
  101. () => (subdomainAvailable.value === true) || i18n.t('this_subdomain_is_already_in_use')
  102. ]
  103. }
  104. },
  105. beforeDestroy() {
  106. repositoryHelper.cleanRepository(Subdomain)
  107. }
  108. })
  109. </script>
  110. <style scoped>
  111. .validation_status {
  112. font-size: 13px;
  113. }
  114. .validation_status.valid {
  115. color: green;
  116. }
  117. </style>