Add.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <SmartModal v-if="show" dialog :title="t('team.new')" @close="hideModal">
  3. <template #body>
  4. <div class="flex flex-col px-2">
  5. <input
  6. id="selectLabelTeamAdd"
  7. v-model="name"
  8. v-focus
  9. class="input floating-input"
  10. placeholder=" "
  11. type="text"
  12. autocomplete="off"
  13. @keyup.enter="addNewTeam"
  14. />
  15. <label for="selectLabelTeamAdd">
  16. {{ t("action.label") }}
  17. </label>
  18. </div>
  19. </template>
  20. <template #footer>
  21. <span>
  22. <ButtonPrimary
  23. :label="t('action.save')"
  24. :loading="isLoading"
  25. @click.native="addNewTeam"
  26. />
  27. <ButtonSecondary
  28. :label="t('action.cancel')"
  29. @click.native="hideModal"
  30. />
  31. </span>
  32. </template>
  33. </SmartModal>
  34. </template>
  35. <script setup lang="ts">
  36. import { ref } from "@nuxtjs/composition-api"
  37. import { pipe } from "fp-ts/function"
  38. import * as TE from "fp-ts/TaskEither"
  39. import { createTeam } from "~/helpers/backend/mutations/Team"
  40. import { TeamNameCodec } from "~/helpers/backend/types/TeamName"
  41. import { useI18n, useToast } from "~/helpers/utils/composables"
  42. const t = useI18n()
  43. const toast = useToast()
  44. defineProps<{
  45. show: boolean
  46. }>()
  47. const emit = defineEmits<{
  48. (e: "hide-modal"): void
  49. }>()
  50. const name = ref<string | null>(null)
  51. const isLoading = ref(false)
  52. const addNewTeam = async () => {
  53. isLoading.value = true
  54. await pipe(
  55. TeamNameCodec.decode(name.value),
  56. TE.fromEither,
  57. TE.mapLeft(() => "invalid_name" as const),
  58. TE.chainW(createTeam),
  59. TE.match(
  60. (err) => {
  61. // err is of type "invalid_name" | GQLError<Err>
  62. if (err === "invalid_name") {
  63. toast.error(`${t("team.name_length_insufficient")}`)
  64. } else {
  65. // Handle GQL errors (use err obj)
  66. }
  67. },
  68. () => {
  69. toast.success(`${t("team.new_created")}`)
  70. hideModal()
  71. }
  72. )
  73. )()
  74. isLoading.value = false
  75. }
  76. const hideModal = () => {
  77. name.value = null
  78. emit("hide-modal")
  79. }
  80. </script>