Add.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <SmartModal v-if="show" :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 :label="t('action.save')" @click.native="addNewTeam" />
  23. <ButtonSecondary
  24. :label="t('action.cancel')"
  25. @click.native="hideModal"
  26. />
  27. </span>
  28. </template>
  29. </SmartModal>
  30. </template>
  31. <script setup lang="ts">
  32. import { ref } from "@nuxtjs/composition-api"
  33. import { pipe } from "fp-ts/function"
  34. import * as TE from "fp-ts/TaskEither"
  35. import { createTeam } from "~/helpers/backend/mutations/Team"
  36. import { TeamNameCodec } from "~/helpers/backend/types/TeamName"
  37. import { useI18n, useToast } from "~/helpers/utils/composables"
  38. const t = useI18n()
  39. const toast = useToast()
  40. defineProps<{
  41. show: boolean
  42. }>()
  43. const emit = defineEmits<{
  44. (e: "hide-modal"): void
  45. }>()
  46. const name = ref<string | null>(null)
  47. const addNewTeam = () =>
  48. pipe(
  49. TeamNameCodec.decode(name.value), // Perform decode (returns either)
  50. TE.fromEither, // Convert either to a task either
  51. TE.mapLeft(() => "invalid_name" as const), // Failure above is an invalid_name, give it an identifiable value
  52. TE.chainW(createTeam), // Create the team
  53. TE.match(
  54. (err) => {
  55. // err is of type "invalid_name" | GQLError<Err>
  56. if (err === "invalid_name") {
  57. toast.error(`${t("team.name_length_insufficient")}`)
  58. } else {
  59. // Handle GQL errors (use err obj)
  60. }
  61. },
  62. () => {
  63. // Success logic ?
  64. hideModal()
  65. }
  66. )
  67. )()
  68. const hideModal = () => {
  69. name.value = null
  70. emit("hide-modal")
  71. }
  72. </script>