| 1 |
import { useState, createContext, useContext } from '@wordpress/element'; |
| 2 |
|
| 3 |
/** |
| 4 |
* Context Component. |
| 5 |
*/ |
| 6 |
const SettingsContext = createContext( null ); |
| 7 |
|
| 8 |
export function useSettings() { |
| 9 |
return useContext( SettingsContext ); |
| 10 |
} |
| 11 |
|
| 12 |
const SettingsProvider = ( { children } ) => { |
| 13 |
const [ rating, setRating ] = useState( 0 ); |
| 14 |
const [ feedback, setFeedback ] = useState( '' ); |
| 15 |
const [ currentPage, setCurrentPage ] = useState( 'ratings' ); |
| 16 |
const [ nextButtonDisabled, setNextButtonDisabled ] = useState( true ); |
| 17 |
const [ isOpened, setIsOpened ] = useState( true ); |
| 18 |
|
| 19 |
// Notification |
| 20 |
const [ showNotification, setShowNotification ] = useState( false ); |
| 21 |
const [ notificationMessage, setNotificationMessage ] = useState( '' ); |
| 22 |
const [ notificationType, setNotificationType ] = useState( '' ); |
| 23 |
|
| 24 |
return ( |
| 25 |
<SettingsContext.Provider |
| 26 |
value={ { |
| 27 |
rating, |
| 28 |
setRating, |
| 29 |
feedback, |
| 30 |
setFeedback, |
| 31 |
currentPage, |
| 32 |
setCurrentPage, |
| 33 |
nextButtonDisabled, |
| 34 |
setNextButtonDisabled, |
| 35 |
showNotification, |
| 36 |
setShowNotification, |
| 37 |
notificationMessage, |
| 38 |
setNotificationMessage, |
| 39 |
notificationType, |
| 40 |
setNotificationType, |
| 41 |
isOpened, |
| 42 |
setIsOpened, |
| 43 |
} } |
| 44 |
> |
| 45 |
{ children } |
| 46 |
</SettingsContext.Provider> |
| 47 |
); |
| 48 |
}; |
| 49 |
|
| 50 |
export const useNotifications = () => { |
| 51 |
const { setNotificationMessage, setNotificationType, setShowNotification } = useContext( SettingsContext ); |
| 52 |
|
| 53 |
const error = ( message ) => { |
| 54 |
setNotificationMessage( message ); |
| 55 |
setNotificationType( 'error' ); |
| 56 |
setShowNotification( true ); |
| 57 |
}; |
| 58 |
|
| 59 |
const success = ( message ) => { |
| 60 |
setNotificationMessage( message ); |
| 61 |
setNotificationType( 'success' ); |
| 62 |
setShowNotification( true ); |
| 63 |
}; |
| 64 |
|
| 65 |
return { |
| 66 |
success, |
| 67 |
error, |
| 68 |
}; |
| 69 |
}; |
| 70 |
|
| 71 |
export default SettingsProvider; |
| 72 |
|