function bookmarkReducer(state, action) {
  switch (action.type) {
    case 'SET_BOOKMARKS': {
      // Parse the bookmark category from the payload.
      const parsedCategory = action.payload.category.toLowerCase()

      // Create a new piece of bookmark data.
      const newBookmarkData = createBookmarkData(action.payload)

      // Update the bookmark group data.
      const updatedBookmarkGroup = updateBookmarkGroup(
        state[parsedCategory],
        newBookmarkData
      )
      return { ...state, [parsedCategory]: updatedBookmarkGroup }
    }

    case 'EDIT_BOOKMARK': {
      // Get access to the payload data.
      const { oldData, newData } = action.payload

      // Replace the existing bookmark data.
      const updatedBookmark = state[oldData.category.toLowerCase()].map(
        (bookmark) => {
          // When the existing bookmark data finds a match, begin the update process.
          if (bookmark.name === oldData.name) {
            return {
              ...bookmark,
              name: newData.name,
              link: newData.link ? newData.link : oldData.link,
            }
          }

          // Otherwise, return the existing bookmark data.
          return bookmark
        }
      )

      // Return the updated state.
      return { ...state, [oldData.category.toLowerCase()]: updatedBookmark }
    }

    case 'DELETE_BOOKMARK': {
      // Get the specific bookmark entry data.
      const capturedState =
        state[action.payload.bookmark.category.toLowerCase()]

      // Execute the delete operation.
      const updatedBookmark = capturedState.filter((bookmark) => {
        return bookmark.name !== action.payload.bookmark.name
      })

      // Return the updated state.
      return {
        ...state,
        [action.payload.bookmark.category.toLowerCase()]: updatedBookmark,
      }
    }

    case 'OPEN_DRAWER': {
      const updatedFlags = { ...state.flags, openDrawer: true }
      return { ...state, flags: updatedFlags }
    }
    case 'CLOSE_DRAWER': {
      const updatedFlags = { ...state.flags, openDrawer: false }
      return { ...state, flags: updatedFlags }
    }

    case 'OPEN_EDIT_DRAWER': {
      const updatedFlags = { ...state.flags, openEditDrawer: true }
      return { ...state, flags: updatedFlags }
    }
    case 'CLOSE_EDIT_DRAWER': {
      const updatedFlags = { ...state.flags, openEditDrawer: false }
      return { ...state, flags: updatedFlags }
    }

    case 'OPEN_EDIT_SUCCESS_SNACKBAR': {
      const updatedFlags = { ...state.flags, editSuccessSnackbar: true }
      return { ...state, flags: updatedFlags }
    }
    case 'CLOSE_EDIT_SUCCESS_SNACKBAR': {
      const updatedFlags = { ...state.flags, editSuccessSnackbar: false }
      return { ...state, flags: updatedFlags }
    }

    case 'OPEN_SUCCESS_SNACKBAR': {
      const updatedFlags = { ...state.flags, successSnackbar: true }
      return { ...state, flags: updatedFlags }
    }
    case 'CLOSE_SUCCESS_SNACKBAR': {
      const updatedFlags = { ...state.flags, successSnackbar: false }
      return { ...state, flags: updatedFlags }
    }

    case 'OPEN_ERROR_SNACKBAR': {
      const updatedFlags = { ...state.flags, errorSnackbar: true }
      return { ...state, flags: updatedFlags }
    }
    case 'CLOSE_ERROR_SNACKBAR': {
      const updatedFlags = { ...state.flags, errorSnackbar: false }
      return { ...state, flags: updatedFlags }
    }

    case 'OPEN_DELETE_BOOKMARK_SNACKBAR': {
      const updatedFlags = { ...state.flags, deleteBookmarkSnackbar: true }
      return { ...state, flags: updatedFlags }
    }
    case 'CLOSE_DELETE_BOOKMARK_SNACKBAR': {
      const updatedFlags = { ...state.flags, deleteBookmarkSnackbar: false }
      return { ...state, flags: updatedFlags }
    }

    default:
      throw new Error('Invalid action')
  }
}
/**
 * This is a 'Reducer' function that will do all of the state transformation and manipulation.
 * @param state {object} - This is the accumulated data object used by all the connected components.
 * @param action {object} - This is the object that represents the kind of transformation will be done to the state.
 */
