| 1 |
/** |
| 2 |
* WordPress dependencies. |
| 3 |
*/ |
| 4 |
import apiFetch from '@wordpress/api-fetch'; |
| 5 |
import { createContext, WPElement, useContext, useMemo, useState } from '@wordpress/element'; |
| 6 |
|
| 7 |
/** |
| 8 |
* Instant Results context. |
| 9 |
*/ |
| 10 |
const Context = createContext(); |
| 11 |
|
| 12 |
/** |
| 13 |
* Weighting settings app. |
| 14 |
* |
| 15 |
* @param {object} props Component props. |
| 16 |
* @param {string} props.apiUrl API URL. |
| 17 |
* @param {Function} props.children Component children. |
| 18 |
* @param {'auto'|'manual'} props.metaMode Metadata management mode. |
| 19 |
* @param {object} props.weightableFields Weightable fields, indexed by post type. |
| 20 |
* @param {object} props.weightingConfiguration Weighting configuration, indexed by post type. |
| 21 |
* @returns {WPElement} Element. |
| 22 |
*/ |
| 23 |
export const WeightingSettingsProvider = ({ |
| 24 |
apiUrl, |
| 25 |
children, |
| 26 |
metaMode, |
| 27 |
weightableFields, |
| 28 |
weightingConfiguration, |
| 29 |
}) => { |
| 30 |
const [currentWeightingConfiguration, setCurrentWeightingConfiguration] = useState({ |
| 31 |
...weightingConfiguration, |
| 32 |
}); |
| 33 |
|
| 34 |
const [isBusy, setIsBusy] = useState(false); |
| 35 |
|
| 36 |
/** |
| 37 |
* Whether to show weighting for metadata. |
| 38 |
*/ |
| 39 |
const isManual = useMemo(() => metaMode === 'manual', [metaMode]); |
| 40 |
|
| 41 |
/** |
| 42 |
* Handle data change. |
| 43 |
* |
| 44 |
* @param {string} postType Post type to update. |
| 45 |
* @param {Array} values New values. |
| 46 |
* @returns {void} |
| 47 |
*/ |
| 48 |
const setWeightingForPostType = (postType, values) => { |
| 49 |
setCurrentWeightingConfiguration({ |
| 50 |
...currentWeightingConfiguration, |
| 51 |
[postType]: values, |
| 52 |
}); |
| 53 |
}; |
| 54 |
|
| 55 |
/** |
| 56 |
* Save settings. |
| 57 |
* |
| 58 |
* @returns {void} |
| 59 |
*/ |
| 60 |
const save = async () => { |
| 61 |
setIsBusy(true); |
| 62 |
|
| 63 |
try { |
| 64 |
await apiFetch({ |
| 65 |
body: JSON.stringify(currentWeightingConfiguration), |
| 66 |
headers: { |
| 67 |
'Content-Type': 'application/json', |
| 68 |
}, |
| 69 |
method: 'POST', |
| 70 |
url: apiUrl, |
| 71 |
}); |
| 72 |
} catch (e) { |
| 73 |
console.error(e); // eslint-disable-line no-console |
| 74 |
throw e; |
| 75 |
} finally { |
| 76 |
setIsBusy(false); |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
// eslint-disable-next-line react/jsx-no-constructed-context-values |
| 81 |
const contextValue = { |
| 82 |
currentWeightingConfiguration, |
| 83 |
isBusy, |
| 84 |
isManual, |
| 85 |
save, |
| 86 |
setWeightingForPostType, |
| 87 |
weightableFields, |
| 88 |
}; |
| 89 |
|
| 90 |
/** |
| 91 |
* Render. |
| 92 |
*/ |
| 93 |
return <Context.Provider value={contextValue}>{children}</Context.Provider>; |
| 94 |
}; |
| 95 |
|
| 96 |
/** |
| 97 |
* Use the API Search context. |
| 98 |
* |
| 99 |
* @returns {object} API Search Context. |
| 100 |
*/ |
| 101 |
export const useWeightingSettings = () => { |
| 102 |
return useContext(Context); |
| 103 |
}; |
| 104 |
|