-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathsuggestion.slice.js
50 lines (43 loc) · 1.62 KB
/
suggestion.slice.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
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
export const fetchSuggestion =
createAsyncThunk(/* Task 15: Complete the `createAsyncThunk()` function to load a suggestion from this URL: http://localhost:3004/api/suggestion */
'suggestion/fetchSuggestion',
async (arg, thunkAPI) => {
const reponse = await fetch('http://localhost:3004/api/suggestion');
const { data } = await reponse.json();
console.log(data);
return data;
}
);
const initialState = {
suggestion: '',
loading: false,
error: true,
};
const options = {
name: 'suggestion',
initialState,
reducers: {},
extraReducers: {
/* Task 16: Inside `extraReducers`, add reducers to handle all three promise lifecycle states - pending, fulfilled, and rejected - for the `fetchSuggestion()` call */
[fetchSuggestion.pending]: (state) => {
state.loading = true;
state.error = false;
},
[fetchSuggestion.fulfilled]: (state, { payload: { imageUrl, caption} }) => {
state.suggestion = { imageUrl, caption };
state.loading = false;
state.error = false;
},
[fetchSuggestion.rejected]: (state, action) => {
state.loading = false;
state.error = true;
},
},
};
const suggestionSlice = createSlice(options);
export default suggestionSlice.reducer;
// Task 17: Create a selector, called `selectSuggestion`, for the `suggestion` state variable and export it from the file
export const selectSuggestion = (state) => state.suggestion.suggestion;
export const selectLoading = (state) => state.suggestion.loading;
export const selectError = (state) => state.suggestion.error;