{"size": 522, "ext": "js", "max_stars_repo_path": "nextjs/src/hooks/useTodos.js", "max_stars_repo_name": "hyesungoh/soManyToDos", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "nextjs/src/hooks/useTodos.js", "max_issues_repo_name": "hyesungoh/soManyToDos", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "nextjs/src/hooks/useTodos.js", "max_forks_repo_name": "hyesungoh/soManyToDos", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.7272727273, "max_line_length": 59, "alphanum_fraction": 0.6264367816}
import useSWR from "swr";
import { del, post } from "../lib/api";
import { fetcher } from "../lib/fetcher";

function useTodos() {
  const { data, error, mutate } = useSWR("/todo", fetcher);

  async function createTodo(todoString) {
    await post("/todo", { fields: { name: todoString } });
    mutate({ ...data, fields: { name: todoString } });
  }

  async function deleteTodo(todoId) {
    await del(`/todo/${todoId}`);
    mutate();
  }

  return { data, error, createTodo, deleteTodo };
}

export default useTodos;
