Connecting features #1
@@ -1,5 +1,6 @@
|
||||
import React from 'react';
|
||||
import './App.css';
|
||||
import redditSlice from './features/reddit/redditSlice';
|
||||
|
||||
function App() {
|
||||
return (
|
||||
|
||||
@@ -1,10 +1,49 @@
|
||||
import { createSlice } from "@reduxjs/toolkit";
|
||||
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
|
||||
|
||||
export const fetchBySub = createAsyncThunk(
|
||||
'reddit/fetchBySub',
|
||||
async(subreddit) => { // expects an argument corresponding to the url, in json format, of a given subreddit
|
||||
try {
|
||||
const myRequest = new Request(subreddit); // initializes request
|
||||
let response = await fetch(myRequest);
|
||||
let json = await response.json();
|
||||
let postsArray = json.data.children; // unpacks individual post objects from the subreddit JSON file, as an array
|
||||
return postsArray;
|
||||
} catch(e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export const postsSlice = createSlice({
|
||||
name: 'posts',
|
||||
initialState: {},
|
||||
reducers: {},
|
||||
extraReducers: {},
|
||||
initialState: {
|
||||
posts: [],
|
||||
requestsPending: false,
|
||||
requestDenied: false,
|
||||
},
|
||||
reducers: {
|
||||
filterPosts(state,action) { // Expects action.payload to be the searchterm imported from the state of searchBar
|
||||
state.posts.filter(post => (post.data.title !== action.payload) && (post.data.selftext !== action.payload));
|
||||
}
|
||||
},
|
||||
extraReducers: (builder) => {
|
||||
builder.addCase(fetchBySub.pending, (state,action) => {
|
||||
state.requestsPending = true;
|
||||
state.requestDenied = false;
|
||||
})
|
||||
builder.addCase(fetchBySub.rejected, (state,action) => {
|
||||
state.requestsPending = false;
|
||||
state.requestDenied = true;
|
||||
})
|
||||
builder.addCase(fetchBySub.fulfilled, (state,action) => {
|
||||
state.requestsPending = false;
|
||||
state.requestDenied = false;
|
||||
for (let sub in action.payload) { // iterates over postsArray to avoid
|
||||
state.posts.push(sub); // nesting arrays within the state's posts
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
export default postsSlice.reducer;
|
||||
Reference in New Issue
Block a user