Add.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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, useContext } 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. const {
  38. app: { i18n },
  39. $toast,
  40. } = useContext()
  41. const t = i18n.t.bind(i18n)
  42. defineProps<{
  43. show: boolean
  44. }>()
  45. const emit = defineEmits<{
  46. (e: "hide-modal"): void
  47. }>()
  48. const name = ref<string | null>(null)
  49. const addNewTeam = () =>
  50. pipe(
  51. TeamNameCodec.decode(name.value), // Perform decode (returns either)
  52. TE.fromEither, // Convert either to a task either
  53. TE.mapLeft(() => "invalid_name" as const), // Failure above is an invalid_name, give it an identifiable value
  54. TE.chainW(createTeam), // Create the team
  55. TE.match(
  56. (err) => {
  57. // err is of type "invalid_name" | GQLError<Err>
  58. if (err === "invalid_name") {
  59. $toast.error(`${t("team.name_length_insufficient")}`)
  60. } else {
  61. // Handle GQL errors (use err obj)
  62. }
  63. },
  64. () => {
  65. // Success logic ?
  66. hideModal()
  67. }
  68. )
  69. )()
  70. const hideModal = () => {
  71. name.value = null
  72. emit("hide-modal")
  73. }
  74. </script>