{"size": 5269, "ext": "js", "max_stars_repo_path": "src/actions/ideaActions.js", "max_stars_repo_name": "carcarvroom/simplize", "max_stars_repo_stars_event_min_datetime": "2020-09-11T00:48:31.000Z", "max_stars_repo_stars_event_max_datetime": "2020-09-11T00:48:31.000Z", "max_issues_repo_path": "src/actions/ideaActions.js", "max_issues_repo_name": "carcarvroom/simplize-frontend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/actions/ideaActions.js", "max_forks_repo_name": "carcarvroom/simplize-frontend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.9505494505, "max_line_length": 157, "alphanum_fraction": 0.5619662175}
const loadBoards = (payload) => ({type: "LOAD_IDEA_BOARDS", payload})

export const getIdeaBoards = userId => {
  return async dispatch => {
    try {
      const res = await fetch(`http://localhost:3000/my_boards/${userId}/ideaboard`, {headers: {"Authorization": `Bearer ${localStorage.getItem("token")}`}})
      const ideaBoards = await res.json()
      console.log('fetched ideaBoards', ideaBoards)
      const sortedIdeas = ideaBoards.map(board => {
        return {...board,
          tasks: board.tasks.sort((a, b) => {
            return parseInt(a.title) - parseInt(b.title)
          })
        }
      }).map(list => {
          return {
            ...list,
            tasks: list.tasks.map(task => {
              return {
                ...task,
                title: `card-${task.title}`
              }
            })
          }
      })
      dispatch(loadBoards(sortedIdeas))
    }
    catch(error) {
      console.log('Idea Board Fetch Error:', error)
    }
  }
}

export const createIdea = idea => {
  return async dispatch => {
    try {
      const res = await fetch(`http://localhost:3000/tasks`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json",
            "Authorization": `Bearer ${localStorage.getItem("token")}`
        },
        body: JSON.stringify(idea)
      })
      const newIdea = await res.json()
      if(newIdea.error) {
          alert(newIdea.error)
      } else {
        console.log('created new idea!', newIdea)
        dispatch(getIdeaBoards(parseInt(localStorage.getItem('userId'))))
      }
    }
    catch(error) {
      console.log('Create Issue Error:', error)
    }
  }
}

export const editIdeaCard = (cardId, updatedInfo) => {
  return async dispatch => {
    try {
      const res = await fetch(`http://localhost:3000/tasks/${cardId}`, {
        method: "PATCH",
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json",
            "Authorization": `Bearer ${localStorage.getItem("token")}`
        },
        body: JSON.stringify(updatedInfo)
      })
      const ideaUpdated = await res.json()
      dispatch(getIdeaBoards(parseInt(localStorage.getItem('userId'))))
    }
    catch(error) {
      console.log('Update Task Card Error:', error)
    }
  }
}

export const editIdeaPosition = (ideaId, updatedInfo, boardId=null) => {
  return async dispatch => {
    try {
      const res = await fetch(`http://localhost:3000/tasks/${ideaId}`, {
        method: "PATCH",
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json",
            "Authorization": `Bearer ${localStorage.getItem("token")}`
        },
        body: JSON.stringify(updatedInfo)
      })
      const ideaUpdated = await res.json()
      if(boardId) {
        dispatch(getIdeaBoards(parseInt(localStorage.getItem('userId'))))
      }
    }
    catch(error) {
      console.log('Update Task Card Error:', error)
    }
  }
}

export const deleteIdea = (taskId) => {
  return async dispatch => {
    try {
      await fetch(`http://localhost:3000/tasks/${taskId}`, {
        method: "DELETE",
        headers: {
          "Authorization": `Bearer ${localStorage.getItem("token")}`
        }
      })
      dispatch(getIdeaBoards(parseInt(localStorage.getItem('userId'))))
    }
    catch(error) {
      console.log('Delete Issue Error:', error)
    }
  }
}

export const createIdeaboard = ideaboard => {
  return async dispatch => {
    try {
      const res = await fetch(`http://localhost:3000/boards`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json",
            "Authorization": `Bearer ${localStorage.getItem("token")}`
        },
        body: JSON.stringify(ideaboard)
      })
      const newideaboard = await res.json()
      if(newideaboard.error) {
          alert(newideaboard.error)
      } else {
        dispatch(getIdeaBoards(parseInt(localStorage.getItem('userId'))))
      }
    }
    catch(error) {
      console.log('Create Ideaboard Error:', error)
    }
  }
}

export const editIdeaboard = (boardId, updatedInfo) => {
  return async dispatch => {
    try {
      const res = await fetch(`http://localhost:3000/boards/${boardId}`, {
        method: "PATCH",
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json",
            "Authorization": `Bearer ${localStorage.getItem("token")}`
        },
        body: JSON.stringify(updatedInfo)
      })
      const ideaboardUpdated = await res.json()
      dispatch(getIdeaBoards(parseInt(localStorage.getItem('userId'))))
    }
    catch(error) {
      console.log('Update Ideaboard Error:', error)
    }
  }
}

export const deleteIdeaboard = boardId => {
  return async dispatch => {
    try {
      await fetch(`http://localhost:3000/boards/${boardId}`, {
        method: "DELETE",
        headers: {
          "Authorization": `Bearer ${localStorage.getItem("token")}`
        }
      })
      dispatch(getIdeaBoards(parseInt(localStorage.getItem('userId'))))
    }
    catch(error) {
      console.log('Delete Ideaboard Error:', error)
    }
  }
}