-
-
Notifications
You must be signed in to change notification settings - Fork 209
/
PinnedRecordingCard.tsx
200 lines (180 loc) · 5.46 KB
/
PinnedRecordingCard.tsx
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
import * as React from "react";
import { isEqual, isNil } from "lodash";
import MediaQuery from "react-responsive";
import { faEllipsisV, faThumbtack } from "@fortawesome/free-solid-svg-icons";
import { IconProp } from "@fortawesome/fontawesome-svg-core";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
preciseTimestamp,
getListenablePin,
pinnedRecordingToListen,
getArtistName,
getTrackName,
} from "../utils/utils";
import GlobalAppContext from "../utils/GlobalAppContext";
import Card from "../components/Card";
import ListenControl from "../listens/ListenControl";
import ListenCard from "../listens/ListenCard";
export const DEFAULT_COVER_ART_URL = "/static/img/default_cover_art.png";
export type PinnedRecordingCardProps = {
userName: string;
pinnedRecording: PinnedRecording;
currentFeedback?: ListenFeedBack | null;
// Only used when not passing a custom feedbackComponent
updateFeedbackCallback?: (
recordingMsid: string,
score: ListenFeedBack | RecommendationFeedBack
) => void;
className?: string;
isCurrentUser: Boolean;
removePinFromPinsList: (pin: PinnedRecording) => void;
newAlert: (
alertType: AlertType,
title: string,
message: string | JSX.Element
) => void;
};
type PinnedRecordingCardState = {
currentlyPinned?: Boolean;
isDeleted: Boolean;
};
export default class PinnedRecordingCard extends React.Component<
PinnedRecordingCardProps,
PinnedRecordingCardState
> {
static contextType = GlobalAppContext;
declare context: React.ContextType<typeof GlobalAppContext>;
constructor(props: PinnedRecordingCardProps) {
super(props);
this.state = {
currentlyPinned: this.determineIfCurrentlyPinned(),
isDeleted: false,
};
}
determineIfCurrentlyPinned = (): Boolean => {
const { pinnedRecording } = this.props;
const pinnedUntilTime: Date = new Date(pinnedRecording.pinned_until * 1000);
// invalid date
if (Number.isNaN(pinnedUntilTime.getTime())) {
return false;
}
const currentTime: Date = new Date();
if (currentTime < pinnedUntilTime) {
return true;
}
return false;
};
handleError = (error: string | Error, title?: string): void => {
const { newAlert } = this.props;
if (!error) {
return;
}
newAlert(
"danger",
title || "Error",
typeof error === "object" ? error.message : error
);
};
unpinRecording = async () => {
const { pinnedRecording, isCurrentUser, newAlert } = this.props;
const { APIService, currentUser } = this.context;
if (isCurrentUser && currentUser?.auth_token) {
try {
const status = await APIService.unpinRecording(currentUser.auth_token);
if (status === 200) {
this.setState({ currentlyPinned: false });
newAlert(
"success",
`You unpinned a recording.`,
`${getArtistName(pinnedRecording)} - ${getTrackName(
pinnedRecording
)}`
);
}
} catch (error) {
this.handleError(error, "Error while unpinning recording");
}
}
};
deletePin = async (pin: PinnedRecording) => {
const { isCurrentUser, removePinFromPinsList } = this.props;
const { APIService, currentUser } = this.context;
if (isCurrentUser && currentUser?.auth_token) {
try {
const status = await APIService.deletePin(
currentUser.auth_token,
pin.row_id
);
if (status === 200) {
this.setState({ currentlyPinned: false });
this.setState({ isDeleted: true });
// wait for the animation to finish
setTimeout(function removeListen() {
removePinFromPinsList(pin);
}, 1000);
}
} catch (error) {
this.handleError(error, "Error while deleting pin");
}
}
};
render() {
const {
pinnedRecording,
newAlert,
currentFeedback,
updateFeedbackCallback,
} = this.props;
const { currentlyPinned, isDeleted } = this.state;
const thumbnail = currentlyPinned ? (
<div className="pinned-recording-icon">
<span className="icon">
<FontAwesomeIcon icon={faThumbtack as IconProp} />
</span>
<span className="small">Pinned</span>
</div>
) : undefined;
const blurb = pinnedRecording.blurb_content ? (
<div className="blurb-content" title={pinnedRecording.blurb_content}>
"{pinnedRecording.blurb_content}"
</div>
) : undefined;
const additionalMenuItems = (
<>
{currentlyPinned && (
<ListenControl
title="Unpin"
text="Unpin"
action={() => this.unpinRecording()}
/>
)}
<ListenControl
title="Delete Pin"
text="Delete Pin"
action={() => this.deletePin(pinnedRecording)}
/>
</>
);
const cssClasses = ["pinned-recording-card"];
if (currentlyPinned) {
cssClasses.push("currently-pinned");
}
if (isDeleted) {
cssClasses.push("deleted");
}
return (
<ListenCard
className={cssClasses.join(" ")}
listen={pinnedRecordingToListen(pinnedRecording)}
currentFeedback={currentFeedback}
updateFeedbackCallback={updateFeedbackCallback}
showTimestamp
showUsername={false}
newAlert={newAlert}
additionalMenuItems={additionalMenuItems}
additionalContent={blurb}
thumbnail={thumbnail}
/>
);
}
}