-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfilePagePostCard.jsx
202 lines (184 loc) · 5.83 KB
/
ProfilePagePostCard.jsx
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import React from 'react';
import styles from '../css/profilePagePostCard.module.css';
import Image from 'next/image';
import profilePlaceholder from '../images/profilePlaceholder.png';
import deleteAPost from '../hooks/deleteAPost';
import editAPost from '../hooks/editAPost';
import { useRouter } from 'next/router';
import { useState } from 'react';
import LanguageSelect from './LanguageSelect';
export default function ProfilePagePostCard({ props, userName }) {
const {
postId,
postTime,
postTitle,
programmingLanguage,
projectDescription,
weekDayAvailability,
dailyAvailability,
timeZone,
user,
photoURL,
} = props;
const date = new Date(postTime);
const readableDate = date.toLocaleDateString('en-GB');
const readableTime = date.toLocaleTimeString('en-GB').slice(0, 5);
const [isPostBeingEdited, setIsPostBeingEdited] = useState(false);
const [editProjectDescription, setEditProjectDescription] =
useState(projectDescription);
const [editProgrammingLanguage, setEditProgrammingLanguage] =
useState(programmingLanguage);
const [editWeekDayAvailability, setEditWeekDayAvailability] =
useState(weekDayAvailability);
const [editDailyAvailability, setEditDailyAvailability] =
useState(dailyAvailability);
const [editTimeZone, setEditTimeZone] = useState(timeZone);
const weeklyAvailability = [
'Weekdays (Mon-Fri)',
'Weekends (Sat-Sun)',
'Anytime',
'Flexible (varying availability)',
];
const timesOfDay = [
'Mornings (6am - 12pm)',
'Afternoons (12pm - 6pm)',
'Evenings (6pm - 12am)',
'Night (12am - 6am)',
'Anytime (24/7)',
'Flexible (varying availability)',
];
const router = useRouter();
function handleEditPost() {
setIsPostBeingEdited(true);
}
function handleCancelEditPost() {
setIsPostBeingEdited(false);
}
function handleUpdatePost() {
editAPost(
postId,
editProjectDescription,
editProgrammingLanguage,
editDailyAvailability,
editWeekDayAvailability,
editTimeZone
);
setIsPostBeingEdited(false);
setTimeout(() => {
router.push(`/users/${userName}`);
}, 1500);
}
function handleDeletePost() {
deleteAPost(postId);
setTimeout(() => {
router.push(`/users/${userName}`);
}, 1500);
}
function onChangeEditProjectDescription(event) {
setEditProjectDescription(event.target.value);
}
function OnChangeEditLanguage(event) {
setEditProgrammingLanguage(event.target.value);
}
function OnChangeTimeZone(event) {
setEditTimeZone(event.target.value);
}
return (
<div className={styles.profileCardContainer}>
<div className={styles.title}>{postTitle}</div>
<div className={styles.postInfo}>
{isPostBeingEdited ? (
<label>
Update programming language:
<LanguageSelect
selectedLanguages={editProgrammingLanguage}
setSelectedLanguages={setEditProgrammingLanguage}
/>
</label>
) : (
<div>Language: {editProgrammingLanguage}</div>
)}
<br />
{isPostBeingEdited ? (
<label>
Choose availability :
<select
value={weekDayAvailability}
onChange={(e) => setEditWeekDayAvailability(e.target.value)}
className="border p-2"
>
{weeklyAvailability.map((availabilityOption) => {
return (
<option key={availabilityOption}>{availabilityOption}</option>
);
})}
</select>
<select
className="border p-2"
value={dailyAvailability}
onChange={(e) => setEditDailyAvailability(e.target.value)}
>
{timesOfDay.map((availableTime) => {
return <option key={availableTime}>{availableTime}</option>;
})}
</select>
</label>
) : (
<div>
Availability: {dailyAvailability} {weekDayAvailability}
</div>
)}
<br />
{isPostBeingEdited ? (
<label>
Choose a time zone :
<select name="Time-zone" onChange={OnChangeTimeZone}>
<option value="GMT">GMT</option>
<option value="UTC">UTC</option>
<option value="PST">PST</option>
<option value="UTC">UTC</option>
<option value="ET">ET</option>
<option value="CT">CT</option>
<option value="PT">PT</option>
</select>
</label>
) : (
<div>Time zone:{editTimeZone}</div>
)}
{isPostBeingEdited ? <div>Enter new project description:</div> : null}
{isPostBeingEdited ? (
<textarea
className={styles.textarea}
name="edit-project-description"
onChange={onChangeEditProjectDescription}
placeholder={editProjectDescription}
></textarea>
) : (
<div>{projectDescription}</div>
)}
<div>
Created: {readableDate} at {readableTime}{' '}
</div>
<div className={styles.buttons}>
{isPostBeingEdited ? (
<button onClick={handleCancelEditPost} className={styles.button}>
Cancel Editing
</button>
) : ( user === userName &&
<button onClick={handleEditPost} className={styles.button}>
Edit Post
</button>
)}
{isPostBeingEdited ? (
<button onClick={handleUpdatePost} className={styles.button}>
Update Post
</button>
) : null}
{(user === userName && <button onClick={handleDeletePost} className={styles.button}>
Delete Post
</button>)}
</div>
</div>
</div>
);
}