Team.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { pipe } from "fp-ts/function"
  2. import * as TE from "fp-ts/TaskEither"
  3. import { runMutation } from "../GQLClient"
  4. import { TeamName } from "../types/TeamName"
  5. import {
  6. CreateTeamDocument,
  7. CreateTeamMutation,
  8. CreateTeamMutationVariables,
  9. DeleteTeamDocument,
  10. DeleteTeamMutation,
  11. DeleteTeamMutationVariables,
  12. LeaveTeamDocument,
  13. LeaveTeamMutation,
  14. LeaveTeamMutationVariables,
  15. RemoveTeamMemberDocument,
  16. RemoveTeamMemberMutation,
  17. RemoveTeamMemberMutationVariables,
  18. RenameTeamDocument,
  19. RenameTeamMutation,
  20. RenameTeamMutationVariables,
  21. TeamMemberRole,
  22. UpdateTeamMemberRoleDocument,
  23. UpdateTeamMemberRoleMutation,
  24. UpdateTeamMemberRoleMutationVariables,
  25. } from "../graphql"
  26. type DeleteTeamErrors =
  27. | "team/not_required_role"
  28. | "team/invalid_id"
  29. | "team/member_not_found"
  30. | "ea/not_invite_or_admin"
  31. type LeaveTeamErrors =
  32. | "team/invalid_id"
  33. | "team/member_not_found"
  34. | "ea/not_invite_or_admin"
  35. type CreateTeamErrors = "team/name_invalid" | "ea/not_invite_or_admin"
  36. type RenameTeamErrors =
  37. | "ea/not_invite_or_admin"
  38. | "team/invalid_id"
  39. | "team/not_required_role"
  40. type UpdateTeamMemberRoleErrors =
  41. | "ea/not_invite_or_admin"
  42. | "team/invalid_id"
  43. | "team/not_required_role"
  44. type RemoveTeamMemberErrors =
  45. | "ea/not_invite_or_admin"
  46. | "team/invalid_id"
  47. | "team/not_required_role"
  48. export const createTeam = (name: TeamName) =>
  49. pipe(
  50. runMutation<
  51. CreateTeamMutation,
  52. CreateTeamMutationVariables,
  53. CreateTeamErrors
  54. >(CreateTeamDocument, {
  55. name,
  56. }),
  57. TE.map(({ createTeam }) => createTeam)
  58. )
  59. export const deleteTeam = (teamID: string) =>
  60. runMutation<
  61. DeleteTeamMutation,
  62. DeleteTeamMutationVariables,
  63. DeleteTeamErrors
  64. >(
  65. DeleteTeamDocument,
  66. {
  67. teamID,
  68. },
  69. {
  70. additionalTypenames: ["Team"],
  71. }
  72. )
  73. export const leaveTeam = (teamID: string) =>
  74. runMutation<LeaveTeamMutation, LeaveTeamMutationVariables, LeaveTeamErrors>(
  75. LeaveTeamDocument,
  76. {
  77. teamID,
  78. },
  79. {
  80. additionalTypenames: ["Team"],
  81. }
  82. )
  83. export const renameTeam = (teamID: string, newName: TeamName) =>
  84. pipe(
  85. runMutation<
  86. RenameTeamMutation,
  87. RenameTeamMutationVariables,
  88. RenameTeamErrors
  89. >(RenameTeamDocument, {
  90. newName,
  91. teamID,
  92. }),
  93. TE.map(({ renameTeam }) => renameTeam)
  94. )
  95. export const updateTeamMemberRole = (
  96. userUid: string,
  97. teamID: string,
  98. newRole: TeamMemberRole
  99. ) =>
  100. pipe(
  101. runMutation<
  102. UpdateTeamMemberRoleMutation,
  103. UpdateTeamMemberRoleMutationVariables,
  104. UpdateTeamMemberRoleErrors
  105. >(UpdateTeamMemberRoleDocument, {
  106. newRole,
  107. userUid,
  108. teamID,
  109. }),
  110. TE.map(({ updateTeamMemberRole }) => updateTeamMemberRole)
  111. )
  112. export const removeTeamMember = (userUid: string, teamID: string) =>
  113. runMutation<
  114. RemoveTeamMemberMutation,
  115. RemoveTeamMemberMutationVariables,
  116. RemoveTeamMemberErrors
  117. >(RemoveTeamMemberDocument, {
  118. userUid,
  119. teamID,
  120. })