give
/
src
/
PaymentGateways
/
Gateways
/
Offline
/
resources
/
formBuilder
/
useSetDefaultAttributes.ts
addOfflineAttributes.ts
2 years ago
index.tsx
2 years ago
useSetDefaultAttributes.ts
2 years ago
withOfflineInspectorControls.tsx
2 years ago
useSetDefaultAttributes.ts
27 lines
| 1 | import {useEffect} from '@wordpress/element'; |
| 2 | |
| 3 | type AttributesDefinitions = { |
| 4 | [key: string]: { |
| 5 | type?: string; |
| 6 | enum?: string[]; |
| 7 | default?: any; |
| 8 | }; |
| 9 | }; |
| 10 | |
| 11 | export default function useSetDefaultAttributes( |
| 12 | attributes: {[key: string]: any}, |
| 13 | setAttributes: (attributes: {[key: string]: any}) => void, |
| 14 | attributesDefinitions: AttributesDefinitions |
| 15 | ) { |
| 16 | useEffect(() => { |
| 17 | const attributesWithDefaults = Object.keys(attributesDefinitions).reduce((acc, key) => { |
| 18 | if (!attributes.hasOwnProperty(key) && attributesDefinitions[key].hasOwnProperty('default')) { |
| 19 | acc[key] = attributesDefinitions[key]['default']; |
| 20 | } |
| 21 | return acc; |
| 22 | }, {}); |
| 23 | |
| 24 | setAttributes(attributesWithDefaults); |
| 25 | }, []); |
| 26 | } |
| 27 |