I have two functions,
1- Add Song to favorite List:
I send an request to endpoint that's add a song to user favorite list, its add song based on song_id I passed in body request.
2- Remove Song from the favorite list:
as previous but remove song from a favorite list based on song_id.
So in Player Component, I have a heart Icon that's called functions it's when users click on it i call to add a song, else i call to remove the song.
All these stuff work without redux!
So I want to save song_id and bassed on it i want to add a checker if this song_id exist that's mean this song is in a favorite list before "heart icon will be is fulled" when user click on it I want to remove this song from favorite list So I call the second function To send a request to server and so on.
So i make an action/reducer for this case but i think it's not well.
UI "Player Component" - "without redux"
addToFavorite = async () => {
const {tunes, token, currentTrackIndex} = this.state;
this.setState({isFavorite: true});
let id = tunes[currentTrackIndex].id;
try {
let AuthStr = `Bearer ${token}`;
const headers = {
'Content-Type': 'application/json',
Authorization: AuthStr,
};
let response = await API.post(
'/btn_add_favourite',
{
id,
},
{
headers: headers,
},
);
if (response.data.status === 'success') {
alert('added');
} else {
alert('already exist');
}
} catch (err) {
this.setState({isFavorite: false});
console.log(err);
}
};
deleteFromFavorite = async () => {
const {tunes, token, isFavorite, currentTrackIndex} = this.state;
let id = tunes[currentTrackIndex].id;
console.log(tunes[currentTrackIndex]);
try {
let AuthStr = `Bearer ${token}`;
const headers = {
'Content-Type': 'application/json',
Authorization: AuthStr,
};
if (isFavorite) {
let response = await API.post(
'/favourite_delete',
{
tracks_id: id,
},
{
headers: headers,
},
);
if (response.status === 200) {
alert('song deleted from your favorite list');
this.setState({isFavorite: false});
}
console.log(response);
}
} catch (err) {
console.log(err);
}
};
<Button
onPress={() =>
this.state.isFavorite // Not using redux yet so by default false
? this.deleteFromFavorite()
: this.addToFavorite()
}
transparent
style={styles.btnTransparent}>
<Icon
style={styles.iconColor}
type="MaterialIcons"
name={this.state.isFavorite ? 'favorite' : 'favorite-border'}
/>
</Button>
Redux Stuff
Action/isFavoriteAction.js
import {ADD_TO_FAVORITE, REMOVE_FROM_FAVORITE} from './types';
export const addToFavoriteFunction = isFavorite => {
return {
type: ADD_TO_FAVORITE,
payload: isFavorite,
};
};
export const removeFromFavoriteFunction = isFavorite => {
return {
type: REMOVE_FROM_FAVORITE,
payload: isFavorite,
};
};
reducer/isFavorite.js
import {ADD_TO_FAVORITE, REMOVE_FROM_FAVORITE} from '../actions/types';
let initial_state = {
isFavorite: false,
};
const isFavoriteReducer = (state = initial_state, action) => {
switch (action.type) {
case ADD_TO_FAVORITE:
state = {
...state,
isFavorite: true,
};
break;
case REMOVE_FROM_FAVORITE:
state = {
...state,
isFavorite: false,
};
break;
default:
return state;
}
return state;
};
export default isFavoriteReducer;
See Question&Answers more detail:
os