|
| 1 | +import { Button } from '@equinor/eds-core-react'; |
| 2 | +import { useAppState } from '@equinor/fusion-framework-react-app/state'; |
| 3 | +import type { Language } from './types'; |
| 4 | + |
| 5 | +/** |
| 6 | + * SimpleStateExamples - Component demonstrating basic useAppState patterns |
| 7 | + * |
| 8 | + * This component showcases three fundamental patterns: |
| 9 | + * 1. Boolean state management |
| 10 | + * 2. String state with type safety |
| 11 | + * 3. Optional state (can be undefined) |
| 12 | + * |
| 13 | + * Key Learning Points: |
| 14 | + * - How to use useAppState with different data types |
| 15 | + * - Providing default values for better UX |
| 16 | + * - Hierarchical state key naming conventions |
| 17 | + * - Simple state update patterns |
| 18 | + */ |
| 19 | +export const SimpleStateExamples = () => { |
| 20 | + // EXAMPLE 1: Boolean state management |
| 21 | + // State key: 'app.notifications.enabled' - hierarchical naming for app settings |
| 22 | + const [isNotificationEnabled, setIsNotificationEnabled] = useAppState<boolean>( |
| 23 | + 'app.notifications.enabled', |
| 24 | + { defaultValue: true }, // Always provide sensible defaults |
| 25 | + ); |
| 26 | + |
| 27 | + // EXAMPLE 2: String state with type safety |
| 28 | + // Using custom Language type for better type safety |
| 29 | + const [currentLanguage, setCurrentLanguage] = useAppState<Language>('app.language', { |
| 30 | + defaultValue: 'en', |
| 31 | + }); |
| 32 | + |
| 33 | + // EXAMPLE 3: Optional state (can be undefined) |
| 34 | + // No default value means it starts as undefined |
| 35 | + const [lastLoginTime, setLastLoginTime] = useAppState<string>('user.lastLogin'); |
| 36 | + |
| 37 | + // Example of simple state update - setting current timestamp as string |
| 38 | + const handleLogin = () => { |
| 39 | + setLastLoginTime(new Date().toLocaleString()); |
| 40 | + }; |
| 41 | + |
| 42 | + return ( |
| 43 | + <section style={{ marginBottom: '40px' }}> |
| 44 | + <h2>📚 Basic State Management</h2> |
| 45 | + <div style={{ border: '2px solid #e0e0e0', borderRadius: '8px', padding: '20px' }}> |
| 46 | + {/* Boolean State Example */} |
| 47 | + <div style={{ marginBottom: '24px' }}> |
| 48 | + <h3>Boolean State: Notifications</h3> |
| 49 | + <p> |
| 50 | + <strong>Current Value:</strong> {isNotificationEnabled ? '✅ Enabled' : '❌ Disabled'} |
| 51 | + </p> |
| 52 | + <p> |
| 53 | + <strong>State Key:</strong> <code>'app.notifications.enabled'</code> |
| 54 | + </p> |
| 55 | + <div style={{ marginTop: '12px', display: 'flex', gap: '8px' }}> |
| 56 | + <Button |
| 57 | + onClick={() => setIsNotificationEnabled(true)} |
| 58 | + variant={isNotificationEnabled ? 'contained' : 'outlined'} |
| 59 | + > |
| 60 | + Enable |
| 61 | + </Button> |
| 62 | + <Button |
| 63 | + onClick={() => setIsNotificationEnabled(false)} |
| 64 | + variant={!isNotificationEnabled ? 'contained' : 'outlined'} |
| 65 | + > |
| 66 | + Disable |
| 67 | + </Button> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + |
| 71 | + {/* String State Example */} |
| 72 | + <div style={{ marginBottom: '24px' }}> |
| 73 | + <h3>String State: Language Selection</h3> |
| 74 | + <p> |
| 75 | + <strong>Current Value:</strong> {currentLanguage} |
| 76 | + </p> |
| 77 | + <p> |
| 78 | + <strong>State Key:</strong> <code>'app.language'</code> |
| 79 | + </p> |
| 80 | + <div style={{ marginTop: '12px', display: 'flex', gap: '8px', flexWrap: 'wrap' }}> |
| 81 | + <Button |
| 82 | + onClick={() => setCurrentLanguage('en')} |
| 83 | + variant={currentLanguage === 'en' ? 'contained' : 'outlined'} |
| 84 | + size="small" |
| 85 | + > |
| 86 | + English |
| 87 | + </Button> |
| 88 | + <Button |
| 89 | + onClick={() => setCurrentLanguage('no')} |
| 90 | + variant={currentLanguage === 'no' ? 'contained' : 'outlined'} |
| 91 | + size="small" |
| 92 | + > |
| 93 | + Norsk |
| 94 | + </Button> |
| 95 | + <Button |
| 96 | + onClick={() => setCurrentLanguage('es')} |
| 97 | + variant={currentLanguage === 'es' ? 'contained' : 'outlined'} |
| 98 | + size="small" |
| 99 | + > |
| 100 | + Español |
| 101 | + </Button> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + |
| 105 | + {/* Optional State Example */} |
| 106 | + <div> |
| 107 | + <h3>Optional State: Last Login Time</h3> |
| 108 | + <p> |
| 109 | + <strong>Current Value:</strong> {lastLoginTime || 'Never logged in'} |
| 110 | + </p> |
| 111 | + <p> |
| 112 | + <strong>State Key:</strong> <code>'user.lastLogin'</code> |
| 113 | + </p> |
| 114 | + <div style={{ marginTop: '12px', display: 'flex', gap: '8px' }}> |
| 115 | + <Button onClick={handleLogin} variant="outlined"> |
| 116 | + Simulate Login |
| 117 | + </Button> |
| 118 | + <Button |
| 119 | + onClick={() => setLastLoginTime(undefined)} |
| 120 | + variant="outlined" |
| 121 | + disabled={!lastLoginTime} |
| 122 | + > |
| 123 | + Clear Login Time |
| 124 | + </Button> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + </section> |
| 129 | + ); |
| 130 | +}; |
0 commit comments