Skip to content
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

Open
wants to merge 5 commits into
base: nextjs-standard
Choose a base branch
from
Open

Redux PR #26

wants to merge 5 commits into from

Conversation

sonnynomnom
Copy link
Member

No description provided.

@sonnynomnom sonnynomnom changed the base branch from main to nextjs-standard February 11, 2022 05:45
Copy link
Contributor

@codecaaron codecaaron left a 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
Comment on lines 257 to 267
return state.concat([
{
roomId: ++lastId,
socketUserMap: action.payload.user,
playlist: [],
queue: [],
currentPlayingIndex: 0,
currentPlayingTime: 0,
isPlaying: false,
}
]);
Copy link
Contributor

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
       }
   }
}

Copy link
Member Author

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?

Copy link
Member Author

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
Copy link
Member Author

@sonnynomnom sonnynomnom Apr 12, 2022

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?

Copy link
Member Author

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.

Copy link
Contributor

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
       }
    }
}

Copy link
Contributor

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
    }
}

Copy link
Member Author

@sonnynomnom sonnynomnom Apr 21, 2022

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
      }
   }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants