use-modal.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import { reactive, ref, SetupContext } from 'vue'
  18. import { useI18n } from 'vue-i18n'
  19. import { useAsyncState } from '@vueuse/core'
  20. import {
  21. queryWorkerAddressList,
  22. saveWorkerGroup
  23. } from '@/service/modules/worker-groups'
  24. export function useModal(
  25. props: any,
  26. ctx: SetupContext<('cancelModal' | 'confirmModal')[]>
  27. ) {
  28. const { t } = useI18n()
  29. const variables = reactive({
  30. workerGroupFormRef: ref(),
  31. model: {
  32. id: ref<number>(-1),
  33. name: ref(''),
  34. addrList: ref<Array<number>>([]),
  35. generalOptions: []
  36. },
  37. rules: {
  38. name: {
  39. required: true,
  40. trigger: ['input', 'blur'],
  41. validator() {
  42. if (variables.model.name === '') {
  43. return new Error(t('security.worker_group.group_name_tips'))
  44. }
  45. }
  46. },
  47. addrList: {
  48. required: true,
  49. trigger: ['input', 'blur'],
  50. validator() {
  51. if (variables.model.addrList.length < 1) {
  52. return new Error(t('security.worker_group.worker_addresses_tips'))
  53. }
  54. }
  55. }
  56. }
  57. })
  58. const getListData = () => {
  59. const { state } = useAsyncState(
  60. queryWorkerAddressList().then((res: Array<string>) => {
  61. variables.model.generalOptions = res.map(
  62. (item): { label: string; value: string } => {
  63. return {
  64. label: item,
  65. value: item
  66. }
  67. }
  68. ) as any
  69. }),
  70. {}
  71. )
  72. return state
  73. }
  74. const handleValidate = (statusRef: number) => {
  75. variables.workerGroupFormRef.validate((errors: any) => {
  76. if (!errors) {
  77. statusRef === 0 ? submitWorkerGroupModal() : updateWorkerGroupModal()
  78. } else {
  79. return
  80. }
  81. })
  82. }
  83. const submitWorkerGroupModal = () => {
  84. const data = {
  85. name: variables.model.name,
  86. addrList: variables.model.addrList.toString()
  87. }
  88. saveWorkerGroup(data).then(() => {
  89. variables.model.name = ''
  90. variables.model.addrList = []
  91. ctx.emit('confirmModal', props.showModalRef)
  92. })
  93. }
  94. const updateWorkerGroupModal = () => {
  95. const data = {
  96. id: variables.model.id,
  97. name: variables.model.name,
  98. addrList: variables.model.addrList.toString()
  99. }
  100. saveWorkerGroup(data).then(() => {
  101. ctx.emit('confirmModal', props.showModalRef)
  102. })
  103. }
  104. return {
  105. variables,
  106. handleValidate,
  107. getListData
  108. }
  109. }