mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Basic page for accepting invites
This commit is contained in:
parent
691177284e
commit
d57cac47e7
2 changed files with 96 additions and 0 deletions
51
packages/web/lib/networking/mutations/joinGroupMutation.ts
Normal file
51
packages/web/lib/networking/mutations/joinGroupMutation.ts
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import { gql } from 'graphql-request'
|
||||
import { gqlFetcher } from '../networkHelpers'
|
||||
|
||||
export interface RecommendationGroup {
|
||||
id: string
|
||||
name: string
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
}
|
||||
|
||||
type JoinGroupResponse = {
|
||||
joinGroup?: JoinGroupData
|
||||
errorCodes?: string[]
|
||||
}
|
||||
|
||||
type JoinGroupData = {
|
||||
group: RecommendationGroup | undefined
|
||||
}
|
||||
|
||||
export async function joinGroupMutation(
|
||||
inviteCode: string
|
||||
): Promise<RecommendationGroup | undefined> {
|
||||
const mutation = gql`
|
||||
mutation JoinGroup($inviteCode: String!) {
|
||||
joinGroup(inviteCode: $inviteCode) {
|
||||
... on JoinGroupSuccess {
|
||||
group {
|
||||
id
|
||||
name
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
... on JoinGroupError {
|
||||
errorCodes
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
console.log('JoinGroupMutation', mutation)
|
||||
|
||||
const response = await gqlFetcher(mutation, { inviteCode })
|
||||
console.log(' -- response', response)
|
||||
const data = response as JoinGroupResponse | undefined
|
||||
const error = data?.errorCodes?.find(() => true)
|
||||
if (error) {
|
||||
throw error
|
||||
}
|
||||
return data?.joinGroup?.group
|
||||
}
|
||||
45
packages/web/pages/invite/[inviteCode].tsx
Normal file
45
packages/web/pages/invite/[inviteCode].tsx
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import { useRouter } from 'next/router'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { PageMetaData } from '../../components/patterns/PageMetaData'
|
||||
import { ProfileLayout } from '../../components/templates/ProfileLayout'
|
||||
import { joinGroupMutation } from '../../lib/networking/mutations/joinGroupMutation'
|
||||
import { showSuccessToast } from '../../lib/toastHelpers'
|
||||
|
||||
export default function InvitePage(): JSX.Element {
|
||||
const router = useRouter()
|
||||
const { inviteCode } = router.query
|
||||
|
||||
const [error, setError] = useState<string | undefined>(undefined)
|
||||
|
||||
useEffect(() => {
|
||||
if (!router.isReady) {
|
||||
return
|
||||
}
|
||||
if (!inviteCode || typeof inviteCode != 'string') {
|
||||
setError('No invite code provided')
|
||||
}
|
||||
|
||||
const joinGroup = async () => {
|
||||
try {
|
||||
const result = await joinGroupMutation(inviteCode as string)
|
||||
if (!result) {
|
||||
throw 'Unknown error occurred.'
|
||||
}
|
||||
showSuccessToast(`You have been added to the ${result.name} group`)
|
||||
} catch (error) {
|
||||
setError('Unable to join group')
|
||||
}
|
||||
}
|
||||
|
||||
joinGroup().catch((error) => {
|
||||
setError(error)
|
||||
})
|
||||
}, [router, inviteCode])
|
||||
return (
|
||||
<>
|
||||
<PageMetaData title="Accept Invite - Omnivore" path="/invite" />
|
||||
<ProfileLayout>Accepting invite to join</ProfileLayout>
|
||||
<div data-testid="invite-page-tag" />
|
||||
</>
|
||||
)
|
||||
}
|
||||
Loading…
Reference in a new issue