Skip to content

Commit

Permalink
Api to actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanies committed Jul 1, 2024
1 parent 2fcab59 commit 07ca9c2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 84 deletions.
49 changes: 5 additions & 44 deletions components/elements/NewJobForm.tsx
Original file line number Diff line number Diff line change
@@ -1,69 +1,30 @@
"use client"
import { useEffect, useState } from "react";
import { auth } from '@/src/auth/auth';
import { createJob } from "@/src/app/lib/actions";

export default function NewJobForm() {
const [money, setMoney] = useState<string>('');
const [jobTitle, setJobTitle] = useState<string>('');
const [jobDescription, setJobDescription] = useState<string>('');


const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();

const formData = {
money,
jobTitle,
jobDescription
};

try {
const response = await fetch('/api/newjob', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData),
});

if (!response.ok) {
throw new Error('Network response was not ok');
}

alert('Job created successfully!');

setMoney('');
setJobTitle('');
setJobDescription('');
} catch (error) {
console.error('Error:', error);
alert('Failed to create job');
}
};

return (
<form className="mt-12 flex flex-col gap-2" onSubmit={handleSubmit}>
<form className="mt-12 flex flex-col gap-2" action={createJob}>
<div className="flex gap-2">
<input
className="border rounded p-2"
type="text"
name="position"
placeholder="Puesto"
value={jobTitle}
onChange={(e) => setJobTitle(e.target.value)}
/>
<input
className="border rounded p-2"
type="text"
name="salary"
placeholder="Pago"
onChange={(e) => setMoney(e.target.value)}
/>
</div>
<div>
<textarea
name="description"
className="border h-[13rem] rounded p-2 w-full"
placeholder="Descripción"
value={jobDescription}
onChange={(e) => setJobDescription(e.target.value)}
/>
</div>
<button type="submit" className="bg-[#0070B9] text-[#fff]">
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/auth/[...nextauth]/route.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

import { handlers } from "@/auth/auth"
import { handlers } from "@/src/auth/auth"
export const { GET, POST } = handlers
39 changes: 0 additions & 39 deletions src/app/api/newjob/route.tsx

This file was deleted.

49 changes: 49 additions & 0 deletions src/app/lib/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use server'
import { auth } from '@/src/auth/auth';
import { getUserID, getUserName } from '@/src/utils/userInfo';
import { sql } from '@vercel/postgres';
import { NextResponse } from 'next/server';

export async function createJob(formData: FormData) {
console.log('createJob', formData)

const rawFormData = Object.fromEntries(formData.entries());

const jobTitle = rawFormData.position as string;
const money = rawFormData.salary as string;
const jobDescription = rawFormData.description as string;

try {
const session = await auth();
if (!session || !session.user || !session.user.accessToken) {
throw new Error("Usuario no autenticado");
}

console.log(session);

if (!money) {
return ("Cantidad de dinero inválida");
}
if (!jobTitle) {
return ("El título del trabajo es obligatorio");
}
if (!jobDescription) {
return ("La descripción del trabajo es obligatoria");
}

const username = await getUserName();
const userid = await getUserID();

await sql`
INSERT INTO oferta (identify, imagen, usuario, pago, descripcion, titulo)
VALUES (${userid}, ${session.user.image}, ${username}, ${money}, ${jobDescription}, ${jobTitle})
`;

return "Succesful";
} catch (error) {
return "Error";
}
}



0 comments on commit 07ca9c2

Please sign in to comment.