forked from FindFirst-Development/FindFirst-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookmarkCard.tsx
More file actions
303 lines (280 loc) · 8.01 KB
/
BookmarkCard.tsx
File metadata and controls
303 lines (280 loc) · 8.01 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { ReactNode, RefObject, useEffect, useRef, useState } from "react";
import { Card } from "react-bootstrap";
import { useTagsDispatch } from "@/contexts/TagContext";
import Bookmark from "@/types/Bookmarks/Bookmark";
import TagAction from "@/types/Bookmarks/TagAction";
import DeleteModal from "./DeleteModal";
import style from "./bookmarkCard.module.scss";
import { useBookmarkDispatch } from "@/contexts/BookmarkContext";
import BookmarkAction from "@/types/Bookmarks/BookmarkAction";
import Tag from "@/types/Bookmarks/Tag";
import api from "@/api/Api";
import CardBody from "./CardBody";
import { SERVER_URL } from "@type/global";
import TagInput from "./TagInput";
const imgApi = SERVER_URL + "/api/screenshots/";
interface BookmarkProp {
bookmark: Readonly<Bookmark>;
}
/**
* function to add a Tag to a Bookmark
* @param bookmark bookmark
* @param tag string title of tag
* @returns Promise<TagAction> populates bookmark with the tags.
*/
async function addTagToBookmark(
bookmark: Bookmark,
tag: string
): Promise<TagAction> {
let action: TagAction = {
type: "add",
id: -1,
title: "",
bookmark: bookmark,
};
await api.addBookmarkTag(bookmark?.id, tag).then((response) => {
// It will always be the last index since it was the last added.
// let index = response.data.length - 1;
action.id = response.data.id;
action.title = response.data.title;
bookmark.tags.push({ id: action.id, title: action.title });
});
return action;
}
function OverlayCard({
url,
currentBookmark,
bookmark,
inEditMode,
edit,
changeEditMode,
}: {
url: string;
currentBookmark: RefObject<Bookmark>;
bookmark: Bookmark;
inEditMode: boolean;
edit: RefObject<Bookmark>;
changeEditMode: () => void;
}): ReactNode {
return (
<Card className={style.bookmarkCard}>
<div className="row g-0">
<div className="col-6 col-sm-12">
<Card.Img
className={`${style.cImg}`}
src={url + bookmark.screenshotUrl}
alt="screenshot preview"
></Card.Img>
</div>
<div className="col-6 col-sm-12">
<PlainCard
changeEditMode={changeEditMode}
bookmark={bookmark}
currentBookmark={currentBookmark}
inEditMode={inEditMode}
edit={edit}
></PlainCard>
</div>
</div>
</Card>
);
}
function PlainCard({
currentBookmark,
bookmark,
inEditMode,
edit,
changeEditMode,
}: {
currentBookmark: RefObject<Bookmark>;
bookmark: Bookmark;
inEditMode: boolean;
edit: RefObject<Bookmark>;
changeEditMode: () => void;
}): ReactNode {
return (
<CardBody
bookmark={currentBookmark.current}
highlight={bookmark.textHighlight}
inEditMode={inEditMode}
edit={edit}
changeEditMode={changeEditMode}
/>
);
}
export default function BookmarkCard({ bookmark }: Readonly<BookmarkProp>) {
const dispatch = useTagsDispatch();
const bkmkDispatch = useBookmarkDispatch();
const [input, setInput] = useState("");
const [inEditMode, setInEditMode] = useState(false);
const [strTags, setStrTags] = useState<string[]>([]);
const [show, setShow] = useState(false);
/*
* Create copies to compare state, its technically shallow but I have no nested properties
* that are edited in the partial update. For example Tags would be shallow copied.
* Thus set the before and after, initially they are the same.
*/
const currentBookmark = useRef<Bookmark>({ ...bookmark });
const edit = useRef<Bookmark>({ ...bookmark });
// Set tags on the card from the bookmark json object.
useEffect(() => {
if (bookmark) {
const tagList: string[] = [];
bookmark.tags.forEach((tag: Tag) => {
tagList.push(tag.title);
});
setStrTags(tagList);
}
}, [bookmark]);
const handleClose = () => {
setShow(false);
};
const handleShow = () => setShow(true);
const handleEdits = (inEditMode: boolean) => {
if (!inEditMode && isChanges(currentBookmark, edit)) {
sendPatch(edit.current);
currentBookmark.current = { ...edit.current };
}
};
const sendPatch = (edit: Bookmark) => {
api.updateBookmark({
id: edit.id,
title: edit.title,
url: edit.url,
isScrapable: edit.scrapable,
});
};
const isChanges = (
beforeEdit: RefObject<Bookmark>,
edit: RefObject<Bookmark>
) => {
return JSON.stringify(beforeEdit.current) != JSON.stringify(edit.current);
};
/**
* Decrement all the tags associated to this bookmark
* then remove the bookmark itself.
* Remove this from the inverse list of tags -> bookmarks when that map is created
*
* Consider creating a typescript class to act a handler for
* the state of bookmarks.
*/
function deleteBkmk() {
let action: BookmarkAction = {
type: "delete",
bookmarkId: bookmark.id,
bookmarks: [],
};
// delete the bookmark.
bkmkDispatch(action);
// decrement the bookmark counters
bookmark.tags.forEach((tag) => {
const idx = getIdxFromTitle(tag.title);
const tagId = bookmark.tags[idx].id;
// update the sidebar.
let action: TagAction = {
type: "delete",
id: tagId,
title: "",
bookmark,
};
dispatch(action);
});
}
const onDeleteTag = (idx: number) => {
const tagId = bookmark.tags[idx].id;
if (currentBookmark.current) {
currentBookmark.current.tags = currentBookmark.current.tags.filter(
(_t, i) => i !== idx
);
}
api.deleteTagById(bookmark.id, tagId);
let titles = currentBookmark.current.tags.map((t) => t.title); // just the titles display
setStrTags(titles);
// update the sidebar.
let action: TagAction = {
type: "delete",
id: tagId,
title: "",
bookmark,
};
dispatch(action);
};
const onPushTag = (tag: string) =>
addTagToBookmark(bookmark, tag).then((action) => {
dispatch(action);
setStrTags([...strTags, tag]);
currentBookmark.current.tags.push({ id: action.id, title: action.title });
});
function getIdxFromTitle(title: string): number {
return bookmark.tags.findIndex((t) => t.title == title);
}
const onChange = (e: any) => {
const { value } = e.target;
setInput(value);
};
function Content(): ReactNode {
return bookmark.screenshotUrl ? (
<OverlayCard
url={imgApi}
changeEditMode={changeEditMode}
bookmark={bookmark}
currentBookmark={currentBookmark}
inEditMode={inEditMode}
edit={edit}
/>
) : (
<PlainCard
changeEditMode={changeEditMode}
bookmark={bookmark}
currentBookmark={currentBookmark}
inEditMode={inEditMode}
edit={edit}
></PlainCard>
);
}
function changeEditMode() {
setInEditMode(!inEditMode);
handleEdits(!inEditMode);
}
return (
<div
data-testid={`bookmark-${bookmark.title}`}
className={`${style.cardRoot}`}
>
<div className={`card ${style.bookmarkCard}`}>
<div className={style.cardHeader}>
<button
type="button"
aria-label="Close"
className={`${style.deleteBookmarkIcon} btn-close`}
onClick={handleShow}
data-testid={`bk-id-${bookmark.id}-deleteBtn`}
/>
<button
onClick={changeEditMode}
className={`btn ${style.editBookmarkIcon}`}
data-testid={`${bookmark.id}-edit-btn`}
>
<i className="bi bi-pen"></i>
</button>
</div>
<DeleteModal
show={show}
handleClose={handleClose}
deleteBkmk={deleteBkmk}
/>
<Content />
<div className={`card-footer ${style.cardFooter}`}>
<TagInput
tags={strTags}
inputValue={input}
setInputValue={setInput}
onDeleteTag={onDeleteTag}
onPushTag={onPushTag}
testIdPrefix={`bk-${bookmark.id}-`}
></TagInput>
</div>
</div>
</div>
);
}