Invite.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. :alt="$t('empty.invites')"
  264. />
  265. <span class="text-center pb-4">
  266. {{ $t("empty.invites") }}
  267. </span>
  268. <ButtonSecondary
  269. :label="$t('add.new')"
  270. filled
  271. @click.native="addNewInvitee"
  272. />
  273. </div>
  274. </div>
  275. <div
  276. v-if="newInvites.length"
  277. class="
  278. px-4
  279. mt-4
  280. py-4
  281. rounded
  282. border border-dividerLight
  283. flex flex-col
  284. items-start
  285. "
  286. >
  287. <span
  288. class="
  289. mb-4
  290. px-2
  291. py-1
  292. flex
  293. justify-center
  294. items-center
  295. font-semibold
  296. rounded-full
  297. bg-primaryDark
  298. border border-divider
  299. "
  300. >
  301. <i class="text-secondaryLight mr-2 material-icons">help_outline</i>
  302. {{ $t("profile.roles") }}
  303. </span>
  304. <p>
  305. <span class="text-secondaryLight">
  306. {{ $t("profile.roles_description") }}
  307. </span>
  308. </p>
  309. <ul class="mt-4 space-y-4">
  310. <li class="flex">
  311. <span
  312. class="
  313. font-semibold
  314. text-secondaryDark
  315. uppercase
  316. truncate
  317. max-w-16
  318. w-1/4
  319. "
  320. >
  321. {{ $t("profile.owner") }}
  322. </span>
  323. <span class="flex flex-1">
  324. {{ $t("profile.owner_description") }}
  325. </span>
  326. </li>
  327. <li class="flex">
  328. <span
  329. class="
  330. font-semibold
  331. text-secondaryDark
  332. uppercase
  333. truncate
  334. max-w-16
  335. w-1/4
  336. "
  337. >
  338. {{ $t("profile.editor") }}
  339. </span>
  340. <span class="flex flex-1">
  341. {{ $t("profile.editor_description") }}
  342. </span>
  343. </li>
  344. <li class="flex">
  345. <span
  346. class="
  347. font-semibold
  348. text-secondaryDark
  349. uppercase
  350. truncate
  351. max-w-16
  352. w-1/4
  353. "
  354. >
  355. {{ $t("profile.viewer") }}
  356. </span>
  357. <span class="flex flex-1">
  358. {{ $t("profile.viewer_description") }}
  359. </span>
  360. </li>
  361. </ul>
  362. </div>
  363. </div>
  364. </template>
  365. <template #footer>
  366. <p
  367. v-if="sendInvitesResult.length"
  368. class="flex flex-1 text-secondaryLight justify-between"
  369. >
  370. <SmartAnchor
  371. class="link"
  372. :label="`← \xA0 ${$t('team.invite_more')}`"
  373. @click.native="
  374. () => {
  375. sendInvitesResult = []
  376. newInvites = [
  377. {
  378. key: '',
  379. value: 'VIEWRER',
  380. },
  381. ]
  382. }
  383. "
  384. />
  385. <SmartAnchor
  386. class="link"
  387. :label="`${$t('action.dismiss')}`"
  388. @click.native="hideModal"
  389. />
  390. </p>
  391. <span v-else>
  392. <ButtonPrimary :label="$t('team.invite')" @click.native="sendInvites" />
  393. <ButtonSecondary
  394. :label="$t('action.cancel')"
  395. @click.native="hideModal"
  396. />
  397. </span>
  398. </template>
  399. </SmartModal>
  400. </template>
  401. <script setup lang="ts">
  402. import {
  403. watch,
  404. ref,
  405. reactive,
  406. useContext,
  407. computed,
  408. } from "@nuxtjs/composition-api"
  409. import * as T from "fp-ts/Task"
  410. import * as E from "fp-ts/Either"
  411. import * as A from "fp-ts/Array"
  412. import * as O from "fp-ts/Option"
  413. import { flow, pipe } from "fp-ts/function"
  414. import { Email, EmailCodec } from "../../helpers/backend/types/Email"
  415. import {
  416. TeamInvitationAddedDocument,
  417. TeamInvitationRemovedDocument,
  418. TeamMemberRole,
  419. GetPendingInvitesDocument,
  420. GetPendingInvitesQuery,
  421. GetPendingInvitesQueryVariables,
  422. } from "../../helpers/backend/graphql"
  423. import {
  424. createTeamInvitation,
  425. CreateTeamInvitationErrors,
  426. revokeTeamInvitation,
  427. } from "../../helpers/backend/mutations/TeamInvitation"
  428. import { GQLError, useGQLQuery } from "~/helpers/backend/GQLClient"
  429. const {
  430. $toast,
  431. app: { i18n },
  432. } = useContext()
  433. const t = i18n.t.bind(i18n)
  434. const newInviteeOptions = ref<any | null>(null)
  435. const props = defineProps({
  436. show: Boolean,
  437. editingTeamID: { type: String, default: null },
  438. })
  439. const emit = defineEmits<{
  440. (e: "hide-modal"): void
  441. }>()
  442. const pendingInvites = useGQLQuery<
  443. GetPendingInvitesQuery,
  444. GetPendingInvitesQueryVariables,
  445. ""
  446. >({
  447. query: GetPendingInvitesDocument,
  448. variables: reactive({
  449. teamID: props.editingTeamID,
  450. }),
  451. updateSubs: computed(() =>
  452. !props.editingTeamID
  453. ? []
  454. : [
  455. {
  456. key: 4,
  457. query: TeamInvitationAddedDocument,
  458. variables: {
  459. teamID: props.editingTeamID,
  460. },
  461. },
  462. {
  463. key: 5,
  464. query: TeamInvitationRemovedDocument,
  465. variables: {
  466. teamID: props.editingTeamID,
  467. },
  468. },
  469. ]
  470. ),
  471. defer: true,
  472. })
  473. watch(
  474. () => props.editingTeamID,
  475. () => {
  476. if (props.editingTeamID) {
  477. pendingInvites.execute({
  478. teamID: props.editingTeamID,
  479. })
  480. }
  481. }
  482. )
  483. const removeInvitee = async (id: string) => {
  484. const result = await revokeTeamInvitation(id)()
  485. if (E.isLeft(result)) {
  486. $toast.error(`${t("error.something_went_wrong")}`, {
  487. icon: "error_outline",
  488. })
  489. } else {
  490. $toast.success(`${t("team.member_removed")}`, {
  491. icon: "person",
  492. })
  493. }
  494. }
  495. const newInvites = ref<Array<{ key: string; value: TeamMemberRole }>>([
  496. {
  497. key: "",
  498. value: TeamMemberRole.Viewer,
  499. },
  500. ])
  501. const addNewInvitee = () => {
  502. newInvites.value.push({
  503. key: "",
  504. value: TeamMemberRole.Viewer,
  505. })
  506. }
  507. const updateNewInviteeRole = (index: number, role: TeamMemberRole) => {
  508. newInvites.value[index].value = role
  509. }
  510. const removeNewInvitee = (id: number) => {
  511. newInvites.value.splice(id, 1)
  512. }
  513. type SendInvitesErrorType =
  514. | {
  515. email: Email
  516. status: "error"
  517. error: GQLError<CreateTeamInvitationErrors>
  518. }
  519. | {
  520. email: Email
  521. status: "success"
  522. }
  523. const sendInvitesResult = ref<Array<SendInvitesErrorType>>([])
  524. const sendingInvites = ref<boolean>(false)
  525. const sendInvites = async () => {
  526. const validationResult = pipe(
  527. newInvites.value,
  528. O.fromPredicate(
  529. (invites): invites is Array<{ key: Email; value: TeamMemberRole }> =>
  530. pipe(
  531. invites,
  532. A.every((invitee) => EmailCodec.is(invitee.key))
  533. )
  534. ),
  535. O.map(
  536. A.map((invitee) =>
  537. createTeamInvitation(invitee.key, invitee.value, props.editingTeamID)
  538. )
  539. )
  540. )
  541. if (O.isNone(validationResult)) {
  542. // Error handling for no validation
  543. $toast.error(`${t("error.incorrect_email")}`, {
  544. icon: "error_outline",
  545. })
  546. return
  547. }
  548. sendingInvites.value = true
  549. sendInvitesResult.value = await pipe(
  550. A.sequence(T.task)(validationResult.value),
  551. T.chain(
  552. flow(
  553. A.mapWithIndex((i, el) =>
  554. pipe(
  555. el,
  556. E.foldW(
  557. (err) => ({
  558. status: "error" as const,
  559. email: newInvites.value[i].key as Email,
  560. error: err,
  561. }),
  562. () => ({
  563. status: "success" as const,
  564. email: newInvites.value[i].key as Email,
  565. })
  566. )
  567. )
  568. ),
  569. T.of
  570. )
  571. )
  572. )()
  573. sendingInvites.value = false
  574. }
  575. const getErrorMessage = (error: SendInvitesErrorType) => {
  576. if (error.type === "network_error") {
  577. return t("error.network_error")
  578. } else {
  579. switch (error.error) {
  580. case "team/invalid_id":
  581. return t("team.invalid_id")
  582. case "team/member_not_found":
  583. return t("team.member_not_found")
  584. case "team_invite/already_member":
  585. return t("team.already_member")
  586. case "team_invite/member_has_invite":
  587. return t("team.member_has_invite")
  588. }
  589. }
  590. }
  591. const hideModal = () => {
  592. sendingInvites.value = false
  593. sendInvitesResult.value = []
  594. newInvites.value = [
  595. {
  596. key: "",
  597. value: TeamMemberRole.Viewer,
  598. },
  599. ]
  600. emit("hide-modal")
  601. }
  602. </script>