-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
153 lines (138 loc) · 4.32 KB
/
index.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import React, { useEffect, useState } from 'react';
import { format, parseISO } from 'date-fns';
import { toast } from 'react-toastify';
import PropTypes from 'prop-types';
import Loader from 'react-loader-spinner';
import {
MdEdit,
MdDeleteForever,
MdDateRange,
MdLocationOn,
} from 'react-icons/md';
import nl2br from 'react-nl2br';
import api from '~/services/api';
import history from '~/services/history';
import { Container, Details, Subscriptions } from './styles';
export default function Meetup({ match }) {
const { id } = match.params;
const [loading, setLoading] = useState(true);
const [meetup, setMeetup] = useState(null);
useEffect(() => {
async function loadMeetup() {
try {
const response = await api.get(`organizing/${id}`);
setMeetup({
...response.data.meetup,
subscriptions: response.data.subscriptions,
formattedDate: format(
parseISO(response.data.meetup.date),
"dd/MM/Y - HH'h'mm"
),
});
setLoading(false);
} catch (err) {
toast.error('Meetup not found');
history.push('/');
}
}
loadMeetup();
}, [id]);
function handleEdit() {
history.push(`/meetups/${id}/edit`);
}
async function handleCancel() {
try {
await api.delete(`meetups/${id}`);
toast.success('Meetup canceled succesfully!');
history.push('/dashboard');
} catch (err) {
toast.error('Error canceling meetup, try again');
}
}
return (
<Container>
{loading ? (
<div className="loading">
<Loader type="TailSpin" color="#9a68ed" width={32} height={32} />
</div>
) : (
<Details>
<header>
<h1>{meetup.title}</h1>
{!meetup.past && (
<nav>
<button type="button" className="edit" onClick={handleEdit}>
<MdEdit size={16} color="#fff" />
Edit
</button>
<button type="button" className="cancel" onClick={handleCancel}>
<MdDeleteForever size={20} color="#fff" />
Cancel
</button>
</nav>
)}
</header>
<main>
<div className="image-wrapper">
<img src={meetup.banner.url} alt={meetup.title} />
</div>
<div className="wrapper">
<p>{nl2br(meetup.description)}</p>
<footer>
<div className="info">
<p>
<MdDateRange size={18} color="#9a68ed" />
{meetup.formattedDate}
</p>
<p>
<MdLocationOn size={18} color="#9a68ed" />
{meetup.location}
</p>
</div>
{meetup.subscriptions.length >= 1 && (
<Subscriptions>
<div>
{meetup.subscriptions.slice(0, 5).map((sub, index) => (
<img
style={{
zIndex: 10 - index,
}}
key={String(index)}
src={
sub.user.avatar
? sub.user.avatar.url
: `https://api.adorable.io/avatars/50/${sub.user.id}`
}
alt={sub.user.name}
/>
))}
{meetup.subscriptions.length > 5 && (
<div className="dots">
<div />
<div />
<div />
</div>
)}
</div>
<span>
<strong>{meetup.subscriptions.length}</strong>{' '}
subscription
{meetup.subscriptions.length > 1 && 's'}
</span>
</Subscriptions>
)}
</footer>
</div>
</main>
</Details>
)}
</Container>
);
}
Meetup.propTypes = {
match: PropTypes.shape({
params: PropTypes.shape({
id: PropTypes.string.isRequired,
}),
}).isRequired,
};