-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathphotos.slice.js
More file actions
47 lines (39 loc) · 1.69 KB
/
Copy pathphotos.slice.js
File metadata and controls
47 lines (39 loc) · 1.69 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
import { createSlice } from '@reduxjs/toolkit';
import { selectSearchTerm } from '../search/search.slice';
import photos from './photos.data.js';
const initialState = {
photos,
};
const options = {
name: 'photos',
initialState,
reducers: {
// Task 1: Create an `addPhoto()` case reducer that adds a photo to state.photos.
// Task 1 Hint: You can use state.photos.unshift()
// `unshift()` documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
addPhoto: (state, action) => {
state.photos.unshift({ id: state.photos.length + 1, caption: action.payload.caption, imageUrl: action.payload.imageUrl });
},
// Task 6: Create an `removePhoto()` case reducer that removes a photo from state.photos
// Task 6 Hint: You can use state.photos.splice()
// `splice()` documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
removePhoto: (state, action) => {
state.photos.splice(
state.photos.findIndex((photo) => photo.id === action.payload),
1
);
}
},
};
const photosSlice = createSlice(options);
export const { addPhoto, removePhoto } = photosSlice.actions;
export default photosSlice.reducer;
export const selectAllPhotos = (state) => state.photos.photos;
export const selectFilteredPhotos = (state) => {
// Task 12: Complete `selectFilteredPhotos()` selector to return a filtered list of photos whose captions match the user's search term
const photos = selectAllPhotos(state);
const searchTerm = selectSearchTerm(state);
return photos.filter((photo) =>
photo.caption.toLowerCase().includes(searchTerm.toLowerCase())
);
};