joining a group via code works now!

This commit is contained in:
Michael Fatemi 2021-07-06 23:01:13 -04:00
parent 543b9e948e
commit 6264e82d5e
2 changed files with 53 additions and 9 deletions

View File

@ -1,22 +1,45 @@
import { useCallback, useState } from 'react'; import { useCallback, useEffect, useState } from 'react';
import { joinGroup, resolveCode } from './api';
import UIButton from './UIButton'; import UIButton from './UIButton';
import UIPressable from './UIPressable'; import UIPressable from './UIPressable';
import UISecondaryBox from './UISecondaryBox'; import UISecondaryBox from './UISecondaryBox';
import UITextInput from './UITextInput'; import UITextInput from './UITextInput';
import useToggle from './useToggle'; import useToggle from './useToggle';
export type GroupPreview = {
name: string;
id: number;
};
function GroupJoiner() { function GroupJoiner() {
const [code, setCode] = useState(''); const [code, setCode] = useState('');
const [joining, setJoining] = useState(false); const [joining, setJoining] = useState(false);
const [group, setGroup] = useState<GroupPreview | null>(null);
useEffect(() => {
const initialCode = code;
resolveCode(code).then((group) => {
if (code === initialCode) {
setGroup(group);
}
});
}, [code]);
const join = useCallback(() => { const join = useCallback(() => {
if (code) { const id = group?.id;
if (code && id) {
console.log('Joining a group with the code', code); console.log('Joining a group with the code', code);
setJoining(true); setJoining(true);
setTimeout(() => { joinGroup(id, code)
setJoining(false); .then(({ status }) => {
}, 500); if (status === 'success') {
window.location.href = '/groups/' + id;
} }
}, [code]); })
.finally(() => setJoining(false));
}
}, [code, group?.id]);
const buttonEnabled = code.length > 0 && !joining; const buttonEnabled = code.length > 0 && !joining;
@ -25,9 +48,18 @@ function GroupJoiner() {
<h1>Join Group</h1> <h1>Join Group</h1>
Code Code
<UITextInput value={code} onChangeText={setCode} /> <UITextInput value={code} onChangeText={setCode} />
<UIButton onClick={join} style={!buttonEnabled ? { color: 'grey' } : {}}> {group && (
<>
<br />
<span>Found group: {group.name}</span>
<UIButton
onClick={join}
style={!buttonEnabled ? { color: 'grey' } : {}}
>
{joining ? 'Joining' : 'Join'} {joining ? 'Joining' : 'Join'}
</UIButton> </UIButton>
</>
)}
</UISecondaryBox> </UISecondaryBox>
); );
} }

View File

@ -1,4 +1,5 @@
import { IEventSignup } from './Event'; import { IEventSignup } from './Event';
import { GroupPreview } from './GroupJoinerLink';
async function post(path: string, data: any) { async function post(path: string, data: any) {
const res = await fetch('http://localhost:5000/api' + path, { const res = await fetch('http://localhost:5000/api' + path, {
@ -151,3 +152,14 @@ export async function denyInvite(carpoolId: number, userId: number) {
export async function getMe() { export async function getMe() {
return await get('/users/@me'); return await get('/users/@me');
} }
export async function resolveCode(code: string): Promise<GroupPreview> {
return await get('/resolve_code/' + code);
}
export async function joinGroup(id: number, code: string) {
const result = await post('/groups/' + id + '/join', { code });
return {
status: result.status,
};
}