Invite.vue 17 KB

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