mirror of
https://github.com/myfatemi04/wheelshare-frontend.git
synced 2025-04-16 00:50:18 -04:00
clean up events code
This commit is contained in:
parent
a37323fc07
commit
25e7c08605
|
@ -1,6 +1,4 @@
|
||||||
const domain = process.env.REACT_APP_API_DOMAIN;
|
import { domain } from '../api';
|
||||||
|
|
||||||
console.log({ domain });
|
|
||||||
|
|
||||||
export async function createSession(code: string, redirectUrl: string) {
|
export async function createSession(code: string, redirectUrl: string) {
|
||||||
const res = await fetch(domain + 'create_session', {
|
const res = await fetch(domain + 'create_session', {
|
||||||
|
|
|
@ -1,14 +1,9 @@
|
||||||
import { useCallback, useEffect } from 'react';
|
import { useCallback, useEffect } from 'react';
|
||||||
import { green, lightgrey } from '../../lib/colors';
|
import { green, lightgrey } from '../../lib/colors';
|
||||||
import getPlaceDetails from '../../lib/getPlaceDetails';
|
import getPlaceDetails from '../../lib/getPlaceDetails';
|
||||||
import {
|
import { addOrUpdateEventSignup, getEvent, removeEventSignup } from '../api';
|
||||||
addOrUpdateEventSignup,
|
|
||||||
getEvent,
|
|
||||||
getEventSignups,
|
|
||||||
removeEventSignup,
|
|
||||||
} from '../api';
|
|
||||||
import { useMe } from '../hooks';
|
import { useMe } from '../hooks';
|
||||||
import { IEvent, IEventSignup } from '../types';
|
import { IEvent } from '../types';
|
||||||
import UIButton from '../UI/UIButton';
|
import UIButton from '../UI/UIButton';
|
||||||
import UILink from '../UI/UILink';
|
import UILink from '../UI/UILink';
|
||||||
import UIPlacesAutocomplete from '../UI/UIPlacesAutocomplete';
|
import UIPlacesAutocomplete from '../UI/UIPlacesAutocomplete';
|
||||||
|
@ -24,8 +19,6 @@ function GroupName({ group }: { group: IEvent['group'] }) {
|
||||||
return <UILink href={`/groups/${group.id}`}>{group.name}</UILink>;
|
return <UILink href={`/groups/${group.id}`}>{group.name}</UILink>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const NOT_LOADED = {};
|
|
||||||
|
|
||||||
export default function Event({
|
export default function Event({
|
||||||
id,
|
id,
|
||||||
initial,
|
initial,
|
||||||
|
@ -33,13 +26,14 @@ export default function Event({
|
||||||
id: number;
|
id: number;
|
||||||
initial?: IEvent;
|
initial?: IEvent;
|
||||||
}) {
|
}) {
|
||||||
const [event, setEvent] = useImmutable<IEvent | null>({
|
const [event, setEvent] = useImmutable<IEvent>({
|
||||||
id,
|
id,
|
||||||
name: '',
|
name: '',
|
||||||
group: {
|
group: {
|
||||||
id: 0,
|
id: 0,
|
||||||
name: '',
|
name: '',
|
||||||
},
|
},
|
||||||
|
signups: {},
|
||||||
carpools: [],
|
carpools: [],
|
||||||
startTime: '',
|
startTime: '',
|
||||||
endTime: '',
|
endTime: '',
|
||||||
|
@ -51,8 +45,6 @@ export default function Event({
|
||||||
duration: 0,
|
duration: 0,
|
||||||
...(initial || {}),
|
...(initial || {}),
|
||||||
});
|
});
|
||||||
const [signups, setSignups] =
|
|
||||||
useImmutable<Record<string, IEventSignup>>(NOT_LOADED);
|
|
||||||
|
|
||||||
const me = useMe()!;
|
const me = useMe()!;
|
||||||
|
|
||||||
|
@ -71,13 +63,13 @@ export default function Event({
|
||||||
if (placeId) {
|
if (placeId) {
|
||||||
const details = await getPlaceDetails(placeId);
|
const details = await getPlaceDetails(placeId);
|
||||||
|
|
||||||
signups[me.id] = {
|
event.signups[me.id] = {
|
||||||
user: { id: me.id, name: me.name },
|
user: { id: me.id, name: me.name },
|
||||||
placeId,
|
placeId,
|
||||||
...details,
|
...details,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
signups[me.id] = {
|
event.signups[me.id] = {
|
||||||
user: { id: me.id, name: me.name },
|
user: { id: me.id, name: me.name },
|
||||||
placeId: null,
|
placeId: null,
|
||||||
latitude: null,
|
latitude: null,
|
||||||
|
@ -86,30 +78,18 @@ export default function Event({
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[id, me.id, me.name, signups]
|
[event.signups, id, me.id, me.name]
|
||||||
);
|
);
|
||||||
|
|
||||||
const removeSignup = useCallback(async () => {
|
const removeSignup = useCallback(async () => {
|
||||||
await removeEventSignup(id);
|
await removeEventSignup(id);
|
||||||
|
|
||||||
if (signups[me.id]) {
|
if (event.signups[me.id]) {
|
||||||
delete signups[me.id];
|
delete event.signups[me.id];
|
||||||
}
|
}
|
||||||
}, [id, me.id, signups]);
|
}, [id, me.id, event.signups]);
|
||||||
|
|
||||||
const interested = !!signups[me.id];
|
const interested = !!event.signups[me.id];
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
getEventSignups(id)
|
|
||||||
.then((signups) => {
|
|
||||||
const signupMap: Record<string, IEventSignup> = {};
|
|
||||||
for (let signup of signups) {
|
|
||||||
signupMap[signup.user.id] = signup;
|
|
||||||
}
|
|
||||||
setSignups(signupMap);
|
|
||||||
})
|
|
||||||
.catch(console.error);
|
|
||||||
}, [id, setSignups]);
|
|
||||||
|
|
||||||
if (!event) {
|
if (!event) {
|
||||||
return <UISecondaryBox>Loading...</UISecondaryBox>;
|
return <UISecondaryBox>Loading...</UISecondaryBox>;
|
||||||
|
@ -124,7 +104,6 @@ export default function Event({
|
||||||
refresh,
|
refresh,
|
||||||
default: false,
|
default: false,
|
||||||
tentativeInvites,
|
tentativeInvites,
|
||||||
signups,
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<UISecondaryBox>
|
<UISecondaryBox>
|
||||||
|
@ -151,15 +130,15 @@ export default function Event({
|
||||||
updateSignup(placeId);
|
updateSignup(placeId);
|
||||||
}}
|
}}
|
||||||
style={
|
style={
|
||||||
signups[me.id]?.placeId != null
|
event.signups[me.id]?.placeId != null
|
||||||
? { border: '2px solid ' + green }
|
? { border: '2px solid ' + green }
|
||||||
: {}
|
: {}
|
||||||
}
|
}
|
||||||
placeId={signups[me.id]?.placeId}
|
placeId={event.signups[me.id]?.placeId}
|
||||||
/>
|
/>
|
||||||
<br />
|
<br />
|
||||||
<EventCarpools />
|
<EventCarpools />
|
||||||
{signups !== null && <EventSignups />}
|
{event.signups !== null && <EventSignups />}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</UISecondaryBox>
|
</UISecondaryBox>
|
||||||
|
|
|
@ -12,10 +12,11 @@ import { useMe } from '../hooks';
|
||||||
import { IEvent } from '../types';
|
import { IEvent } from '../types';
|
||||||
import useOptimalPath from '../useOptimalPath';
|
import useOptimalPath from '../useOptimalPath';
|
||||||
import EventContext from './EventContext';
|
import EventContext from './EventContext';
|
||||||
import useMySignup from './useMySignup';
|
import { useMySignup } from './EventHooks';
|
||||||
|
|
||||||
function useMemberLocations(members: IEvent['carpools'][0]['members']) {
|
function useMemberLocations(members: IEvent['carpools'][0]['members']) {
|
||||||
const { signups } = useContext(EventContext);
|
const { event } = useContext(EventContext);
|
||||||
|
const signups = event.signups;
|
||||||
|
|
||||||
return useMemo(
|
return useMemo(
|
||||||
() =>
|
() =>
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { createContext } from 'react';
|
import { createContext } from 'react';
|
||||||
import { IEvent, IEventSignup } from '../types';
|
import { IEvent } from '../types';
|
||||||
|
|
||||||
const EventContext = createContext({
|
const EventContext = createContext({
|
||||||
refresh: () => {
|
refresh: () => {
|
||||||
|
@ -7,7 +7,6 @@ const EventContext = createContext({
|
||||||
},
|
},
|
||||||
event: null! as IEvent,
|
event: null! as IEvent,
|
||||||
default: true,
|
default: true,
|
||||||
signups: {} as Record<string, IEventSignup>,
|
|
||||||
tentativeInvites: {} as Record<number, boolean>,
|
tentativeInvites: {} as Record<number, boolean>,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,12 @@ import { useContext, useMemo } from 'react';
|
||||||
import { useMe } from '../hooks';
|
import { useMe } from '../hooks';
|
||||||
import EventContext from './EventContext';
|
import EventContext from './EventContext';
|
||||||
|
|
||||||
export default function useMySignup() {
|
export function useSignups() {
|
||||||
const { signups } = useContext(EventContext);
|
return useContext(EventContext).event.signups;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useMySignup() {
|
||||||
|
const signups = useSignups();
|
||||||
const me = useMe()!;
|
const me = useMe()!;
|
||||||
|
|
||||||
return useMemo(() => signups[me.id] ?? null, [signups, me.id]);
|
return useMemo(() => signups[me.id] ?? null, [signups, me.id]);
|
|
@ -7,7 +7,7 @@ import { IEventSignup } from '../types';
|
||||||
import EventCarpoolCreateButton from './EventCarpoolCreateButton';
|
import EventCarpoolCreateButton from './EventCarpoolCreateButton';
|
||||||
import EventContext from './EventContext';
|
import EventContext from './EventContext';
|
||||||
import pickLatLong from './pickLatLong';
|
import pickLatLong from './pickLatLong';
|
||||||
import useMySignup from './useMySignup';
|
import { useMySignup } from './EventHooks';
|
||||||
|
|
||||||
function EventSignup({ signup }: { signup: IEventSignup }) {
|
function EventSignup({ signup }: { signup: IEventSignup }) {
|
||||||
const { user } = signup;
|
const { user } = signup;
|
||||||
|
@ -79,7 +79,8 @@ function EventSignup({ signup }: { signup: IEventSignup }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function EventSignups() {
|
export default function EventSignups() {
|
||||||
const { event, signups } = useContext(EventContext);
|
const { event } = useContext(EventContext);
|
||||||
|
const signups = event.signups;
|
||||||
const carpools = event.carpools;
|
const carpools = event.carpools;
|
||||||
|
|
||||||
const signupsWithoutCarpool = useMemo(() => {
|
const signupsWithoutCarpool = useMemo(() => {
|
||||||
|
@ -92,6 +93,8 @@ export default function EventSignups() {
|
||||||
.map((id) => signups[id]);
|
.map((id) => signups[id]);
|
||||||
}, [signups, carpools]);
|
}, [signups, carpools]);
|
||||||
|
|
||||||
|
console.log(signups);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{ display: 'flex', flexDirection: 'column' }}>
|
<div style={{ display: 'flex', flexDirection: 'column' }}>
|
||||||
<h3 style={{ marginBlockEnd: '0' }}>People without a carpool</h3>
|
<h3 style={{ marginBlockEnd: '0' }}>People without a carpool</h3>
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import { GroupPreview } from './GroupJoinerLink';
|
import { GroupPreview } from './GroupJoinerLink';
|
||||||
import { IInvitation, IEventSignup, ICarpool, IEvent, IGroup } from './types';
|
import { IInvitation, IEventSignup, ICarpool, IEvent, IGroup } from './types';
|
||||||
|
|
||||||
const base = (() => {
|
export const domain =
|
||||||
const domain =
|
process.env.NODE_ENV === 'production'
|
||||||
process.env.NODE_ENV === 'production'
|
? process.env.REACT_APP_API_PROD
|
||||||
? process.env.REACT_APP_API_PROD
|
: process.env.REACT_APP_API_LOCAL;
|
||||||
: process.env.REACT_APP_API_LOCAL;
|
|
||||||
|
|
||||||
|
export const base = (() => {
|
||||||
if (domain?.endsWith('/')) {
|
if (domain?.endsWith('/')) {
|
||||||
return domain.slice(0, -1) + '/api';
|
return domain.slice(0, -1) + '/api';
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -71,6 +71,7 @@ export type IEvent = {
|
||||||
name: string;
|
name: string;
|
||||||
}[];
|
}[];
|
||||||
}[];
|
}[];
|
||||||
|
signups: Record<string, IEventSignup>;
|
||||||
startTime: string; // Datestring
|
startTime: string; // Datestring
|
||||||
duration: number;
|
duration: number;
|
||||||
endTime: string | null; // Datestring
|
endTime: string | null; // Datestring
|
||||||
|
|
Loading…
Reference in New Issue
Block a user