-
Notifications
You must be signed in to change notification settings - Fork 0
/
postListingsSlice.js
166 lines (139 loc) · 5.18 KB
/
postListingsSlice.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
154
155
156
157
158
159
160
161
162
163
164
165
166
import { createSlice } from "@reduxjs/toolkit";
import { fetchSubTopPosts } from "../../redditAPI";
import { addPost } from "../postContent/postContentSlice";
export const initialState = {
staticDataLoaded: false,
subreddits: {},
listingsLoaded: false,
listings: {}
};
export const postListings = createSlice({
name: "postListings",
initialState,
reducers: {
addSubreddit(state, action) {
const { name } = action.payload;
state.subreddits[name] = { name: name, postsRetrieved: false };
},
changeStaticDataLoadedStatus(state, action) {
const { loaded } = action.payload;
state.staticDataLoaded = loaded;
},
changeSubRetrievedStatus(state, action) {
const { name, retrieved } = action.payload;
state.subreddits[name].postsRetrieved = retrieved;
},
addListing(state, action) {
const { name, path, includedSubs, postIds } = action.payload;
state.listings[name] = { name, path, includedSubs, postIds };
},
changeListingsLoadedStatus(state, action) {
const { loaded } = action.payload;
state.listingsLoaded = loaded;
},
updateListingPostIds(state, action) {
const { name, postIds } = action.payload;
state.listings[name].postIds = postIds;
},
},
});
export const { addSubreddit } = postListings.actions;
export const { changeStaticDataLoadedStatus } = postListings.actions;
export const { changeSubRetrievedStatus } = postListings.actions;
export const { addListing } = postListings.actions;
export const { changeListingsLoadedStatus } = postListings.actions;
export const { updateListingPostIds } = postListings.actions;
export const selectSubreddits = (state) => state.postListings.subreddits;
export const selectStaticDataLoadedStatus = (state) => state.postListings.staticDataLoaded;
export const selectListingsLoadedStatus = (state) => state.postListings.listingsLoaded;
export const selectAllListings = (state) => state.postListings.listings;
export const selectListing = (state, name) => state.postListings.listings[name];
const getPostCategory = (post, listings) => {
// Assumes each subreddit is in a single category
// If not, the first inclusive category will be assigned as *the* category
let postCategory = null;
for (const key in listings) {
if (key === "All") {
continue;
}
const category = listings[key];
if (category.includedSubs.includes(post.subreddit)) {
postCategory = category.name;
break;
}
}
return postCategory;
}
const fetchSubPostData = (sub) => async (dispatch, getState) => {
try {
const { data, successfulFetch } = await fetchSubTopPosts(sub, 'day');
const postsData = data.data.children.map(post => post.data);
const state = getState();
let listings = selectAllListings(state); // Used to categorise posts
postsData.forEach(post => {
const postSummary = {
author: post.author,
category: getPostCategory(post, listings),
commentCount: post.num_comments,
commentsPath: post.permalink,
id: post.id,
isSelfPost: post.is_self,
link: post.url,
selfText: post.selftext,
subreddit: post.subreddit,
thumbnail: post.thumbnail,
title: post.title,
upvotes: post.ups,
};
dispatch(addPost(postSummary));
});
dispatch(changeSubRetrievedStatus({ name: sub, retrieved: successfulFetch }));
return postsData.map(post => post.id);
} catch(e) {
throw(e);
}
};
export const generateOrderedPostIds = postIds => {
// postIds: array of arrays, each containing post ids for a given subreddit
// Alternately merge arrays of ids into a single array (feed)
// Arrays within array can be of unknown number and length
// E.g. [[1, 2, 3], [4, 5]] becomes [1, 4, 2, 5, 3]
const sourceArrays = [...postIds];
let maxLength = 0;
sourceArrays.forEach(a => {
maxLength = a.length > maxLength ? a.length : maxLength;
});
const orderedIds = [];
for (let i = 0; i < maxLength; i++) {
sourceArrays.forEach(arr => {
if (arr.length > i) {
orderedIds.push(arr[i]);
}
});
}
return orderedIds;
};
export const fetchListingsData = () => async (dispatch, getState) => {
// https://redux.js.org/usage/writing-logic-thunks
// https://redux.js.org/tutorials/essentials/part-5-async-logic
const state = getState();
const subreddits = selectSubreddits(state);
const subNames = Object.keys(subreddits);
// Make fewer requests during development to avoid rate limit
// const twoSubs = subNames.slice(2, 4);
// Dispatch sub post requests asynchronously; wait for all to resolve or reject
// Use `twoSubs` instead of `subNames` during development
const settledPromises = await Promise.allSettled(subNames.map(sub => {
return dispatch(fetchSubPostData(sub));
}));
const subPostIds = [];
settledPromises.forEach(subPromise => {
if (subPromise.status === "fulfilled") {
subPostIds.push(subPromise.value);
}
});
const orderedFeedIds = generateOrderedPostIds(subPostIds);
dispatch(addListing({ name: "All", path: "", includedSubs: subNames, postIds: orderedFeedIds }));
dispatch(changeListingsLoadedStatus({ loaded: true }));
};
export default postListings.reducer;