mirror of
https://github.com/myfatemi04/wheelshare-frontend.git
synced 2025-04-17 17:40:16 -04:00
add group link resolver
This commit is contained in:
parent
7cd5323ff7
commit
f0c4dd3c40
|
@ -16,6 +16,7 @@ const Authenticator = lazy(() => import('./Authentication/Authenticator'));
|
||||||
const CarpoolPage = lazy(() => import('./Carpool/CarpoolPage'));
|
const CarpoolPage = lazy(() => import('./Carpool/CarpoolPage'));
|
||||||
const EventPage = lazy(() => import('./Event/EventPage'));
|
const EventPage = lazy(() => import('./Event/EventPage'));
|
||||||
const GroupPage = lazy(() => import('./Group/GroupPage'));
|
const GroupPage = lazy(() => import('./Group/GroupPage'));
|
||||||
|
const GroupSharedLinkResolver = lazy(() => import('./GroupSharedLinkResolver'));
|
||||||
|
|
||||||
const style: CSSProperties = {
|
const style: CSSProperties = {
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
|
@ -59,6 +60,10 @@ export default function App() {
|
||||||
<NotificationsProvider>
|
<NotificationsProvider>
|
||||||
<Header />
|
<Header />
|
||||||
<Route path="/" exact component={WheelShare} />
|
<Route path="/" exact component={WheelShare} />
|
||||||
|
<Route
|
||||||
|
path="/join/:code"
|
||||||
|
component={GroupSharedLinkResolver}
|
||||||
|
/>
|
||||||
<Route path="/carpools/:id" component={CarpoolPage} />
|
<Route path="/carpools/:id" component={CarpoolPage} />
|
||||||
<Route path="/events/:id" component={EventPage} />
|
<Route path="/events/:id" component={EventPage} />
|
||||||
<Route path="/groups/:id" component={GroupPage} />
|
<Route path="/groups/:id" component={GroupPage} />
|
||||||
|
|
67
src/components/GroupSharedLinkResolver.tsx
Normal file
67
src/components/GroupSharedLinkResolver.tsx
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
|
import { useParams } from 'react-router-dom';
|
||||||
|
import { joinGroup, resolveCode } from './api';
|
||||||
|
import { GroupPreview } from './GroupJoinerLink';
|
||||||
|
import UIButton from './UI/UIButton';
|
||||||
|
import UILink from './UI/UILink';
|
||||||
|
|
||||||
|
export default function GroupSharedLinkResolver() {
|
||||||
|
const { code } = useParams<{ code: string }>();
|
||||||
|
const [group, setGroup] = useState<GroupPreview>();
|
||||||
|
const [resolving, setResolving] = useState(false);
|
||||||
|
const [joining, setJoining] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setResolving(true);
|
||||||
|
resolveCode(code)
|
||||||
|
.then(setGroup)
|
||||||
|
.finally(() => setResolving(false));
|
||||||
|
}, [code]);
|
||||||
|
|
||||||
|
const join = useCallback(() => {
|
||||||
|
const id = group?.id;
|
||||||
|
if (code && id) {
|
||||||
|
console.log('Joining a group with the code', code);
|
||||||
|
setJoining(true);
|
||||||
|
joinGroup(id, code)
|
||||||
|
.then(({ status }) => {
|
||||||
|
if (status === 'success') {
|
||||||
|
window.location.href = '/groups/' + id;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.finally(() => setJoining(false));
|
||||||
|
}
|
||||||
|
}, [code, group?.id]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}
|
||||||
|
>
|
||||||
|
{resolving ? (
|
||||||
|
<h1>Searching for group...</h1>
|
||||||
|
) : !group ? (
|
||||||
|
<h1>No group found</h1>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<h2>{group.name}</h2>
|
||||||
|
{joining ? (
|
||||||
|
'Joining...'
|
||||||
|
) : (
|
||||||
|
<UIButton
|
||||||
|
onClick={join}
|
||||||
|
style={{
|
||||||
|
backgroundColor: '#f3f3f3',
|
||||||
|
width: '10ch',
|
||||||
|
textAlign: 'center',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Join
|
||||||
|
</UIButton>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
<br />
|
||||||
|
<UILink href="/">Home</UILink>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user