mirror of
https://github.com/myfatemi04/wheelshare-frontend.git
synced 2025-04-21 19:29:51 -04:00
make list of people accurate
This commit is contained in:
parent
94b6c521ea
commit
cadcaa39cb
|
@ -1,6 +1,7 @@
|
||||||
import { useEffect, useRef, useState } from 'react';
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
import { addEventSignup, removeEventSignup } from './api';
|
import { addEventSignup, getEventSignups, removeEventSignup } from './api';
|
||||||
import { green, lightgrey } from './colors';
|
import { green, lightgrey } from './colors';
|
||||||
|
import { useMe } from './hooks';
|
||||||
import latlongdist, { R_miles } from './latlongdist';
|
import latlongdist, { R_miles } from './latlongdist';
|
||||||
import UIButton from './UIButton';
|
import UIButton from './UIButton';
|
||||||
import UIPlacesAutocomplete from './UIPlacesAutocomplete';
|
import UIPlacesAutocomplete from './UIPlacesAutocomplete';
|
||||||
|
@ -186,39 +187,39 @@ function Carpools({ event }: { event: IEvent }) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export type IPerson = {
|
export type IEventSignup = {
|
||||||
|
user: {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: number;
|
||||||
|
};
|
||||||
|
placeId: string;
|
||||||
|
formattedAddress: string;
|
||||||
latitude: number;
|
latitude: number;
|
||||||
longitude: number;
|
longitude: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
const dummyPeopleData: IPerson[] = [
|
function Signups({
|
||||||
{
|
event,
|
||||||
id: 0,
|
signups,
|
||||||
name: 'Rushil Umaretiya',
|
myPlaceId,
|
||||||
latitude: 11.1,
|
}: {
|
||||||
longitude: 10.09,
|
event: IEvent;
|
||||||
},
|
signups: IEventSignup[];
|
||||||
{
|
myPlaceId: string | null;
|
||||||
id: 1,
|
}) {
|
||||||
name: 'Nitin Kanchinadam',
|
|
||||||
latitude: 11.09,
|
|
||||||
longitude: 10.12,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
function People({ event, placeId }: { event: IEvent; placeId: string | null }) {
|
|
||||||
const PADDING = '1rem';
|
const PADDING = '1rem';
|
||||||
// eslint-disable-next-line
|
const placeDetails = usePlace(myPlaceId);
|
||||||
const [people, setPeople] = useState(dummyPeopleData);
|
|
||||||
const placeDetails = usePlace(placeId);
|
|
||||||
const locationLongitude = event.latitude;
|
const locationLongitude = event.latitude;
|
||||||
const locationLatitude = event.longitude;
|
const locationLatitude = event.longitude;
|
||||||
|
const me = useMe();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{ display: 'flex', flexDirection: 'column' }}>
|
<div style={{ display: 'flex', flexDirection: 'column' }}>
|
||||||
<h3 style={{ marginBlockEnd: '0' }}>People</h3>
|
<h3 style={{ marginBlockEnd: '0' }}>People</h3>
|
||||||
{people.map(({ name, latitude, longitude, id }) => {
|
{signups.map(({ latitude, longitude, user }) => {
|
||||||
|
if (user.id === me?.id) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
let extraDistance = null;
|
let extraDistance = null;
|
||||||
if (placeDetails != null) {
|
if (placeDetails != null) {
|
||||||
const myLatitude = placeDetails.latitude;
|
const myLatitude = placeDetails.latitude;
|
||||||
|
@ -260,8 +261,9 @@ function People({ event, placeId }: { event: IEvent; placeId: string | null }) {
|
||||||
marginTop: '0.5rem',
|
marginTop: '0.5rem',
|
||||||
marginBottom: '0.5rem',
|
marginBottom: '0.5rem',
|
||||||
}}
|
}}
|
||||||
|
key={user.id}
|
||||||
>
|
>
|
||||||
<b>{name}</b>
|
<b>{user.name}</b>
|
||||||
{extraDistance ? `: +${extraDistance.toFixed(1)} miles` : ''}
|
{extraDistance ? `: +${extraDistance.toFixed(1)} miles` : ''}
|
||||||
<div
|
<div
|
||||||
style={{
|
style={{
|
||||||
|
@ -287,44 +289,68 @@ export default function Event({ event }: { event: IEvent }) {
|
||||||
const { name, group, formattedAddress, startTime, endTime } = event;
|
const { name, group, formattedAddress, startTime, endTime } = event;
|
||||||
const [haveRide, toggleHaveRide] = useToggle(false);
|
const [haveRide, toggleHaveRide] = useToggle(false);
|
||||||
const [placeId, setPlaceId] = useState<string | null>(null);
|
const [placeId, setPlaceId] = useState<string | null>(null);
|
||||||
const [interested, toggleInterested] = useToggle(false);
|
const [interested, setInterested] = useState(false);
|
||||||
const [updating, setUpdating] = useState(false);
|
const [updating, setUpdating] = useState(false);
|
||||||
|
const [signups, setSignups] = useState<IEventSignup[]>([]);
|
||||||
|
const toggleInterested = useCallback(() => setInterested((i) => !i), []);
|
||||||
const toggleInterestedThrottled = useThrottle(toggleInterested, 500);
|
const toggleInterestedThrottled = useThrottle(toggleInterested, 500);
|
||||||
const existingSignup = useRef({
|
const existingSignup = useRef({
|
||||||
interested: false,
|
interested: false,
|
||||||
placeId: null as string | null,
|
placeId: null as string | null,
|
||||||
eventId: null as number | null,
|
eventId: null as number | null,
|
||||||
});
|
});
|
||||||
|
const me = useMe();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const prev = existingSignup.current;
|
const removeSignup = () => {
|
||||||
if (prev.interested === false && interested === false) {
|
if (prev.interested) {
|
||||||
return;
|
removeEventSignup(event.id)
|
||||||
}
|
.then(() => {
|
||||||
|
|
||||||
if (
|
|
||||||
(prev.interested === true && interested === false) ||
|
|
||||||
(interested === true && prev.placeId !== null && placeId === null)
|
|
||||||
) {
|
|
||||||
removeEventSignup(event.id).finally(() => setUpdating(false));
|
|
||||||
prev.interested = false;
|
prev.interested = false;
|
||||||
return;
|
})
|
||||||
|
.finally(() => setUpdating(false));
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (
|
const addSignup = () => {
|
||||||
interested === true &&
|
if (!prev.interested) {
|
||||||
(prev.placeId !== placeId || prev.eventId !== event.id) &&
|
addEventSignup(event.id, placeId!)
|
||||||
placeId !== null
|
.then(() => {
|
||||||
) {
|
|
||||||
prev.placeId = placeId;
|
prev.placeId = placeId;
|
||||||
prev.eventId = event.id;
|
prev.eventId = event.id;
|
||||||
prev.interested = true;
|
prev.interested = true;
|
||||||
|
})
|
||||||
|
.finally(() => setUpdating(false));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
addEventSignup(event.id, placeId!).finally(() => setUpdating(false));
|
const prev = existingSignup.current;
|
||||||
return;
|
|
||||||
|
if (!interested) {
|
||||||
|
removeSignup();
|
||||||
|
} else if (placeId == null) {
|
||||||
|
removeSignup();
|
||||||
|
} else {
|
||||||
|
addSignup();
|
||||||
}
|
}
|
||||||
}, [event.id, interested, placeId, updating]);
|
}, [event.id, interested, placeId, updating]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
getEventSignups(event.id)
|
||||||
|
.then((signups) => {
|
||||||
|
setSignups(signups);
|
||||||
|
for (let signup of signups) {
|
||||||
|
if (signup.user.id === me?.id) {
|
||||||
|
setInterested(true);
|
||||||
|
existingSignup.current.eventId = event.id;
|
||||||
|
existingSignup.current.placeId = signup.placeId;
|
||||||
|
existingSignup.current.interested = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(console.error);
|
||||||
|
}, [event.id, me?.id]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<UISecondaryBox>
|
<UISecondaryBox>
|
||||||
<UISecondaryHeader>{name}</UISecondaryHeader>
|
<UISecondaryHeader>{name}</UISecondaryHeader>
|
||||||
|
@ -373,7 +399,7 @@ export default function Event({ event }: { event: IEvent }) {
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<Carpools event={event} />
|
<Carpools event={event} />
|
||||||
<People event={event} placeId={placeId} />
|
<Signups event={event} myPlaceId={placeId} signups={signups} />
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</UISecondaryBox>
|
</UISecondaryBox>
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { IEventSignup } from './Event';
|
||||||
|
|
||||||
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, {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
|
@ -47,8 +49,14 @@ export async function getPlaceDetails(
|
||||||
return await get('/place/' + placeId);
|
return await get('/place/' + placeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getEventSignups(
|
||||||
|
eventId: number
|
||||||
|
): Promise<IEventSignup[]> {
|
||||||
|
return await get(`/events/${eventId}/signups`);
|
||||||
|
}
|
||||||
|
|
||||||
export async function addEventSignup(eventId: number, placeId: string) {
|
export async function addEventSignup(eventId: number, placeId: string) {
|
||||||
post(`/events/${eventId}/signup`, {
|
return await post(`/events/${eventId}/signup`, {
|
||||||
placeId,
|
placeId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
5
src/components/hooks.ts
Normal file
5
src/components/hooks.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import { useContext } from 'react';
|
||||||
|
import AuthenticationContext from './Authentication/AuthenticationContext';
|
||||||
|
|
||||||
|
export const useAuth = () => useContext(AuthenticationContext);
|
||||||
|
export const useMe = () => useAuth().user;
|
Loading…
Reference in New Issue
Block a user