Invite.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <template>
  2. <SmartModal v-if="show" :title="$t('team.invite')" @close="hideModal">
  3. <template #body>
  4. <div v-if="sendInvitesResult.length" class="flex flex-col px-4">
  5. <div class="flex flex-col max-w-md justify-center items-center">
  6. <SmartIcon class="h-6 text-accent w-6" name="users" />
  7. <h3 class="my-2 text-center text-lg">
  8. {{ $t("team.we_sent_invite_link") }}
  9. </h3>
  10. <p class="text-center">
  11. {{ $t("team.we_sent_invite_link_description") }}
  12. </p>
  13. </div>
  14. <div
  15. class="
  16. flex
  17. border border-dividerLight
  18. mt-8
  19. rounded
  20. flex-col
  21. space-y-6
  22. p-4
  23. "
  24. >
  25. <div
  26. v-for="(invitee, index) in sendInvitesResult"
  27. :key="`invitee-${index}`"
  28. >
  29. <p class="flex items-center">
  30. <i
  31. class="material-icons mr-4"
  32. :class="
  33. invitee.status === 'error' ? 'text-red-500' : 'text-green-500'
  34. "
  35. >
  36. {{
  37. invitee.status === "error"
  38. ? "error_outline"
  39. : "mark_email_read"
  40. }}
  41. </i>
  42. <span class="truncate">{{ invitee.email }}</span>
  43. </p>
  44. <p v-if="invitee.status === 'error'" class="ml-8 text-red-500 mt-2">
  45. {{ getErrorMessage(invitee.error) }}
  46. </p>
  47. </div>
  48. </div>
  49. </div>
  50. <div
  51. v-else-if="sendingInvites"
  52. class="flex p-4 items-center justify-center"
  53. >
  54. <SmartSpinner />
  55. </div>
  56. <div v-else class="flex flex-col px-2">
  57. <div class="flex flex-1 justify-between items-center">
  58. <label for="memberList" class="pb-4 px-4">
  59. {{ $t("team.pending_invites") }}
  60. </label>
  61. </div>
  62. <div class="divide-y divide-dividerLight border-divider border rounded">
  63. <div
  64. v-if="pendingInvites.loading"
  65. class="flex p-4 items-center justify-center"
  66. >
  67. <SmartSpinner />
  68. </div>
  69. <div v-else>
  70. <div
  71. v-if="!pendingInvites.loading && E.isRight(pendingInvites.data)"
  72. >
  73. <div
  74. v-for="(invitee, index) in pendingInvites.data.right.team
  75. .teamInvitations"
  76. :key="`invitee-${index}`"
  77. class="divide-x divide-dividerLight flex"
  78. >
  79. <input
  80. v-if="invitee"
  81. class="
  82. bg-transparent
  83. flex flex-1
  84. text-secondaryLight
  85. py-2
  86. px-4
  87. "
  88. :placeholder="`${$t('team.email')}`"
  89. :name="'param' + index"
  90. :value="invitee.inviteeEmail"
  91. readonly
  92. />
  93. <input
  94. class="
  95. bg-transparent
  96. flex flex-1
  97. text-secondaryLight
  98. py-2
  99. px-4
  100. "
  101. :placeholder="`${$t('team.permissions')}`"
  102. :name="'value' + index"
  103. :value="
  104. typeof invitee.inviteeRole === 'string'
  105. ? invitee.inviteeRole
  106. : JSON.stringify(invitee.inviteeRole)
  107. "
  108. readonly
  109. />
  110. <div class="flex">
  111. <ButtonSecondary
  112. v-tippy="{ theme: 'tooltip' }"
  113. :title="$t('action.remove')"
  114. svg="trash"
  115. color="red"
  116. @click.native="removeInvitee(invitee.id)"
  117. />
  118. </div>
  119. </div>
  120. </div>
  121. <div
  122. v-if="
  123. E.isRight(pendingInvites.data) &&
  124. pendingInvites.data.right.team.teamInvitations.length === 0
  125. "
  126. class="
  127. flex flex-col
  128. text-secondaryLight
  129. p-4
  130. items-center
  131. justify-center
  132. "
  133. >
  134. <span class="text-center">
  135. {{ $t("empty.pending_invites") }}
  136. </span>
  137. </div>
  138. <div
  139. v-if="!pendingInvites.loading && E.isLeft(pendingInvites.data)"
  140. class="flex flex-col p-4 items-center"
  141. >
  142. <i class="mb-4 material-icons">help_outline</i>
  143. {{ $t("error.something_went_wrong") }}
  144. </div>
  145. </div>
  146. </div>
  147. <div class="flex pt-4 flex-1 justify-between items-center">
  148. <label for="memberList" class="p-4">
  149. {{ $t("team.invite_tooltip") }}
  150. </label>
  151. <div class="flex">
  152. <ButtonSecondary
  153. svg="plus"
  154. :label="$t('add.new')"
  155. filled
  156. @click.native="addNewInvitee"
  157. />
  158. </div>
  159. </div>
  160. <div class="divide-y divide-dividerLight border-divider border rounded">
  161. <div
  162. v-for="(invitee, index) in newInvites"
  163. :key="`new-invitee-${index}`"
  164. class="divide-x divide-dividerLight flex"
  165. >
  166. <input
  167. v-model="invitee.key"
  168. class="bg-transparent flex flex-1 py-2 px-4"
  169. :placeholder="$t('team.email')"
  170. :name="'invitee' + index"
  171. autofocus
  172. />
  173. <span>
  174. <tippy
  175. ref="newInviteeOptions"
  176. interactive
  177. trigger="click"
  178. theme="popover"
  179. arrow
  180. >
  181. <template #trigger>
  182. <span class="select-wrapper">
  183. <input
  184. class="
  185. bg-transparent
  186. cursor-pointer
  187. flex flex-1
  188. py-2
  189. px-4
  190. "
  191. :placeholder="$t('team.permissions')"
  192. :name="'value' + index"
  193. :value="
  194. typeof invitee.value === 'string'
  195. ? invitee.value
  196. : JSON.stringify(invitee.value)
  197. "
  198. readonly
  199. />
  200. </span>
  201. </template>
  202. <SmartItem
  203. label="OWNER"
  204. @click.native="
  205. () => {
  206. updateNewInviteeRole(index, 'OWNER')
  207. newInviteeOptions[index].tippy().hide()
  208. }
  209. "
  210. />
  211. <SmartItem
  212. label="EDITOR"
  213. @click.native="
  214. () => {
  215. updateNewInviteeRole(index, 'EDITOR')
  216. newInviteeOptions[index].tippy().hide()
  217. }
  218. "
  219. />
  220. <SmartItem
  221. label="VIEWER"
  222. @click.native="
  223. () => {
  224. updateNewInviteeRole(index, 'VIEWER')
  225. newInviteeOptions[index].tippy().hide()
  226. }
  227. "
  228. />
  229. </tippy>
  230. </span>
  231. <div class="flex">
  232. <ButtonSecondary
  233. id="member"
  234. v-tippy="{ theme: 'tooltip' }"
  235. :title="$t('action.remove')"
  236. svg="trash"
  237. color="red"
  238. @click.native="removeNewInvitee(index)"
  239. />
  240. </div>
  241. </div>
  242. <div
  243. v-if="newInvites.length === 0"
  244. class="
  245. flex flex-col
  246. text-secondaryLight
  247. p-4
  248. items-center
  249. justify-center
  250. "
  251. >
  252. <img
  253. :src="`/images/states/${$colorMode.value}/add_group.svg`"
  254. loading="lazy"
  255. class="
  256. flex-col
  257. mb-4
  258. object-contain object-center
  259. h-16
  260. w-16
  261. inline-flex
  262. "
  263. />
  264. <span class="text-center pb-4">
  265. {{ $t("empty.invites") }}
  266. </span>
  267. <ButtonSecondary
  268. :label="$t('add.new')"
  269. filled
  270. @click.native="addNewInvitee"
  271. />
  272. </div>
  273. </div>
  274. <div
  275. v-if="newInvites.length"
  276. class="
  277. px-4
  278. mt-4
  279. py-4
  280. rounded
  281. border border-dividerLight
  282. bg-primaryLight
  283. "
  284. >
  285. <span class="pb-2 flex items-center font-semibold">
  286. <i class="text-secondaryLight mr-2 material-icons">help_outline</i>
  287. {{ $t("profile.roles") }}
  288. </span>
  289. <p>
  290. <span class="text-secondaryLight">
  291. {{ $t("profile.roles_description") }}
  292. </span>
  293. </p>
  294. <ul class="mt-4 space-y-4">
  295. <li class="flex">
  296. <span
  297. class="
  298. font-semibold
  299. text-secondaryDark
  300. uppercase
  301. truncate
  302. max-w-16
  303. w-1/4
  304. "
  305. >
  306. {{ $t("profile.owner") }}
  307. </span>
  308. <span class="flex flex-1">
  309. {{ $t("profile.owner_description") }}
  310. </span>
  311. </li>
  312. <li class="flex">
  313. <span
  314. class="
  315. font-semibold
  316. text-secondaryDark
  317. uppercase
  318. truncate
  319. max-w-16
  320. w-1/4
  321. "
  322. >
  323. {{ $t("profile.editor") }}
  324. </span>
  325. <span class="flex flex-1">
  326. {{ $t("profile.editor_description") }}
  327. </span>
  328. </li>
  329. <li class="flex">
  330. <span
  331. class="
  332. font-semibold
  333. text-secondaryDark
  334. uppercase
  335. truncate
  336. max-w-16
  337. w-1/4
  338. "
  339. >
  340. {{ $t("profile.viewer") }}
  341. </span>
  342. <span class="flex flex-1">
  343. {{ $t("profile.viewer_description") }}
  344. </span>
  345. </li>
  346. </ul>
  347. </div>
  348. </div>
  349. </template>
  350. <template #footer>
  351. <p
  352. v-if="sendInvitesResult.length"
  353. class="flex flex-1 text-secondaryLight justify-between"
  354. >
  355. <SmartAnchor
  356. class="link"
  357. :label="`← \xA0 ${$t('team.invite_more')}`"
  358. @click.native="
  359. () => {
  360. sendInvitesResult = []
  361. newInvites = [
  362. {
  363. key: '',
  364. value: 'VIEWRER',
  365. },
  366. ]
  367. }
  368. "
  369. />
  370. <SmartAnchor
  371. class="link"
  372. :label="`${$t('action.dismiss')}`"
  373. @click.native="hideModal"
  374. />
  375. </p>
  376. <span v-else>
  377. <ButtonPrimary :label="$t('team.invite')" @click.native="sendInvites" />
  378. <ButtonSecondary
  379. :label="$t('action.cancel')"
  380. @click.native="hideModal"
  381. />
  382. </span>
  383. </template>
  384. </SmartModal>
  385. </template>
  386. <script setup lang="ts">
  387. import {
  388. watch,
  389. ref,
  390. reactive,
  391. useContext,
  392. computed,
  393. } from "@nuxtjs/composition-api"
  394. import * as T from "fp-ts/Task"
  395. import * as E from "fp-ts/Either"
  396. import * as A from "fp-ts/Array"
  397. import * as O from "fp-ts/Option"
  398. import { flow, pipe } from "fp-ts/function"
  399. import { Email, EmailCodec } from "../../helpers/backend/types/Email"
  400. import {
  401. TeamInvitationAddedDocument,
  402. TeamInvitationRemovedDocument,
  403. TeamMemberRole,
  404. } from "../../helpers/backend/graphql"
  405. import {
  406. createTeamInvitation,
  407. CreateTeamInvitationErrors,
  408. revokeTeamInvitation,
  409. } from "../../helpers/backend/mutations/TeamInvitation"
  410. import { GQLError, useGQLQuery } from "~/helpers/backend/GQLClient"
  411. import {
  412. GetPendingInvitesDocument,
  413. GetPendingInvitesQuery,
  414. GetPendingInvitesQueryVariables,
  415. } from "~/helpers/backend/graphql"
  416. const {
  417. $toast,
  418. app: { i18n },
  419. } = useContext()
  420. const t = i18n.t.bind(i18n)
  421. const newInviteeOptions = ref<any | null>(null)
  422. const props = defineProps({
  423. show: Boolean,
  424. editingTeamID: { type: String, default: null },
  425. })
  426. const emit = defineEmits<{
  427. (e: "hide-modal"): void
  428. }>()
  429. const pendingInvites = useGQLQuery<
  430. GetPendingInvitesQuery,
  431. GetPendingInvitesQueryVariables,
  432. ""
  433. >({
  434. query: GetPendingInvitesDocument,
  435. variables: reactive({
  436. teamID: props.editingTeamID,
  437. }),
  438. updateSubs: computed(() =>
  439. !props.editingTeamID
  440. ? []
  441. : [
  442. {
  443. key: 4,
  444. query: TeamInvitationAddedDocument,
  445. variables: {
  446. teamID: props.editingTeamID,
  447. },
  448. },
  449. {
  450. key: 5,
  451. query: TeamInvitationRemovedDocument,
  452. variables: {
  453. teamID: props.editingTeamID,
  454. },
  455. },
  456. ]
  457. ),
  458. defer: true,
  459. })
  460. watch(
  461. () => props.editingTeamID,
  462. () => {
  463. if (props.editingTeamID) {
  464. pendingInvites.execute({
  465. teamID: props.editingTeamID,
  466. })
  467. }
  468. }
  469. )
  470. const removeInvitee = async (id: string) => {
  471. const result = await revokeTeamInvitation(id)()
  472. if (E.isLeft(result)) {
  473. $toast.error(`${t("error.something_went_wrong")}`, {
  474. icon: "error_outline",
  475. })
  476. } else {
  477. $toast.success(`${t("team.member_removed")}`, {
  478. icon: "person",
  479. })
  480. }
  481. }
  482. const newInvites = ref<Array<{ key: string; value: TeamMemberRole }>>([
  483. {
  484. key: "",
  485. value: TeamMemberRole.Viewer,
  486. },
  487. ])
  488. const addNewInvitee = () => {
  489. newInvites.value.push({
  490. key: "",
  491. value: TeamMemberRole.Viewer,
  492. })
  493. }
  494. const updateNewInviteeRole = (index: number, role: TeamMemberRole) => {
  495. newInvites.value[index].value = role
  496. }
  497. const removeNewInvitee = (id: number) => {
  498. newInvites.value.splice(id, 1)
  499. }
  500. type SendInvitesErrorType =
  501. | {
  502. email: Email
  503. status: "error"
  504. error: GQLError<CreateTeamInvitationErrors>
  505. }
  506. | {
  507. email: Email
  508. status: "success"
  509. }
  510. const sendInvitesResult = ref<Array<SendInvitesErrorType>>([])
  511. const sendingInvites = ref<boolean>(false)
  512. const sendInvites = async () => {
  513. const validationResult = pipe(
  514. newInvites.value,
  515. O.fromPredicate(
  516. (invites): invites is Array<{ key: Email; value: TeamMemberRole }> =>
  517. pipe(
  518. invites,
  519. A.every((invitee) => EmailCodec.is(invitee.key))
  520. )
  521. ),
  522. O.map(
  523. A.map((invitee) =>
  524. createTeamInvitation(invitee.key, invitee.value, props.editingTeamID)
  525. )
  526. )
  527. )
  528. if (O.isNone(validationResult)) {
  529. // Error handling for no validation
  530. $toast.error(`${t("error.incorrect_email")}`, {
  531. icon: "error_outline",
  532. })
  533. return
  534. }
  535. sendingInvites.value = true
  536. sendInvitesResult.value = await pipe(
  537. A.sequence(T.task)(validationResult.value),
  538. T.chain(
  539. flow(
  540. A.mapWithIndex((i, el) =>
  541. pipe(
  542. el,
  543. E.foldW(
  544. (err) => ({
  545. status: "error" as const,
  546. email: newInvites.value[i].key as Email,
  547. error: err,
  548. }),
  549. () => ({
  550. status: "success" as const,
  551. email: newInvites.value[i].key as Email,
  552. })
  553. )
  554. )
  555. ),
  556. T.of
  557. )
  558. )
  559. )()
  560. sendingInvites.value = false
  561. }
  562. const getErrorMessage = (error: SendInvitesErrorType) => {
  563. if (error.type === "network_error") {
  564. return t("error.network_error")
  565. } else {
  566. switch (error.error) {
  567. case "team/invalid_id":
  568. return t("team.invalid_id")
  569. case "team/member_not_found":
  570. return t("team.member_not_found")
  571. case "team_invite/already_member":
  572. return t("team.already_member")
  573. case "team_invite/member_has_invite":
  574. return t("team.member_has_invite")
  575. }
  576. }
  577. }
  578. const hideModal = () => {
  579. sendingInvites.value = false
  580. sendInvitesResult.value = []
  581. newInvites.value = [
  582. {
  583. key: "",
  584. value: TeamMemberRole.Viewer,
  585. },
  586. ]
  587. emit("hide-modal")
  588. }
  589. </script>