-
Notifications
You must be signed in to change notification settings - Fork 14
/
Slider.js
91 lines (75 loc) · 2.65 KB
/
Slider.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
import React, { useEffect, useState } from "react";
import { Col, Row } from "shards-react";
import MediumCard from "./MediumCard";
import sortAndSetCategory from "../utils/SortAndSetCategeory";
// wrapper for items
function Slider() {
const [itemRows, setItemRows] = useState([]);
const [loading, setLoading] = useState(false);
const mediumURL =
"https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/@sabesan96";
useEffect(() => {
fetch(mediumURL)
.then((res) => res.json())
.then((data) => {
const {
feed: { image, link },
items,
} = data || {};
const posts = items.filter((item) => item.categories.length > 0);
const tagArrays = posts.map((item) => item.categories);
const allTags = tagArrays.flat();
const sortedTagsArray = sortAndSetCategory(allTags) || [];
// console.log(sortedTagsArray);
const tagArticle = [];
let removedBlogs = posts;
for (let i = 0; i < sortedTagsArray.length; i += 1) {
const blogsWithTag = removedBlogs.filter((blog) =>
blog.categories.includes(sortedTagsArray[i])
); // filter
removedBlogs = removedBlogs.filter(
(blog) => blogsWithTag.indexOf(blog) === -1
); // exclude
if (blogsWithTag.length > 0) {
blogsWithTag.forEach((item) => {
item.tag = sortedTagsArray[i];
tagArticle.push(item);
});
}
}
const filteredTagArrays = tagArticle.map((item) => item.tag);
const filteredSortedTagsArray =
sortAndSetCategory(filteredTagArrays) || [];
tagArticle.forEach((item) => {
item.tagNo = filteredSortedTagsArray.indexOf(item.tag) + 1;
item.avatar = image; // push avatar inside the json
item.profileLink = link; // push profile link inside the JSON
});
const tagArticleWithRow = [];
tagArticle.forEach((item, i) => {
const row = Math.floor(i / 3);
if (!tagArticleWithRow[row]) tagArticleWithRow[row] = [];
tagArticleWithRow[row].push(item);
});
setItemRows(tagArticleWithRow);
setLoading(true);
});
}, []);
// eslint-disable jsx-props-no-spreading
return (
<div className="blog__slider">
{loading &&
itemRows.length > 0 &&
itemRows.map((row) => (
<Row>
{row.map((item) => (
<Col lg="4" md="6" sm="12" className="mb-4">
<MediumCard {...item} />
</Col>
))}
</Row>
))}
</div>
);
}
export default Slider;