diff --git a/app/add/components/company/actions/create-company.ts b/app/add/components/company/actions/create-company.ts index 24aa86b..8c03c9a 100644 --- a/app/add/components/company/actions/create-company.ts +++ b/app/add/components/company/actions/create-company.ts @@ -1,4 +1,5 @@ 'use server' +import { pullCompanyJobs } from '@/lib/actions/pull-company-jobs' import { type QueryCreateCompanyResult, queryCreateCompany, @@ -9,18 +10,20 @@ import type { ActionResponse } from '@/lib/types/api' export const actionCreateCompany = async ( company: InsertCompany, -): Promise>> => { +): Promise[number]>> => { try { - const result = await queryCreateCompany(company) + const [companyEntry] = await queryCreateCompany(company) - logger.debug(result) + logger.debug({ companyEntry }, 'Company created') + + await pullCompanyJobs(companyEntry) return { error: false, - data: result, + data: companyEntry, } } catch (error) { - logger.error(error) + logger.error(error, 'Error while creating company') const errorMessage = error instanceof Error diff --git a/app/add/components/company/company-data.tsx b/app/add/components/company/company-data.tsx index 7f4c616..4464125 100644 --- a/app/add/components/company/company-data.tsx +++ b/app/add/components/company/company-data.tsx @@ -7,6 +7,7 @@ import { useAtomValue } from 'jotai' import { useForm } from 'react-hook-form' import { z } from 'zod' +import { useRouter } from 'next/navigation' import { platformAtom, trackerURLAtom } from '../../state' import { actionCreateCompany } from './actions/create-company' @@ -17,6 +18,7 @@ const schema = z.object({ type FormType = z.infer export const CompanyName = () => { + const { push } = useRouter() const platform = useAtomValue(platformAtom) const trackerURL = useAtomValue(trackerURLAtom) @@ -41,7 +43,7 @@ export const CompanyName = () => { trackerType: 'hiring_platform', // TODO: handle this logic properly }) - console.log(result) + push('/') } return ( diff --git a/app/api/companies/process/route.ts b/app/api/companies/process/route.ts index bbcafd9..ed98c09 100644 --- a/app/api/companies/process/route.ts +++ b/app/api/companies/process/route.ts @@ -1,19 +1,11 @@ +import { pullCompanyJobs } from '@/lib/actions/pull-company-jobs' import { querySelectAllCompanies } from '@/lib/db/queries' -import type { SelectCompany } from '@/lib/db/schema' -import { createPlatform } from '@/lib/hiring-platforms/registry' import { logger } from '@/lib/logger' -import type { NonNullableProperty } from '@/lib/types/utils' import { waitFor } from '@/lib/utils/wait-for' import { ReasonPhrases, StatusCodes } from 'http-status-codes' import { headers } from 'next/headers' -// TODO: Express this through type also, on a lower level -const isHiringPlatform = ( - company: SelectCompany, -): company is NonNullableProperty => - company.trackerType === 'hiring_platform' - export const GET = async () => { const headerList = headers() const auth = headerList.get('Authorization') @@ -35,24 +27,7 @@ export const GET = async () => { const companies = await querySelectAllCompanies() for (const company of companies) { - const start = performance.now() - - logger.info(`Processing company ${company.name}`) - - if (isHiringPlatform(company)) { - const platform = createPlatform( - company.hiringPlatform, - new URL(company.trackerURL), - ) - - await platform.fetchJobs(company.id) - } - - const end = performance.now() - - logger.info( - `Company ${company.name} processed in ${(end - start) / 1000}s`, - ) + await pullCompanyJobs(company) } return Response.json( diff --git a/app/components/list/filter-panel.tsx b/app/components/list/filter-panel.tsx index 09d78d8..246fd3b 100644 --- a/app/components/list/filter-panel.tsx +++ b/app/components/list/filter-panel.tsx @@ -1,11 +1,14 @@ 'use client' import { SegmentedControl } from '@radix-ui/themes' -import { usePathname, useRouter } from 'next/navigation' +import { usePathname, useRouter, useSearchParams } from 'next/navigation' export const FilterPanel = () => { + const searchParams = useSearchParams() const pathname = usePathname() const { push } = useRouter() + const filter = searchParams.get('filter') || 'new' + const handleValueChange = (value: string) => { const searchParams = new URLSearchParams({ filter: value }) @@ -13,7 +16,7 @@ export const FilterPanel = () => { } return ( - + New / Unseen Top Choice diff --git a/lib/actions/pull-company-jobs.ts b/lib/actions/pull-company-jobs.ts new file mode 100644 index 0000000..425c5bc --- /dev/null +++ b/lib/actions/pull-company-jobs.ts @@ -0,0 +1,29 @@ +import type { SelectCompany } from '../db/schema' +import { createPlatform } from '../hiring-platforms/registry' +import { logger } from '../logger' +import type { NonNullableProperty } from '../types/utils' + +// TODO: Express this through type also, on a lower level +const isHiringPlatform = ( + company: SelectCompany, +): company is NonNullableProperty => + company.trackerType === 'hiring_platform' + +export const pullCompanyJobs = async (company: SelectCompany) => { + const start = performance.now() + + logger.info(`Processing company ${company.name}`) + + if (isHiringPlatform(company)) { + const platform = createPlatform( + company.hiringPlatform, + new URL(company.trackerURL), + ) + + await platform.fetchJobs(company.id) + } + + const end = performance.now() + + logger.info(`Company ${company.name} processed in ${(end - start) / 1000}s`) +} diff --git a/lib/db/queries.ts b/lib/db/queries.ts index a73feb4..898300f 100644 --- a/lib/db/queries.ts +++ b/lib/db/queries.ts @@ -5,10 +5,7 @@ import { db } from './db' import { type InsertCompany, type InsertJob, companies, jobs } from './schema' export const queryCreateCompany = async (company: InsertCompany) => { - const result = await db - .insert(companies) - .values(company) - .returning({ id: companies.id }) + const result = await db.insert(companies).values(company).returning() return result }