hook up invitations api with notifications bar

This commit is contained in:
Michael Fatemi 2021-07-06 23:11:03 -04:00
parent 8e7b00c69f
commit 5a2088118f
5 changed files with 44 additions and 43 deletions

View File

@ -3,13 +3,14 @@ import { BrowserRouter, Route, Switch } from 'react-router-dom';
import AuthenticationContext from './Authentication/AuthenticationContext'; import AuthenticationContext from './Authentication/AuthenticationContext';
import WheelShare from './WheelShare'; import WheelShare from './WheelShare';
import WheelShareLoggedOut from './WheelShareLoggedOut'; import WheelShareLoggedOut from './WheelShareLoggedOut';
import { INotification } from './Notifications';
import Notifications from './Notifications'; import Notifications from './Notifications';
import { getReceivedInvitationsAndRequests } from './api';
import { IInvitation } from './types';
const Authenticator = lazy(() => import('./Authentication/Authenticator')); const Authenticator = lazy(() => import('./Authentication/Authenticator'));
const Group = lazy(() => import('./Group')); const Group = lazy(() => import('./Group'));
const dummyNotificationData: INotification[] = ([] = [ const dummyNotificationData: IInvitation[] = [
{ {
user: { user: {
id: 0, id: 0,
@ -20,7 +21,7 @@ const dummyNotificationData: INotification[] = ([] = [
name: 'Cross Country', name: 'Cross Country',
}, },
isRequest: true, isRequest: true,
sentTime: new Date(), sentTime: new Date().toISOString(),
}, },
{ {
user: { user: {
@ -32,15 +33,16 @@ const dummyNotificationData: INotification[] = ([] = [
name: 'TJ Lax', name: 'TJ Lax',
}, },
isRequest: false, isRequest: false,
sentTime: new Date(), sentTime: new Date().toISOString(),
}, },
]); ];
export default function App() { export default function App() {
const { user } = useContext(AuthenticationContext); const { user } = useContext(AuthenticationContext);
// eslint-disable-next-line
const [notifications, setNotifications] = useState(dummyNotificationData); const [notifications, setNotifications] = useState(dummyNotificationData);
useEffect(() => { useEffect(() => {
//getNotifications().then(setNotifications); getReceivedInvitationsAndRequests().then(setNotifications);
}, []); }, []);
return ( return (
<div style={{ padding: '1rem' }}> <div style={{ padding: '1rem' }}>

View File

@ -1,12 +1,12 @@
import { INotification } from './Notifications';
import { useCallback } from 'react'; import { useCallback } from 'react';
import { acceptInvite, acceptRequest, denyInvite, denyRequest } from './api'; import { acceptInvite, acceptRequest, denyInvite, denyRequest } from './api';
import { IInvitation } from './types';
import UIButton from './UIButton'; import UIButton from './UIButton';
export default function Notification({ export default function Notification({
notification, notification,
}: { }: {
notification: INotification; notification: IInvitation;
}) { }) {
const carpoolId = notification.carpool.id; const carpoolId = notification.carpool.id;
@ -26,6 +26,8 @@ export default function Notification({
denyInvite(carpoolId, notification.user.id); denyInvite(carpoolId, notification.user.id);
}, [carpoolId, notification.user.id]); }, [carpoolId, notification.user.id]);
const sentTime = new Date(notification.sentTime);
return ( return (
<div className="notification"> <div className="notification">
{notification.user.name} {notification.user.name}
@ -34,15 +36,7 @@ export default function Notification({
) : ( ) : (
<span> invited you to join </span> <span> invited you to join </span>
)} )}
{notification.carpool.name + {notification.carpool.name + sentTime.toLocaleString()}
' ' +
(notification.sentTime.getMonth() + 1) +
'/' +
notification.sentTime.getDate() +
'/' +
notification.sentTime.getFullYear() +
' ' +
notification.sentTime.toLocaleTimeString()}
{notification.isRequest ? ( {notification.isRequest ? (
<div className="notification-buttons" style={{ display: 'flex' }}> <div className="notification-buttons" style={{ display: 'flex' }}>
<UIButton onClick={acceptReq}>Accept</UIButton> <UIButton onClick={acceptReq}>Accept</UIButton>

View File

@ -1,22 +1,10 @@
import Notification from './Notification'; import Notification from './Notification';
import { IInvitation } from './types';
export type INotification = {
user: {
id: number;
name: string;
};
carpool: {
id: number;
name: string;
};
isRequest: boolean;
sentTime: Date;
};
export default function Notifications({ export default function Notifications({
notifications, notifications,
}: { }: {
notifications: INotification[]; notifications: IInvitation[];
}) { }) {
return ( return (
<div className="notifications"> <div className="notifications">

View File

@ -1,5 +1,6 @@
import { IEventSignup } from './Event'; import { IEventSignup } from './Event';
import { GroupPreview } from './GroupJoinerLink'; import { GroupPreview } from './GroupJoinerLink';
import { IInvitation } from './types';
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, {
@ -163,3 +164,17 @@ export async function joinGroup(id: number, code: string) {
status: result.status, status: result.status,
}; };
} }
export async function generateCode(groupId: number) {
return await post('/groups/' + groupId + '/generate_code', {});
}
export async function resetCode(groupId: number) {
return await post('/groups/' + groupId + '/reset_code', {});
}
export async function getReceivedInvitationsAndRequests() {
return (await get(
'/users/@me/received_requests_and_invites'
)) as IInvitation[];
}

View File

@ -8,17 +8,6 @@ export type IUser = {
name: string | null; name: string | null;
}; };
/**
* Model Invitation
*/
export type IInvitation = {
userId: number;
carpoolId: number;
isRequest: boolean;
sentTime: Date;
};
/** /**
* Model Carpool * Model Carpool
*/ */
@ -75,3 +64,16 @@ export type IEventSignup = {
latitude: number | null; latitude: number | null;
longitude: number | null; longitude: number | null;
}; };
export type IInvitation = {
user: {
id: number;
name: string;
};
carpool: {
id: number;
name: string;
};
sentTime: string;
isRequest: boolean;
};