# give/4.17.0/src/PaymentGateways/Gateways/Offline/resources/formBuilder/useSetDefaultAttributes.ts

GiveWP – Donation Plugin and Fundraising Platform, version 4.17.0. 27 lines.

- Page: https://pluginprobe.com/plugins/give/4.17.0/code/src/PaymentGateways/Gateways/Offline/resources/formBuilder/useSetDefaultAttributes.ts
- Raw: https://pluginprobe.com/plugins/give/4.17.0/raw/src/PaymentGateways/Gateways/Offline/resources/formBuilder/useSetDefaultAttributes.ts
- Modified: 2023-10-16T17:55:46+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/give/4.17.0/code/src/PaymentGateways/Gateways/Offline/resources/formBuilder/useSetDefaultAttributes.ts#L10-L20`.

```typescript
import {useEffect} from '@wordpress/element';

type AttributesDefinitions = {
    [key: string]: {
        type?: string;
        enum?: string[];
        default?: any;
    };
};

export default function useSetDefaultAttributes(
    attributes: {[key: string]: any},
    setAttributes: (attributes: {[key: string]: any}) => void,
    attributesDefinitions: AttributesDefinitions
) {
    useEffect(() => {
        const attributesWithDefaults = Object.keys(attributesDefinitions).reduce((acc, key) => {
            if (!attributes.hasOwnProperty(key) && attributesDefinitions[key].hasOwnProperty('default')) {
                acc[key] = attributesDefinitions[key]['default'];
            }
            return acc;
        }, {});

        setAttributes(attributesWithDefaults);
    }, []);
}

```
