diff --git a/src/components/NewUI/App.tsx b/src/components/NewUI/App.tsx index 821b2ab..889b3ba 100644 --- a/src/components/NewUI/App.tsx +++ b/src/components/NewUI/App.tsx @@ -1,52 +1,20 @@ -import { useEffect, useState } from 'react'; -import EventStream from './EventStream'; -import { IGroup } from './Group'; -import GroupCreator from './GroupCreator'; -import GroupList from './GroupList'; -import UIPrimaryTitle from './UIPrimaryTitle'; +import WheelShare from './WheelShare'; +import { BrowserRouter, Route, Switch } from 'react-router-dom'; +import { lazy, Suspense } from 'react'; + +const Group = lazy(() => import('./Group')); export default function App() { - const [groups, setGroups] = useState([]); - - useEffect(() => { - fetch('http://localhost:5000/api/groups') - .then((res) => res.json()) - .then(setGroups); - }, []); - return ( -
- WheelShare - -

Groups

- -

Events

- +
+ + + + + + + +
); } diff --git a/src/components/NewUI/EventCreator.tsx b/src/components/NewUI/EventCreator.tsx index fa9cf48..a00f1e4 100644 --- a/src/components/NewUI/EventCreator.tsx +++ b/src/components/NewUI/EventCreator.tsx @@ -1,42 +1,41 @@ import { useCallback, useState } from 'react'; +import { post } from './api'; +import { IGroup } from './Group'; import UIButton from './UIButton'; import UIDatetimeInput from './UIDatetimeInput'; import UIPlacesAutocomplete from './UIPlacesAutocomplete'; import UISecondaryBox from './UISecondaryBox'; import UITextInput from './UITextInput'; -export default function EventCreator() { +export default function EventCreator({ group }: { group: IGroup }) { const [name, setName] = useState(''); const [startTime, setStartTime] = useState(null); const [endTime, setEndTime] = useState(null); const [placeId, setPlaceId] = useState(null); - const [groupId, setGroupId] = useState(''); const createEvent = useCallback(() => { - fetch('http://localhost:5000/api/events', { - method: 'post', - body: JSON.stringify({ - name, - startTime, - endTime, - groupId, - placeId, - }), - headers: { - 'Content-Type': 'application/json', - }, - }); - }, [name, startTime, endTime, groupId, placeId]); + post('/events', { + name, + startTime, + endTime, + groupId: group.id, + placeId, + }) + .then((response) => response.json()) + .then(({ id }) => { + window.location.href = `/groups/${id}`; + }); + }, [name, startTime, endTime, group.id, placeId]); return ( -

Create Event

+

+ Create Event +

+

{group.name}

Name
- Group - -
Start time
diff --git a/src/components/NewUI/Group.tsx b/src/components/NewUI/Group.tsx index b04aedd..a04a72f 100644 --- a/src/components/NewUI/Group.tsx +++ b/src/components/NewUI/Group.tsx @@ -1,3 +1,6 @@ +import { useEffect, useState } from 'react'; +import { useParams } from 'react-router'; +import { Link } from 'react-router-dom'; import { IEvent } from './Event'; import EventCreator from './EventCreator'; import EventStream from './EventStream'; @@ -8,13 +11,51 @@ export type IGroup = { name: string; }; -export default function Group({ events, name }: IGroup) { +export default function Group() { + const { id } = useParams<{ id: string }>(); + const [loading, setLoading] = useState(true); + const [group, setGroup] = useState(null); + const [events, setEvents] = useState([]); + + useEffect(() => { + setLoading(true); + fetch('http://localhost:5000/api/groups/' + id) + .then((response) => response.json()) + .then(setGroup) + .finally(() => setLoading(false)); + + fetch('http://localhost:5000/api/groups/' + id + '/events') + .then((response) => response.json()) + .then(setEvents); + }, [id]); + + if (!group && !loading) { + return ( +
+

Group Not Found

+ Home +
+ ); + } + + if (!group) { + return null; + } + + const { name } = group; + return ( -
- +

{name}

+
); - // } diff --git a/src/components/NewUI/GroupCreator.tsx b/src/components/NewUI/GroupCreator.tsx index eeda386..dac9088 100644 --- a/src/components/NewUI/GroupCreator.tsx +++ b/src/components/NewUI/GroupCreator.tsx @@ -1,4 +1,5 @@ import React, { useCallback, useState } from 'react'; +import { post } from './api'; import UIButton from './UIButton'; import UISecondaryBox from './UISecondaryBox'; import UITextInput from './UITextInput'; @@ -6,14 +7,8 @@ import UITextInput from './UITextInput'; export default function GroupCreator() { const [name, setName] = useState(''); const createGroup = useCallback(() => { - fetch('http://localhost:5000/api/groups', { - method: 'post', - body: JSON.stringify({ - name, - }), - headers: { - 'Content-Type': 'application/json', - }, + post('/groups', { + name, }); }, [name]); diff --git a/src/components/NewUI/GroupList.tsx b/src/components/NewUI/GroupList.tsx index 1dc49ca..0e97ae8 100644 --- a/src/components/NewUI/GroupList.tsx +++ b/src/components/NewUI/GroupList.tsx @@ -4,7 +4,17 @@ import UISecondaryBox from './UISecondaryBox'; function GroupListItem({ group }: { group: IGroup }) { return ( -

{group.name}

+

{ + window.location.href = `/groups/${group.id}`; + }} + > + {group.name} +

); } diff --git a/src/components/NewUI/WheelShare.tsx b/src/components/NewUI/WheelShare.tsx new file mode 100644 index 0000000..8435c47 --- /dev/null +++ b/src/components/NewUI/WheelShare.tsx @@ -0,0 +1,52 @@ +import { useEffect, useState } from 'react'; +import EventStream from './EventStream'; +import { IGroup } from './Group'; +import GroupCreator from './GroupCreator'; +import GroupList from './GroupList'; +import UIPrimaryTitle from './UIPrimaryTitle'; + +export default function WheelShare() { + const [groups, setGroups] = useState([]); + + useEffect(() => { + fetch('http://localhost:5000/api/groups') + .then((res) => res.json()) + .then(setGroups); + }, []); + + return ( +
+ WheelShare + +

Groups

+ +

Events

+ +
+ ); +} diff --git a/src/components/NewUI/api.ts b/src/components/NewUI/api.ts new file mode 100644 index 0000000..81168bf --- /dev/null +++ b/src/components/NewUI/api.ts @@ -0,0 +1,9 @@ +export function post(path: string, data: any) { + return fetch('http://localhost:5000/api' + path, { + method: 'post', + body: JSON.stringify(data), + headers: { + 'Content-Type': 'application/json', + }, + }); +}