'use server';

import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import { setTimeout } from 'node:timers/promises';

export async function createBook(formData: FormData) {
  const newBook = Object.fromEntries(formData.entries());

  await setTimeout(5000);

  await fetch('http://localhost:3001/books', {
    method: 'POST',
    body: JSON.stringify(newBook),
    headers: {
      'Content-Type': 'application/json',
    },
  });

  revalidatePath('/books');
  redirect('/books');
}
