mirror of
https://github.com/myfatemi04/wheelshare-frontend.git
synced 2025-04-17 17:40:16 -04:00
add who is the creator of the carpool message
This commit is contained in:
parent
559fabccb7
commit
00b1c5d08b
|
@ -1,4 +1,4 @@
|
||||||
import { createContext, useCallback, useEffect } from 'react';
|
import { createContext, useCallback, useEffect, useMemo } from 'react';
|
||||||
import {
|
import {
|
||||||
acceptCarpoolRequest,
|
acceptCarpoolRequest,
|
||||||
cancelCarpoolInvite,
|
cancelCarpoolInvite,
|
||||||
|
@ -7,6 +7,7 @@ import {
|
||||||
leaveCarpool,
|
leaveCarpool,
|
||||||
sendCarpoolInvite,
|
sendCarpoolInvite,
|
||||||
} from '../api';
|
} from '../api';
|
||||||
|
import { useMe } from '../hooks';
|
||||||
import { ICarpool } from '../types';
|
import { ICarpool } from '../types';
|
||||||
import UILink from '../UI/UILink';
|
import UILink from '../UI/UILink';
|
||||||
import UISecondaryBox from '../UI/UISecondaryBox';
|
import UISecondaryBox from '../UI/UISecondaryBox';
|
||||||
|
@ -25,6 +26,7 @@ type CarpoolState = {
|
||||||
event: ICarpool['event'];
|
event: ICarpool['event'];
|
||||||
members: { id: number; name: string }[];
|
members: { id: number; name: string }[];
|
||||||
invitations: Record<number, ICarpool['invitations'][0]>;
|
invitations: Record<number, ICarpool['invitations'][0]>;
|
||||||
|
creatorId: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const CarpoolContext = createContext({
|
export const CarpoolContext = createContext({
|
||||||
|
@ -49,6 +51,12 @@ export const CarpoolContext = createContext({
|
||||||
export default function Carpool({ id }: { id: number }) {
|
export default function Carpool({ id }: { id: number }) {
|
||||||
const [carpool, setCarpool] = useImmutable<CarpoolState | null>(null);
|
const [carpool, setCarpool] = useImmutable<CarpoolState | null>(null);
|
||||||
|
|
||||||
|
const me = useMe();
|
||||||
|
const isCreator = useMemo(
|
||||||
|
() => carpool?.creatorId === me?.id,
|
||||||
|
[carpool?.creatorId, me?.id]
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getCarpool(id).then((carpool) => {
|
getCarpool(id).then((carpool) => {
|
||||||
const invitationsMap: Record<number, ICarpool['invitations'][0]> = {};
|
const invitationsMap: Record<number, ICarpool['invitations'][0]> = {};
|
||||||
|
@ -60,6 +68,7 @@ export default function Carpool({ id }: { id: number }) {
|
||||||
name: carpool.name,
|
name: carpool.name,
|
||||||
event: carpool.event,
|
event: carpool.event,
|
||||||
members: carpool.members,
|
members: carpool.members,
|
||||||
|
creatorId: carpool.creatorId,
|
||||||
invitations: invitationsMap,
|
invitations: invitationsMap,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -67,6 +76,10 @@ export default function Carpool({ id }: { id: number }) {
|
||||||
|
|
||||||
const acceptRequest = useCallback(
|
const acceptRequest = useCallback(
|
||||||
async (userId: number) => {
|
async (userId: number) => {
|
||||||
|
if (!isCreator) {
|
||||||
|
console.error('Trying to accept request as noncreator');
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!carpool) {
|
if (!carpool) {
|
||||||
console.error(
|
console.error(
|
||||||
'Trying to accept request when carpool has not been loaded.'
|
'Trying to accept request when carpool has not been loaded.'
|
||||||
|
@ -79,11 +92,15 @@ export default function Carpool({ id }: { id: number }) {
|
||||||
delete carpool.invitations[userId];
|
delete carpool.invitations[userId];
|
||||||
carpool.members.push({ id: userId, name });
|
carpool.members.push({ id: userId, name });
|
||||||
},
|
},
|
||||||
[carpool, id]
|
[carpool, id, isCreator]
|
||||||
);
|
);
|
||||||
|
|
||||||
const denyRequest = useCallback(
|
const denyRequest = useCallback(
|
||||||
async (userId: number) => {
|
async (userId: number) => {
|
||||||
|
if (!isCreator) {
|
||||||
|
console.error('Trying to deny request as noncreator');
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!carpool) {
|
if (!carpool) {
|
||||||
console.error(
|
console.error(
|
||||||
'Trying to deny request when carpool has not been loaded.'
|
'Trying to deny request when carpool has not been loaded.'
|
||||||
|
@ -93,11 +110,15 @@ export default function Carpool({ id }: { id: number }) {
|
||||||
await denyCarpoolRequest(id, userId);
|
await denyCarpoolRequest(id, userId);
|
||||||
delete carpool.invitations[userId];
|
delete carpool.invitations[userId];
|
||||||
},
|
},
|
||||||
[carpool, id]
|
[carpool, id, isCreator]
|
||||||
);
|
);
|
||||||
|
|
||||||
const sendInvite = useCallback(
|
const sendInvite = useCallback(
|
||||||
async (user: { id: number; name: string }) => {
|
async (user: { id: number; name: string }) => {
|
||||||
|
if (!isCreator) {
|
||||||
|
console.error('Trying to send invitation as noncreator');
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!carpool) {
|
if (!carpool) {
|
||||||
console.error(
|
console.error(
|
||||||
'Trying to send invite when carpool has not been loaded.'
|
'Trying to send invite when carpool has not been loaded.'
|
||||||
|
@ -111,11 +132,15 @@ export default function Carpool({ id }: { id: number }) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[carpool, id]
|
[carpool, id, isCreator]
|
||||||
);
|
);
|
||||||
|
|
||||||
const cancelInvite = useCallback(
|
const cancelInvite = useCallback(
|
||||||
async (user: { id: number; name: string }) => {
|
async (user: { id: number; name: string }) => {
|
||||||
|
if (!isCreator) {
|
||||||
|
console.error('Trying to cancel invitation as noncreator');
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!carpool) {
|
if (!carpool) {
|
||||||
console.error(
|
console.error(
|
||||||
'Trying to cancel invite when carpool has not been loaded.'
|
'Trying to cancel invite when carpool has not been loaded.'
|
||||||
|
@ -129,7 +154,12 @@ export default function Carpool({ id }: { id: number }) {
|
||||||
}
|
}
|
||||||
delete carpool.invitations[user.id];
|
delete carpool.invitations[user.id];
|
||||||
},
|
},
|
||||||
[carpool, id]
|
[carpool, id, isCreator]
|
||||||
|
);
|
||||||
|
|
||||||
|
const creatorName = useMemo(
|
||||||
|
() => carpool?.members.find((m) => m.id === carpool.creatorId)?.name,
|
||||||
|
[carpool]
|
||||||
);
|
);
|
||||||
|
|
||||||
const eventId = carpool?.event.id;
|
const eventId = carpool?.event.id;
|
||||||
|
@ -164,6 +194,9 @@ export default function Carpool({ id }: { id: number }) {
|
||||||
style={{ width: '45rem', maxWidth: '100vw', alignItems: 'center' }}
|
style={{ width: '45rem', maxWidth: '100vw', alignItems: 'center' }}
|
||||||
>
|
>
|
||||||
<h1>{carpool.name}</h1>
|
<h1>{carpool.name}</h1>
|
||||||
|
{isCreator
|
||||||
|
? 'You are the creator of this carpool.'
|
||||||
|
: `${creatorName} is the creator of this carpool`}
|
||||||
<UILink href={'/events/' + carpool.event.id}>
|
<UILink href={'/events/' + carpool.event.id}>
|
||||||
{carpool.event.name}
|
{carpool.event.name}
|
||||||
</UILink>
|
</UILink>
|
||||||
|
|
|
@ -6,6 +6,7 @@ import useToggle from '../useToggle';
|
||||||
import { CarpoolContext } from './Carpool';
|
import { CarpoolContext } from './Carpool';
|
||||||
import InvitationList from './InvitationList';
|
import InvitationList from './InvitationList';
|
||||||
import RequestList from './RequestList';
|
import RequestList from './RequestList';
|
||||||
|
import useIsCreator from './useIsCreator';
|
||||||
|
|
||||||
const spanStyle = {
|
const spanStyle = {
|
||||||
padding: '0.5rem',
|
padding: '0.5rem',
|
||||||
|
@ -19,6 +20,7 @@ export default function CarpoolTopButtonsMembersOnly() {
|
||||||
const [invitationsOpen, toggleInvitationsOpen] = useToggle(false);
|
const [invitationsOpen, toggleInvitationsOpen] = useToggle(false);
|
||||||
const [requestsOpen, toggleRequestsOpen] = useToggle(false);
|
const [requestsOpen, toggleRequestsOpen] = useToggle(false);
|
||||||
const { leave } = useContext(CarpoolContext);
|
const { leave } = useContext(CarpoolContext);
|
||||||
|
const isCreator = useIsCreator();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -29,12 +31,17 @@ export default function CarpoolTopButtonsMembersOnly() {
|
||||||
margin: '0.5rem 0',
|
margin: '0.5rem 0',
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<span style={spanStyle} onClick={toggleRequestsOpen}>
|
{isCreator && (
|
||||||
<MailOutlineIcon style={{ marginRight: '0.5rem' }} /> View requests
|
<>
|
||||||
</span>
|
<span style={spanStyle} onClick={toggleRequestsOpen}>
|
||||||
<span style={spanStyle} onClick={toggleInvitationsOpen}>
|
<MailOutlineIcon style={{ marginRight: '0.5rem' }} /> View
|
||||||
<PersonAddIcon style={{ marginRight: '0.5rem' }} /> Invite
|
requests
|
||||||
</span>
|
</span>
|
||||||
|
<span style={spanStyle} onClick={toggleInvitationsOpen}>
|
||||||
|
<PersonAddIcon style={{ marginRight: '0.5rem' }} /> Invite
|
||||||
|
</span>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
<span style={spanStyle} onClick={leave}>
|
<span style={spanStyle} onClick={leave}>
|
||||||
<EventBusyIcon style={{ marginRight: '0.5rem' }} /> Leave
|
<EventBusyIcon style={{ marginRight: '0.5rem' }} /> Leave
|
||||||
</span>
|
</span>
|
||||||
|
|
14
src/components/Carpool/useIsCreator.ts
Normal file
14
src/components/Carpool/useIsCreator.ts
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import { useContext, useDebugValue, useMemo } from 'react';
|
||||||
|
import { useMe } from '../hooks';
|
||||||
|
import { CarpoolContext } from './Carpool';
|
||||||
|
|
||||||
|
export default function useIsCreator() {
|
||||||
|
const me = useMe();
|
||||||
|
const carpool = useContext(CarpoolContext).carpool;
|
||||||
|
const isCreator = useMemo(
|
||||||
|
() => carpool?.creatorId === me?.id,
|
||||||
|
[carpool?.creatorId, me?.id]
|
||||||
|
);
|
||||||
|
useDebugValue(isCreator);
|
||||||
|
return isCreator;
|
||||||
|
}
|
|
@ -53,7 +53,7 @@ export default function Event({
|
||||||
tentativeInvites,
|
tentativeInvites,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<UISecondaryBox style={{ minWidth: '15rem' }}>
|
<UISecondaryBox style={{ width: '35rem' }}>
|
||||||
<div style={{ textAlign: 'center' }}>
|
<div style={{ textAlign: 'center' }}>
|
||||||
<UISecondaryHeader>{name}</UISecondaryHeader>
|
<UISecondaryHeader>{name}</UISecondaryHeader>
|
||||||
{group && <GroupName group={group} />}
|
{group && <GroupName group={group} />}
|
||||||
|
|
|
@ -29,6 +29,7 @@ export type ICarpool = {
|
||||||
// id: number;
|
// id: number;
|
||||||
// name: string;
|
// name: string;
|
||||||
// };
|
// };
|
||||||
|
creatorId: number;
|
||||||
members: {
|
members: {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user