Refactor and enhance job-related components and context.
All checks were successful
forgejo/romanjaros/portfolio/pipeline/head This commit looks good
All checks were successful
forgejo/romanjaros/portfolio/pipeline/head This commit looks good
This commit is contained in:
parent
ee8f8cf6e3
commit
f16f36ec92
13 changed files with 180 additions and 101 deletions
25
src/app/context/JobsContext.tsx
Normal file
25
src/app/context/JobsContext.tsx
Normal 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>
|
||||
)
|
||||
}
|
25
src/app/context/useJobs.ts
Normal file
25
src/app/context/useJobs.ts
Normal 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,
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue