Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

Create company in job form

Khuram Niaz5c7daee97b9b
diff --git a/src/actions/job.actions.ts b/src/actions/job.actions.ts
index a22d6ca..ac39907 100644
--- a/src/actions/job.actions.ts
+++ b/src/actions/job.actions.ts
@@ -1,7 +1,7 @@
import "server-only";
import prisma from "@/lib/db";
-export async function getStatusList(): Promise<any | undefined> {
+export const getStatusList = async (): Promise<any | undefined> => {
try {
const statuses = await prisma.jobStatus.findMany();
return statuses;
@@ -9,4 +9,14 @@ export async function getStatusList(): Promise<any | undefined> {
console.error("Failed to fetch status list:", error);
throw new Error("Failed to fetch status list.");
}
-}
+};
+
+export const getCompanyList = async (): Promise<any | undefined> => {
+ try {
+ const companies = await prisma.company.findMany();
+ return companies;
+ } catch (error) {
+ console.error("Failed to fetch company list:", error);
+ throw new Error("Failed to fetch company list.");
+ }
+};
diff --git a/src/app/api/company/route.ts b/src/app/api/company/route.ts
new file mode 100644
index 0000000..722977c
--- /dev/null
+++ b/src/app/api/company/route.ts
@@ -0,0 +1,31 @@
+import "server-only";
+import prisma from "@/lib/db";
+import { NextRequest, NextResponse } from "next/server";
+
+export async function POST(req: NextRequest) {
+ const { name } = await req.json();
+
+ if (!name) {
+ return NextResponse.json(
+ { message: "Company name is required" },
+ { status: 400 }
+ );
+ }
+
+ try {
+ // Upsert the name (create if it does not exist, update if it exists)
+ const upsertedName = await prisma.company.upsert({
+ where: { name },
+ update: { name },
+ create: { name },
+ });
+
+ return NextResponse.json(upsertedName, { status: 201 });
+ } catch (error) {
+ console.error("Error upserting name:", error);
+ return NextResponse.json(
+ { message: "Internal server error" },
+ { status: 500 }
+ );
+ }
+}
diff --git a/src/app/globals.css b/src/app/globals.css
index 4449064..8b0af44 100644
--- a/src/app/globals.css
+++ b/src/app/globals.css
@@ -43,7 +43,7 @@
--accent-foreground: 210 40% 98%;
--destructive: 0 62.8% 40.6%;
--destructive-foreground: 210 40% 98%;
- --border: 217.2 32.6% 21.5%;
+ --border: 217.2 32.6% 23.5%;
--input: 217.2 32.6% 21.5%;
--ring: 224.3 76.3% 48%;
}
diff --git a/src/components/AddJob.tsx b/src/components/AddJob.tsx
index 2404703..30ff9a3 100644
--- a/src/components/AddJob.tsx
+++ b/src/components/AddJob.tsx
@@ -1,15 +1,18 @@
import { DialogHeader, DialogTitle } from "@/components/ui/dialog";
import AddJobForm from "./AddJobForm";
-import { getStatusList } from "@/actions/job.actions";
+import { getCompanyList, getStatusList } from "@/actions/job.actions";
export async function AddJob() {
- const statuses = await getStatusList();
+ const [statuses, companies] = await Promise.all([
+ getStatusList(),
+ getCompanyList(),
+ ]);
return (
<>
<DialogHeader>
<DialogTitle>Add Job</DialogTitle>
</DialogHeader>
- <AddJobForm jobStatuses={statuses} />
+ <AddJobForm jobStatuses={statuses} companies={companies} />
</>
);
}
diff --git a/src/components/AddJobForm.tsx b/src/components/AddJobForm.tsx
index 714efbd..fe8b545 100644
--- a/src/components/AddJobForm.tsx
+++ b/src/components/AddJobForm.tsx
@@ -21,12 +21,19 @@ import { DatePicker } from "./DatePicker";
import SelectFormCtrl from "./Select";
import { JOB_SOURCES } from "@/lib/data/jobSourcesData";
import { SALARY_RANGES } from "@/lib/data/salaryRangeData";
+import { useState } from "react";
interface AddJobFormProps {
jobStatuses: { id: string; statusName: string }[];
+ companies: any[];
}
-export default function AddJobForm({ jobStatuses }: AddJobFormProps) {
+export default function AddJobForm({
+ jobStatuses,
+ companies,
+}: AddJobFormProps) {
+ const [loading, setLoading] = useState(false);
+
const form = useForm<z.infer<typeof AddJobFormSchema>>({
resolver: zodResolver(AddJobFormSchema),
// mode: "onChange",
@@ -48,10 +55,6 @@ export default function AddJobForm({ jobStatuses }: AddJobFormProps) {
});
}
- function handleOnCreate(value: string) {
- console.log("handle new optoin created: ", value);
- }
-
return (
<>
<Form {...form}>
@@ -84,7 +87,11 @@ export default function AddJobForm({ jobStatuses }: AddJobFormProps) {
<FormItem className="flex flex-col">
<FormLabel>Company</FormLabel>
<FormControl>
- <Input {...field} />
+ <Combobox
+ dataKeys={["name", "id"]}
+ options={companies}
+ field={field}
+ />
</FormControl>
<FormMessage />
</FormItem>
@@ -116,11 +123,9 @@ export default function AddJobForm({ jobStatuses }: AddJobFormProps) {
<FormItem className="flex flex-col">
<FormLabel>Job Source</FormLabel>
<Combobox
+ dataKeys={["label", "value"]}
options={JOB_SOURCES}
field={field}
- onCreate={(value) => {
- handleOnCreate(value);
- }}
/>
<FormMessage />
</FormItem>
diff --git a/src/components/ComboBox.tsx b/src/components/ComboBox.tsx
index e8b5436..2ee82d1 100644
--- a/src/components/ComboBox.tsx
+++ b/src/components/ComboBox.tsx
@@ -1,6 +1,6 @@
"use client";
-import { Check, ChevronsUpDown } from "lucide-react";
+import { Check, ChevronsUpDown, CirclePlus, Loader } from "lucide-react";
import { ControllerRenderProps, FieldName } from "react-hook-form";
import { cn } from "@/lib/utils";
@@ -27,21 +27,36 @@ import {
import { Job } from "@/models/job.model";
import { ScrollArea } from "./ui/scroll-area";
import { useState } from "react";
-
-export type ComboboxOptions = {
- value: string;
- label: string;
-};
+import { upsertCompanyName } from "@/utils/company";
+import { delay } from "@/utils/delay";
interface ComboboxProps {
- options: ComboboxOptions[];
+ dataKeys: string[];
+ options: any[];
field: ControllerRenderProps<Job, any>;
- onCreate?: (value: string) => void;
}
-export function Combobox({ options, field, onCreate }: ComboboxProps) {
+export function Combobox({ options, field, dataKeys }: ComboboxProps) {
const [newOption, setNewOption] = useState<string>("");
const [isPopoverOpen, setIsPopoverOpen] = useState<boolean>(false);
+ const [loading, setLoading] = useState(false);
+ const [key1, key2] = dataKeys;
+
+ async function onCreateOption(value: string) {
+ setLoading(true);
+ try {
+ if (field.name === "company") {
+ const newOption = await upsertCompanyName(value);
+ options.unshift(newOption);
+ field.onChange(newOption[key2]);
+ setIsPopoverOpen(false);
+ }
+ } catch (error) {
+ console.error("Error:", error);
+ } finally {
+ setLoading(false);
+ }
+ }
return (
<Popover open={isPopoverOpen} onOpenChange={setIsPopoverOpen}>
@@ -51,18 +66,23 @@ export function Combobox({ options, field, onCreate }: ComboboxProps) {
variant="outline"
role="combobox"
className={cn(
- "w-[200px] justify-between",
+ "w-[240px] justify-between capitalize",
!field.value && "text-muted-foreground"
)}
>
{field.value
- ? options.find((option) => option.value === field.value)?.label
+ ? options.find((option) => option[key2] === field.value)[key1]
: `Select ${field.name}`}
- <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
+
+ {loading ? (
+ <Loader className="h-4 w-4 shrink-0 spinner" />
+ ) : (
+ <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
+ )}
</Button>
</FormControl>
</PopoverTrigger>
- <PopoverContent className="w-[200px] p-0">
+ <PopoverContent className="w-[240px] p-0">
<Command>
<CommandInput
value={newOption}
@@ -71,28 +91,33 @@ export function Combobox({ options, field, onCreate }: ComboboxProps) {
/>
<CommandEmpty
onClick={() => {
- if (onCreate) {
- onCreate(newOption);
- setNewOption(newOption);
- }
+ onCreateOption(newOption);
+ setNewOption("");
}}
className="flex cursor-pointer items-center justify-center gap-1 italic"
>
- <p>Create: </p>
- <p className="block max-w-48 truncate font-semibold text-primary">
- {newOption}
- </p>
+ {field.name === "company" ? (
+ <>
+ <CirclePlus className="h-4 w-4" />
+ <p>Create: </p>
+ <p className="block max-w-48 truncate font-semibold text-primary">
+ {newOption}
+ </p>
+ </>
+ ) : (
+ <p className="font-semibold text-primary">No source found!</p>
+ )}
</CommandEmpty>
<ScrollArea>
<CommandGroup>
- <CommandList>
+ <CommandList className="capitalize">
{options.map((option) => (
<CommandItem
- value={option.label}
- key={option.value}
+ value={option[key1]}
+ key={option[key2]}
onSelect={() => {
if (field.onChange) {
- field.onChange(option.value);
+ field.onChange(option[key2]);
setIsPopoverOpen(false);
}
}}
@@ -100,12 +125,12 @@ export function Combobox({ options, field, onCreate }: ComboboxProps) {
<Check
className={cn(
"mr-2 h-4 w-4",
- option.value === field.value
+ option[key2] === field.value
? "opacity-100"
: "opacity-0"
)}
/>
- {option.label}
+ {option[key1]}
</CommandItem>
))}
</CommandList>
diff --git a/src/utils/company.ts b/src/utils/company.ts
new file mode 100644
index 0000000..62a27f2
--- /dev/null
+++ b/src/utils/company.ts
@@ -0,0 +1,16 @@
+export const upsertCompanyName = async (name: string) => {
+ const response = await fetch("/api/company", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({ name: name.trim().toLowerCase() }),
+ });
+
+ if (!response.ok) {
+ throw new Error(`HTTP error! status: ${response.status}`);
+ }
+
+ const data = await response.json();
+ return data;
+};
diff --git a/src/utils/delay.ts b/src/utils/delay.ts
new file mode 100644
index 0000000..8500828
--- /dev/null
+++ b/src/utils/delay.ts
@@ -0,0 +1,4 @@
+export const delay = (max = 2500) => {
+ const randomDelayTime = Math.floor(Math.random() * (max - 500 + 1));
+ return new Promise((resolve) => setTimeout(resolve, randomDelayTime));
+};