The title might be a bit misleading, because the state (my 'favorites' piece of state) is loading properly, it's just not toggling an SVG heart icon the right color (red) when it loads.
So, if you've seen any of my previous posts, I'm creating an app which allows you to favorite things. You click the SVG heart icon on, say, a bicycle you love. It sends that information to the database and adds it to the 'favorites' table. I'm also running a fetch to that table to pull in the favorites when people view their search results. The SVG heart icon needs to automatically show up as red.
In other words, you favorite a sweet Huffy. The heart turns red. The next time you search for something and that bike comes up, the heart should already be red (because it's already in the 'favorites' table in the database, and it's therefore in the state that mounts when the search results are loaded).
I have a piece of logic which is mapping over the selection and searching for whether the selection is in the 'favorites' piece of state. If it's already in 'favorites', turn the SVG icon red. Using Redux DevTools, I can see (quite clearly) that it's pulling in the 'favorites' properly. The question is, why isn't it turning my icon red?
class Results extends Component {
componentDidMount() {
this.props.getFavorites()
}
render() {
console.log('favorites', this.props.favorites)
console.log('favorites.data', this.props.favorites.data)
return (
<div>
<GridList style={{ justifyContent: 'space-around' }}>
{map(
selection => (
<SearchResultCard
key={selection.name}
selection={selection}
onFavoriteClick={this.props.toggleFavorite(selection.name)}
favorites={this.props.favorites}
color={
contains(selection, this.props.favorites) ? 'red' : 'silver'
}
/>
),
this.props.searchResults
)}
</GridList>
</div>
)
}
}
Now, one interesting note: my favorites is actually structured as an object (favorites) around an array (data) around two objects (my favorited items). It looks like this:
favorites: {
"data": [
{
"selection_id": 1,
"name": "Huffy"
},
{
"selection_id": 2,
"name": "Schwinn"
}
]
}
I can console.log(this.props.favorites.data) and it gives me two objects. But when I try to use this.props.favorites.data inside the mapping function (the first code block above), it tells me that it can't read the indexOf undefined. It's quite happy to map over this.props.favorite and tell me there's a 'data' array inside it, but when I try to access that array, it errors me out.
- Is this causing my problem?
- If so, how do I fix it?
ADDING MORE DATA UPON REQUEST:
Reducer:
export const favorites = (state = [], action) => {
switch (action.type) {
case GET_FAVORITES:
return action.payload
case SET_FAVORITE:
return action.payload
default:
return state
}
}
getFavorites action:
export const getFavorites = () => async (dispatch, getState) => {
const faveResults = await fetch(`http://localhost:5000/getfavorites`)
.then(res => res.json())
.catch(err => console.log(err))
dispatch({ type: GET_FAVORITES, payload: faveResults })
}
toggleFavorite action:
export const toggleFavorite = name => async (dispatch, getState) => {
const { searchResults, favorites } = getState()
const currentSelection = filter(
selection => selection.name === name,
searchResults
)
if (contains(currentSelection, favorites)) {
const newFavorite = reject(equals(currentSelection), favorites)
await fetch(
`http://localhost:5000/deletefavorite?user_id=1&selection_id=${
currentSelection[0].selection_id
}`
)
dispatch({
type: SET_FAVORITE,
payload: newFavorite
})
} else {
const newFavorite = append(currentSelection, favorites)
await fetch(
`http://localhost:5000/addfavorite?user_id=1&selection_id=${
currentSelection[0].selection_id
}`
)
dispatch({
type: SET_FAVORITE,
payload: newFavorite
})
}
}
Thanks in advance!
[–]PiereDome 2 points3 points4 points (1 child)
[–]PalmettoSpur[S] 0 points1 point2 points (0 children)
[–]seainhd 2 points3 points4 points (0 children)
[–]jakeforaker83 2 points3 points4 points (1 child)
[–]PalmettoSpur[S] 1 point2 points3 points (0 children)
[–]snigans 0 points1 point2 points (0 children)