This is a 'Reducer' function that will do all of the state transformation and manipulation.
@param state {object} - This is the accumulated data object used by all the connected components.
@param action {object} - This is the object that represents the kind of transformation will be done to the state.
This is a ' Reducer ' function that will do all of the state transformation and manipulation . @param state { object } - This is the accumulated data object used by all the connected components . @param action { object } - This is the object that represents the kind of transformation will be done to the state .
function bookmarkReducer(state, action) {
  switch (action.type) {
    case 'SET_BOOKMARKS': {
      const parsedCategory = action.payload.category.toLowerCase()
      const newBookmarkData = createBookmarkData(action.payload)
      const updatedBookmarkGroup = updateBookmarkGroup(
        state[parsedCategory],
        newBookmarkData
      )
      return { ...state, [parsedCategory]: updatedBookmarkGroup }
    }
    case 'EDIT_BOOKMARK': {
      const { oldData, newData } = action.payload
      const updatedBookmark = state[oldData.category.toLowerCase()].map(
        (bookmark) => {
          if (bookmark.name === oldData.name) {
            return {
              ...bookmark,
              name: newData.name,
              link: newData.link ? newData.link : oldData.link,
            }
          }
          return bookmark
        }
      )
      return { ...state, [oldData.category.toLowerCase()]: updatedBookmark }
    }
    case 'DELETE_BOOKMARK': {
      const capturedState =
        state[action.payload.bookmark.category.toLowerCase()]
      const updatedBookmark = capturedState.filter((bookmark) => {
        return bookmark.name !== action.payload.bookmark.name
      })
      return {
        ...state,
        [action.payload.bookmark.category.toLowerCase()]: updatedBookmark,
      }
    }
    case 'OPEN_DRAWER': {
      const updatedFlags = { ...state.flags, openDrawer: true }
      return { ...state, flags: updatedFlags }
    }
    case 'CLOSE_DRAWER': {
      const updatedFlags = { ...state.flags, openDrawer: false }
      return { ...state, flags: updatedFlags }
    }
    case 'OPEN_EDIT_DRAWER': {
      const updatedFlags = { ...state.flags, openEditDrawer: true }
      return { ...state, flags: updatedFlags }
    }
    case 'CLOSE_EDIT_DRAWER': {
      const updatedFlags = { ...state.flags, openEditDrawer: false }
      return { ...state, flags: updatedFlags }
    }
    case 'OPEN_EDIT_SUCCESS_SNACKBAR': {
      const updatedFlags = { ...state.flags, editSuccessSnackbar: true }
      return { ...state, flags: updatedFlags }
    }
    case 'CLOSE_EDIT_SUCCESS_SNACKBAR': {
      const updatedFlags = { ...state.flags, editSuccessSnackbar: false }
      return { ...state, flags: updatedFlags }
    }
    case 'OPEN_SUCCESS_SNACKBAR': {
      const updatedFlags = { ...state.flags, successSnackbar: true }
      return { ...state, flags: updatedFlags }
    }
    case 'CLOSE_SUCCESS_SNACKBAR': {
      const updatedFlags = { ...state.flags, successSnackbar: false }
      return { ...state, flags: updatedFlags }
    }
    case 'OPEN_ERROR_SNACKBAR': {
      const updatedFlags = { ...state.flags, errorSnackbar: true }
      return { ...state, flags: updatedFlags }
    }
    case 'CLOSE_ERROR_SNACKBAR': {
      const updatedFlags = { ...state.flags, errorSnackbar: false }
      return { ...state, flags: updatedFlags }
    }
    case 'OPEN_DELETE_BOOKMARK_SNACKBAR': {
      const updatedFlags = { ...state.flags, deleteBookmarkSnackbar: true }
      return { ...state, flags: updatedFlags }
    }
    case 'CLOSE_DELETE_BOOKMARK_SNACKBAR': {
      const updatedFlags = { ...state.flags, deleteBookmarkSnackbar: false }
      return { ...state, flags: updatedFlags }
    }
    default:
      throw new Error('Invalid action')
  }
}
function bookmarkReducer ( state , action ) { switch ( action . type ) { case 'SET_BOOKMARKS' : { const parsedCategory = action . payload . category . toLowerCase ( ) const newBookmarkData = createBookmarkData ( action . payload ) const updatedBookmarkGroup = updateBookmarkGroup ( state [ parsedCategory ] , newBookmarkData ) return { ... state , [ parsedCategory ] : updatedBookmarkGroup } } case 'EDIT_BOOKMARK' : { const { oldData , newData } = action . payload const updatedBookmark = state [ oldData . category . toLowerCase ( ) ] . map ( ( bookmark ) => { if ( bookmark . name === oldData . name ) { return { ... bookmark , name : newData . name , link : newData . link ? newData . link : oldData . link , } } return bookmark } ) return { ... state , [ oldData . category . toLowerCase ( ) ] : updatedBookmark } } case 'DELETE_BOOKMARK' : { const capturedState = state [ action . payload . bookmark . category . toLowerCase ( ) ] const updatedBookmark = capturedState . filter ( ( bookmark ) => { return bookmark . name !== action . payload . bookmark . name } ) return { ... state , [ action . payload . bookmark . category . toLowerCase ( ) ] : updatedBookmark , } } case 'OPEN_DRAWER' : { const updatedFlags = { ... state . flags , openDrawer : true } return { ... state , flags : updatedFlags } } case 'CLOSE_DRAWER' : { const updatedFlags = { ... state . flags , openDrawer : false } return { ... state , flags : updatedFlags } } case 'OPEN_EDIT_DRAWER' : { const updatedFlags = { ... state . flags , openEditDrawer : true } return { ... state , flags : updatedFlags } } case 'CLOSE_EDIT_DRAWER' : { const updatedFlags = { ... state . flags , openEditDrawer : false } return { ... state , flags : updatedFlags } } case 'OPEN_EDIT_SUCCESS_SNACKBAR' : { const updatedFlags = { ... state . flags , editSuccessSnackbar : true } return { ... state , flags : updatedFlags } } case 'CLOSE_EDIT_SUCCESS_SNACKBAR' : { const updatedFlags = { ... state . flags , editSuccessSnackbar : false } return { ... state , flags : updatedFlags } } case 'OPEN_SUCCESS_SNACKBAR' : { const updatedFlags = { ... state . flags , successSnackbar : true } return { ... state , flags : updatedFlags } } case 'CLOSE_SUCCESS_SNACKBAR' : { const updatedFlags = { ... state . flags , successSnackbar : false } return { ... state , flags : updatedFlags } } case 'OPEN_ERROR_SNACKBAR' : { const updatedFlags = { ... state . flags , errorSnackbar : true } return { ... state , flags : updatedFlags } } case 'CLOSE_ERROR_SNACKBAR' : { const updatedFlags = { ... state . flags , errorSnackbar : false } return { ... state , flags : updatedFlags } } case 'OPEN_DELETE_BOOKMARK_SNACKBAR' : { const updatedFlags = { ... state . flags , deleteBookmarkSnackbar : true } return { ... state , flags : updatedFlags } } case 'CLOSE_DELETE_BOOKMARK_SNACKBAR' : { const updatedFlags = { ... state . flags , deleteBookmarkSnackbar : false } return { ... state , flags : updatedFlags } } default : throw new Error ( 'Invalid action' ) } }
This is a 'Reducer' function that will do all of the state transformation and manipulation.
This is a ' Reducer ' function that will do all of the state transformation and manipulation .
// Parse the bookmark category from the payload. // Create a new piece of bookmark data. // Update the bookmark group data. // Get access to the payload data. // Replace the existing bookmark data. // When the existing bookmark data finds a match, begin the update process. // Otherwise, return the existing bookmark data. // Return the updated state. // Get the specific bookmark entry data. // Execute the delete operation. // Return the updated state.