functions.js
unknown
plain_text
2 years ago
3.8 kB
2
Indexable
import { createContext, useContext, useEffect, useState } from "react"; import { categories } from "../services/londi-constants"; import { getFundingMatching } from "../services/londi-funding-matching"; import { getTestFields } from "../services/londi-suggestions"; import { screeningResultsState } from "./state"; export const initializer = (initialValue = screeningResultsState) => JSON.parse(sessionStorage.getItem("kidInfo")) || initialValue; export const DiagnosticContext = createContext(); export const useOutsideClick = (ref) => { const { dispatch } = useContext(DiagnosticContext); useEffect(() => { const handleClickOutside = (event) => { if (ref.current && !ref.current.contains(event.target)) { dispatch({ type: "options" }); } }; // Bind the event listener document.addEventListener("mousedown", handleClickOutside); return () => { // Unbind the event listener on clean up document.removeEventListener("mousedown", handleClickOutside); }; }, [dispatch, ref]); }; export const showFundingRecommendations = (state) => { const resultsReading = getTestFields(state.readingTestId).map((el, i) => { return { ...el, result: state?.resultsReading?.[i] }; }); const secondResultsReading = getTestFields(state.readingSecondTestId).map( (el, i) => { return { ...el, result: state?.resultsSecondReading?.[i] }; } ); const resultsWriting = getTestFields(state.writingTestId).map((el, i) => { return { ...el, result: state?.resultsWriting?.[i] }; }); const secondResultsWriting = getTestFields(state.writingSecondTestId).map( (el, i) => { return { ...el, result: state?.resultsSecondWriting?.[i] }; } ); const resultsCounting = getTestFields(state.countingTestId).map((el, i) => { return { ...el, result: state?.resultsCounting?.[i] }; }); const secondResultsCounting = getTestFields(state.countingSecondTestId).map( (el, i) => { return { ...el, result: state?.resultsSecondCounting?.[i] }; } ); const fundingMatchingRecomendations = { education: state.education, screeningResults: [ { category: categories.Reading, testId: state.readingTestId, results: resultsReading, }, { category: categories.Reading, testId: state.readingSecondTestId, results: secondResultsReading, }, { category: categories.Writing, testId: state.writingTestId, results: resultsWriting, }, { category: categories.Writing, testId: state.writingSecondTestId, results: secondResultsWriting, }, { category: categories.Math, testId: state.countingTestId, results: resultsCounting, }, { category: categories.Math, testId: state.countingSecondTestId, results: secondResultsCounting, }, ], }; //here is all the data that needed for funding recomendations const suggestedRecomendations = getFundingMatching( fundingMatchingRecomendations ); return suggestedRecomendations; }; //function combines multiple reducers export const reduceReducers = (...reducers) => (state, action) => reducers.reduce((acc, nextReducer) => nextReducer(acc, action), state); const getStorageValue = (key, defaultValue) => { // getting stored value const saved = sessionStorage.getItem(key); const initial = JSON.parse(saved); return initial || defaultValue; }; export const useSessionStorage = (key, defaultValue) => { const [value, setValue] = useState(() => { return getStorageValue(key, defaultValue); }); useEffect(() => { // storing input name sessionStorage.setItem(key, JSON.stringify(value)); }, [key, value]); return [value, setValue]; };
Editor is loading...