SubdomainNew.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. :model="model"
  10. :query="query()"
  11. :submitActions="submitActions">
  12. <template #form.input="{entry, updateRepository}">
  13. <v-container fluid class="container">
  14. <v-row>
  15. <v-col cols="12" sm="6">
  16. <div>{{ $t('pleaseEnterYourNewSubdomain')}} :</div>
  17. </v-col>
  18. </v-row>
  19. <v-row>
  20. <v-col cols="12" sm="6">
  21. <UiInputText
  22. field="subdomain"
  23. label="subdomain"
  24. :data="entry['subdomain']"
  25. type="string"
  26. :rules="rules().subdomainRules"
  27. :error="subdomainError"
  28. :error-message="subdomainErrorMessage"
  29. @update="checkSubdomainHook($event, updateRepository)"
  30. />
  31. </v-col>
  32. </v-row>
  33. </v-container>
  34. </template>
  35. <template #form.button>
  36. <NuxtLink :to="{ path: '/parameters/communication'}" class="no-decoration">
  37. <v-btn class="mr-4 ot_light_grey ot_grey--text">
  38. {{ $t('back') }}
  39. </v-btn>
  40. </NuxtLink>
  41. </template>
  42. </UiForm>
  43. </v-card>
  44. </LayoutContainer>
  45. </main>
  46. </template>
  47. <script lang="ts">
  48. import {defineComponent, computed, useContext, useFetch} from '@nuxtjs/composition-api'
  49. import { Repository as VuexRepository } from '@vuex-orm/core/dist/src/repository/Repository'
  50. import { Query, Model } from '@vuex-orm/core'
  51. import {QUERY_TYPE, SUBMIT_TYPE} from '~/types/enums'
  52. import { repositoryHelper } from '~/services/store/repository'
  53. import {AnyJson, ApiResponse, MobytUserStatus} from "~/types/interfaces";
  54. import {Subdomain} from "~/models/Organization/Subdomain";
  55. import {useValidator} from "~/composables/form/useValidator";
  56. export default defineComponent({
  57. props: {
  58. id:{
  59. type: [Number, String],
  60. required: true
  61. }
  62. },
  63. setup () {
  64. const {$dataProvider, app:{i18n}} = useContext()
  65. const repository: VuexRepository<Model> = repositoryHelper.getRepository(Subdomain)
  66. const query: Query = repository.query()
  67. const { subdomainError, subdomainErrorMessage, checkSubdomainUnity } = useValidator($dataProvider, i18n).useHandleSubdomain()
  68. const submitActions = computed(() => {
  69. let actions:AnyJson = {}
  70. actions[SUBMIT_TYPE.SAVE_AND_BACK] = { path: `/parameters/communication` }
  71. return actions
  72. })
  73. // TODO: unity error message don't show up
  74. // TODO: submit is not blocked even when subdomain is invalid
  75. const checkSubdomainHook = async (subdomain: string, updateRepository: any) => {
  76. const valid = (subdomain?.match(/^[\w\-]{2,60}$/) !== null)
  77. console.log('valid: ' + valid)
  78. if (!valid) {
  79. return
  80. }
  81. await checkSubdomainUnity(subdomain)
  82. console.log('unity error: ' + subdomainError.value + ' - ' + subdomainErrorMessage.value)
  83. if (!subdomainError.value) {
  84. updateRepository(subdomain, 'subdomain')
  85. }
  86. }
  87. /** todo Computed properties needs to be returned as functions until nuxt3 : https://github.com/nuxt-community/composition-api/issues/207 **/
  88. return {
  89. model: Subdomain,
  90. query: () => query,
  91. rules: () => getRules(i18n),
  92. panel: 0,
  93. submitActions,
  94. subdomainError,
  95. subdomainErrorMessage,
  96. checkSubdomainHook
  97. }
  98. },
  99. beforeDestroy() {
  100. repositoryHelper.cleanRepository(Subdomain)
  101. }
  102. })
  103. function getRules (i18n: any) {
  104. return {
  105. subdomainRules: [
  106. (subdomain: string) => (subdomain?.length <= 60 && subdomain?.length >= 2) || i18n.t('subdomain_need_to_have_0_to_60_cars'),
  107. (subdomain: string) => subdomain?.match(/^[\w\-]+$/) !== null || i18n.t('subdomain_can_not_contain_spaces_or_special_cars'),
  108. ]
  109. }
  110. }
  111. </script>