aboutsummaryrefslogtreecommitdiff
path: root/src/components/EventCard/EventCard.tsx
blob: 2658fd669864d39747ab1922e401c11f68a98f32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import {
  Card,
  CardHeader,
  CardContent,
  CardActions,
  Button
} from '@material-ui/core';
import { Event } from '../../types';
import requests from '../../requests';
import { useAuth } from '../../hooks/useAuth';

interface PropTypes {
  event: Event;
  mutate: () => void;
}

const useStyles = makeStyles(theme => ({
  actions: {
    display: 'flex',
    justifyContent: 'space-between'
  }
}));

const EventCard: React.FC<PropTypes> = ({ event, mutate }) => {
  const classes = useStyles();
  const { user } = useAuth();
  const {
    data: {
      name,
      date,
      participants,
      conferenceId,
      attendanceId
    }
  } = event;

  const joined = participants.findIndex(x => x === user?.username) >= 0;

  const handleJoin = () => {
    if (user) {
      const update = { data: { participants: [...participants, user.username] }};
      return requests
        .patch(`/events/${event._id}`, update)
        .then(() => mutate());
    }
  };

  const handleRemove = () => {
    return requests
      .delete(`/events/${event._id}`)
      .then(() => mutate());
  };

  return (
    <Card variant="outlined">
      <CardHeader title={name || `Event #${event._id.slice(-4)}`} subheader={date}  />
      <CardContent>
        {conferenceId && <div> ConferenceID: {conferenceId} </div>}
        {attendanceId && <div> AttendanceID: {attendanceId} </div>}
        <div>
          Participants ({participants?.length || 0} / 3)
          <ul>
            {participants?.map(username => (
              <li> {username} {username === user?.username && <b>(you)</b>} </li>
            ))}
          </ul>
        </div>
      </CardContent>
      <CardActions className={classes.actions}>
        <Button
          onClick={handleRemove}
          color="primary"
          size="large"
        >
          Remove
        </Button>
        {!joined && (participants?.length || 0) < 3 && (
          <Button
            onClick={handleJoin}
            variant="contained"
            color="primary"
            size="large"
          >
            Join
          </Button>
        )}
        </CardActions>
    </Card>
  );
};

export default EventCard;