test
3 weeks ago
useCompleteOnboarding.js
3 weeks ago
useDateRangePicker.js
1 month ago
useEmail.ts
1 month ago
useEngagementChartData.js
1 month ago
useLicenseSettings.js
1 month ago
useLink.js
1 month ago
useMediaDetail.js
1 month ago
useMediaLibrary.js
1 month ago
useMediaList.ts
1 month ago
usePerformanceSettings.js
1 month ago
useRegisterActivePage.js
1 month ago
useSettingOption.js
1 month ago
useSimpleSettingsPage.js
1 month ago
useTopPerforming.js
1 month ago
useTopVideosPaginated.js
1 month ago
useUpgradeCTA.js
1 month ago
useUserDetail.js
1 month ago
useSimpleSettingsPage.js
49 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { useCallback } from 'react'; |
| 3 | import { toast } from '@bsf/force-ui'; |
| 4 | import useSettingOption from './useSettingOption'; |
| 5 | import useRegisterActivePage from './useRegisterActivePage'; |
| 6 | |
| 7 | /** |
| 8 | * Collapses the "single-option page" ceremony into one call: option read, |
| 9 | * patch-style update, save-with-toast, and registration with the nav guard. |
| 10 | * |
| 11 | * Used by pages that have exactly one option and the canonical save/discard |
| 12 | * flow (YouTube, Google Analytics, Performance, etc.). Pages with multi-option |
| 13 | * combined saves (Bunny, Presets) or non-option REST flows (License, |
| 14 | * EmailCapture, Webhooks) talk to useSettingOption / useSettingsData directly. |
| 15 | */ |
| 16 | const useSimpleSettingsPage = ( optionKey, defaults, registerActivePage ) => { |
| 17 | const { data, setData, save, reset, isDirty, isSaving, isLoading } = |
| 18 | useSettingOption( optionKey, defaults ); |
| 19 | |
| 20 | const update = useCallback( |
| 21 | ( patch ) => |
| 22 | setData( ( prev ) => ( { ...( prev || defaults ), ...patch } ) ), |
| 23 | [ setData, defaults ] |
| 24 | ); |
| 25 | |
| 26 | const handleSave = useCallback( async () => { |
| 27 | try { |
| 28 | await save(); |
| 29 | toast.success( __( 'Settings saved.', 'presto-player' ), { |
| 30 | autoDismiss: 3000, |
| 31 | } ); |
| 32 | } catch ( err ) { |
| 33 | toast.error( __( 'Could not save settings.', 'presto-player' ), { |
| 34 | autoDismiss: 5000, |
| 35 | } ); |
| 36 | } |
| 37 | }, [ save ] ); |
| 38 | |
| 39 | useRegisterActivePage( registerActivePage, { |
| 40 | isDirty, |
| 41 | save: handleSave, |
| 42 | discard: reset, |
| 43 | } ); |
| 44 | |
| 45 | return { data, update, handleSave, isDirty, isSaving, isLoading }; |
| 46 | }; |
| 47 | |
| 48 | export default useSimpleSettingsPage; |
| 49 |