-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redux PR #26
base: nextjs-standard
Are you sure you want to change the base?
Redux PR #26
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Left a comment about trying to normalize the data structure instead of using an array :)
sockittome.js
Outdated
return state.concat([ | ||
{ | ||
roomId: ++lastId, | ||
socketUserMap: action.payload.user, | ||
playlist: [], | ||
queue: [], | ||
currentPlayingIndex: 0, | ||
currentPlayingTime: 0, | ||
isPlaying: false, | ||
} | ||
]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good use case for normalization due to arrays being tough to mutate / lookup values from.
let nextRoomId = 0;
export default function playlistReducer(state = {}, action) {
switch (action.type) {
case "CREATE_ROOM": {
const roomId = ++nextRoomId;
return {
...state,
[roomId]: {
roomId,
socketUserMap: action.payload.user,
playlist: [],
queue: [],
currentPlayingIndex: 0,
currentPlayingTime: 0,
isPlaying: false,
}
}
}
Normalization helps with both getting and setting of rooms (especially if we only ever are acting on / using 1!) see how updates become simpler:
case "PLAY_PRESSED": {
return {
...state, // keep all other keys the same
[action.payload.id]: {
...state[action.payload.id], // keep the rest of the room state the same
isPlaying: true
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The link says "normalizr" is not maintained anymore. That shouldn't matter though, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
sockittome.js
Outdated
...state, | ||
[action.payload.id]: { | ||
...state[action.payload.id], | ||
currentPlayingIndex: --songIndex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The currentPlayingIndex: --songIndex
doesn't seem right. Is it?
How do I grab the previous currentPlayingIndex
of that specific room?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, you can find my dispatches in the console when you are on the landing page (no need to get inside into a room). You will need to scroll up to the very top of the console though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case "PREV_PRESSED": {
const roomId = action.payload.id;
const room = state[roomId];
return {
...state,
[roomId]: {
...room,
currentPlayingIndex: room.currentPlayingIndex - 1 ?? 0, // Don't go below 0
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reducer pattern's main benefit is for computing the next state from both the input and the current state.
Actions can have empty payloads in certain cases because the type of action only requires the previous state to do the specific task.
case 'PLAY': {
return {
...state,
isPlaying: true
}
}
case 'PAUSE': {
return {
...state,
isPlaying: false
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just updated PREV_PRESSED
and NEXT_PRESSED
.
for PLAY_PRESSED
, shouldn't it be:
case "PLAY_PRESSED": {
const roomId = action.payload.id;
const room = state[roomId];
return {
...state,
[roomId]: {
...room,
isPlaying: true
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep!
No description provided.