mirror of
https://github.com/utopia-os/utopia-ui.git
synced 2026-02-06 09:55:47 +00:00
Redeem invite link whenever a profile is available
This commit is contained in:
parent
1e7320b895
commit
886cbd75ec
@ -165,7 +165,10 @@ function App() {
|
|||||||
<Content>
|
<Content>
|
||||||
<Quests />
|
<Quests />
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path='/*' element={<MapContainer map={map} layers={layers} />}>
|
<Route
|
||||||
|
path='/*'
|
||||||
|
element={<MapContainer map={map} layers={layers} inviteApi={inviteApi} />}
|
||||||
|
>
|
||||||
<Route path='invite/:id' element={<InvitePage inviteApi={inviteApi} />} />
|
<Route path='invite/:id' element={<InvitePage inviteApi={inviteApi} />} />
|
||||||
<Route path='login' element={<LoginPage inviteApi={inviteApi} />} />
|
<Route path='login' element={<LoginPage inviteApi={inviteApi} />} />
|
||||||
<Route path='signup' element={<SignupPage />} />
|
<Route path='signup' element={<SignupPage />} />
|
||||||
|
|||||||
@ -25,6 +25,7 @@ import {
|
|||||||
import { itemsApi } from '../api/itemsApi'
|
import { itemsApi } from '../api/itemsApi'
|
||||||
|
|
||||||
import type { Place } from '../api/directus'
|
import type { Place } from '../api/directus'
|
||||||
|
import type { InviteApi } from '@/api/inviteApi'
|
||||||
import type { LayerProps } from 'utopia-ui'
|
import type { LayerProps } from 'utopia-ui'
|
||||||
|
|
||||||
interface layerApi {
|
interface layerApi {
|
||||||
@ -32,7 +33,15 @@ interface layerApi {
|
|||||||
api: itemsApi<Place>
|
api: itemsApi<Place>
|
||||||
}
|
}
|
||||||
|
|
||||||
function MapContainer({ layers, map }: { layers: LayerProps[]; map: any }) {
|
function MapContainer({
|
||||||
|
layers,
|
||||||
|
map,
|
||||||
|
inviteApi,
|
||||||
|
}: {
|
||||||
|
layers: LayerProps[]
|
||||||
|
map: any
|
||||||
|
inviteApi: InviteApi
|
||||||
|
}) {
|
||||||
const [apis, setApis] = useState<layerApi[]>([])
|
const [apis, setApis] = useState<layerApi[]>([])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -86,6 +95,7 @@ function MapContainer({ layers, map }: { layers: LayerProps[]; map: any }) {
|
|||||||
expandLayerControl={map.expand_layer_control}
|
expandLayerControl={map.expand_layer_control}
|
||||||
tileServerUrl={map.tile_server_url}
|
tileServerUrl={map.tile_server_url}
|
||||||
tileServerAttribution={map.tile_server_attribution}
|
tileServerAttribution={map.tile_server_attribution}
|
||||||
|
inviteApi={inviteApi}
|
||||||
>
|
>
|
||||||
{layers &&
|
{layers &&
|
||||||
apis &&
|
apis &&
|
||||||
|
|||||||
@ -3,8 +3,10 @@ import { MapContainer } from 'react-leaflet'
|
|||||||
|
|
||||||
import { ContextWrapper } from '#components/AppShell/ContextWrapper'
|
import { ContextWrapper } from '#components/AppShell/ContextWrapper'
|
||||||
|
|
||||||
|
import { useRedeemInvite } from './hooks/useRedeemInvite'
|
||||||
import { UtopiaMapInner } from './UtopiaMapInner'
|
import { UtopiaMapInner } from './UtopiaMapInner'
|
||||||
|
|
||||||
|
import type { InviteApi } from '#types/InviteApi'
|
||||||
import type { GeoJsonObject } from 'geojson'
|
import type { GeoJsonObject } from 'geojson'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -58,6 +60,7 @@ function UtopiaMap({
|
|||||||
expandLayerControl,
|
expandLayerControl,
|
||||||
tileServerUrl,
|
tileServerUrl,
|
||||||
tileServerAttribution,
|
tileServerAttribution,
|
||||||
|
inviteApi,
|
||||||
}: {
|
}: {
|
||||||
/** height of the map (default '500px') */
|
/** height of the map (default '500px') */
|
||||||
height?: string
|
height?: string
|
||||||
@ -91,7 +94,11 @@ function UtopiaMap({
|
|||||||
tileServerUrl?: string
|
tileServerUrl?: string
|
||||||
/** configure a custom tile server attribution */
|
/** configure a custom tile server attribution */
|
||||||
tileServerAttribution?: string
|
tileServerAttribution?: string
|
||||||
|
/** API to redeem invite codes */
|
||||||
|
inviteApi: InviteApi
|
||||||
}) {
|
}) {
|
||||||
|
// Check for invite code in localStorage and loaded profile, redeem if possible
|
||||||
|
useRedeemInvite(inviteApi)
|
||||||
return (
|
return (
|
||||||
<ContextWrapper>
|
<ContextWrapper>
|
||||||
<MapContainer
|
<MapContainer
|
||||||
|
|||||||
32
lib/src/Components/Map/hooks/useRedeemInvite.ts
Normal file
32
lib/src/Components/Map/hooks/useRedeemInvite.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { useEffect } from 'react'
|
||||||
|
import { useNavigate } from 'react-router-dom'
|
||||||
|
import { toast } from 'react-toastify'
|
||||||
|
|
||||||
|
import { useMyProfile } from './useMyProfile'
|
||||||
|
|
||||||
|
import type { InviteApi } from '#types/InviteApi'
|
||||||
|
|
||||||
|
export const useRedeemInvite = (inviteApi: InviteApi) => {
|
||||||
|
const inviteCode = localStorage.getItem('inviteCode')
|
||||||
|
|
||||||
|
const { myProfile } = useMyProfile()
|
||||||
|
const navigate = useNavigate()
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
async function redeemInvite() {
|
||||||
|
if (!inviteCode || !myProfile) return
|
||||||
|
|
||||||
|
const invitingProfileId = await inviteApi.redeemInvite(inviteCode, myProfile.id)
|
||||||
|
|
||||||
|
if (invitingProfileId) {
|
||||||
|
toast.success('Invite redeemed successfully!')
|
||||||
|
localStorage.removeItem('inviteCode')
|
||||||
|
navigate(`/item/${invitingProfileId}`)
|
||||||
|
} else {
|
||||||
|
toast.error('Failed to redeem invite')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void redeemInvite()
|
||||||
|
}, [inviteApi, inviteCode, myProfile, navigate])
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user