Add.vue 2.1 KB

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