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
usePerformanceSettings.js
63 lines
| 1 | import { useState, useEffect } from 'react'; |
| 2 | import wpApiFetch from '@wordpress/api-fetch'; |
| 3 | |
| 4 | const usePerformanceSettings = () => { |
| 5 | const [ performance, setPerformance ] = useState( { |
| 6 | module_enabled: false, |
| 7 | automations: true, |
| 8 | } ); |
| 9 | |
| 10 | const [ isLoading, setIsLoading ] = useState( true ); |
| 11 | const [ isSaving, setIsSaving ] = useState( false ); |
| 12 | const [ isDirty, setIsDirty ] = useState( false ); |
| 13 | |
| 14 | useEffect( () => { |
| 15 | const fetchAll = async () => { |
| 16 | setIsLoading( true ); |
| 17 | try { |
| 18 | const settings = await wpApiFetch( { path: '/wp/v2/settings' } ); |
| 19 | if ( settings.presto_player_performance ) { |
| 20 | setPerformance( settings.presto_player_performance ); |
| 21 | } |
| 22 | } catch ( err ) { |
| 23 | console.error( 'usePerformanceSettings: failed to fetch settings', err ); |
| 24 | } finally { |
| 25 | setIsLoading( false ); |
| 26 | } |
| 27 | }; |
| 28 | |
| 29 | fetchAll(); |
| 30 | }, [] ); |
| 31 | |
| 32 | const save = async () => { |
| 33 | setIsSaving( true ); |
| 34 | try { |
| 35 | await wpApiFetch( { |
| 36 | path: '/wp/v2/settings', |
| 37 | method: 'POST', |
| 38 | data: { |
| 39 | presto_player_performance: performance, |
| 40 | }, |
| 41 | } ); |
| 42 | setIsDirty( false ); |
| 43 | } catch ( err ) { |
| 44 | setIsSaving( false ); |
| 45 | throw err; |
| 46 | } |
| 47 | setIsSaving( false ); |
| 48 | }; |
| 49 | |
| 50 | const dirtySetPerformance = ( val ) => { setIsDirty( true ); setPerformance( val ); }; |
| 51 | |
| 52 | return { |
| 53 | performance, |
| 54 | setPerformance: dirtySetPerformance, |
| 55 | isLoading, |
| 56 | isSaving, |
| 57 | isDirty, |
| 58 | save, |
| 59 | }; |
| 60 | }; |
| 61 | |
| 62 | export default usePerformanceSettings; |
| 63 |