-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcv-editor-score.tsx
54 lines (42 loc) · 1.27 KB
/
cv-editor-score.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"use client"
import { createContext, useContext, useEffect, useState } from "react"
import { CVEditorContext } from "./cv-editor"
type CVEditorScoreContextState = any
function getNestedPropValue(obj: any, path: string) {
return path.split(".").reduce((acc, key) => {
return acc[key]
}, obj)
}
export const CVEditorScoreContext = createContext<CVEditorScoreContextState>(0)
export const CVEditorScoreProvider = ({
children,
}: {
children: React.ReactNode
}) => {
const editorContent = useContext(CVEditorContext)
const [state, setState] = useState<CVEditorScoreContextState>(0)
useEffect(() => {
const pointers: Record<string, number> = {
"experience.experience": 25,
"education.education": 15,
"personaldata.email": 5,
"personaldata.summary": 15,
"futurejob.preferredPosition": 10,
"skills.expertise": 15,
"skills.language": 15,
}
let score = 0
for (const pointer in pointers) {
const value = getNestedPropValue(editorContent?.state, pointer)
if (value && value?.length) {
score += pointers[pointer]
}
}
setState(score)
}, [editorContent])
return (
<CVEditorScoreContext.Provider value={{ state, setState }}>
{children}
</CVEditorScoreContext.Provider>
)
}