Refactor and enhance job-related components and context.
All checks were successful
forgejo/romanjaros/portfolio/pipeline/head This commit looks good

This commit is contained in:
Roman Jaroš 2025-01-15 16:14:26 +00:00
parent ee8f8cf6e3
commit f16f36ec92
13 changed files with 180 additions and 101 deletions

View file

@ -0,0 +1,25 @@
"use client"
import {createContext, ReactNode, FC, useState, Dispatch, SetStateAction} from "react";
import {Job} from "../type";
type JobsContextPayload = {
data: Job[]
filter?: string
setFilter: Dispatch<SetStateAction<string | undefined>>
} | undefined
export const JobsContext = createContext<JobsContextPayload>(undefined);
type JobsContextProviderProps = {
jobs: Job[]
children: ReactNode[]
}
export const JobsProvider: FC<JobsContextProviderProps> = ({children, jobs: data}) => {
const [filter, setFilter] = useState<string>()
return (
<JobsContext value={{data, filter, setFilter}}>{children}</JobsContext>
)
}

View file

@ -0,0 +1,25 @@
"use client"
import {useContext} from "react";
import {JobsContext} from "./JobsContext";
export const useJobs = () => {
const context = useContext(JobsContext)
if (!context) {
throw new Error('useJobs must be used within a JobsProvider!');
}
const handleFilterJob = (name: string) => {
if (name === context.filter) {
context.setFilter(undefined)
} else {
context.setFilter(name)
}
}
return {
jobs: context.data,
filter: context?.filter,
filterJobs: handleFilterJob,
}
}