| 1 |
/** |
| 2 |
* Settings Data Store |
| 3 |
* |
| 4 |
* Manages plugin settings and configuration |
| 5 |
* |
| 6 |
* @package ThinkRank |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
import { createReduxStore } from '@wordpress/data'; |
| 11 |
import apiFetch from '@wordpress/api-fetch'; |
| 12 |
|
| 13 |
// Initial state |
| 14 |
const DEFAULT_STATE = { |
| 15 |
settings: {}, |
| 16 |
isLoading: false, |
| 17 |
isSaving: false, |
| 18 |
lastSaved: null, |
| 19 |
}; |
| 20 |
|
| 21 |
// Action types |
| 22 |
const TYPES = { |
| 23 |
SET_SETTINGS: 'SET_SETTINGS', |
| 24 |
SET_LOADING: 'SET_LOADING', |
| 25 |
SET_SAVING: 'SET_SAVING', |
| 26 |
UPDATE_SETTING: 'UPDATE_SETTING', |
| 27 |
SET_LAST_SAVED: 'SET_LAST_SAVED', |
| 28 |
}; |
| 29 |
|
| 30 |
// Reducer |
| 31 |
const reducer = (state = DEFAULT_STATE, action) => { |
| 32 |
switch (action.type) { |
| 33 |
case TYPES.SET_SETTINGS: |
| 34 |
return { |
| 35 |
...state, |
| 36 |
settings: action.settings, |
| 37 |
}; |
| 38 |
|
| 39 |
case TYPES.SET_LOADING: |
| 40 |
return { |
| 41 |
...state, |
| 42 |
isLoading: action.loading, |
| 43 |
}; |
| 44 |
|
| 45 |
case TYPES.SET_SAVING: |
| 46 |
return { |
| 47 |
...state, |
| 48 |
isSaving: action.saving, |
| 49 |
}; |
| 50 |
|
| 51 |
case TYPES.UPDATE_SETTING: |
| 52 |
return { |
| 53 |
...state, |
| 54 |
settings: { |
| 55 |
...state.settings, |
| 56 |
[action.key]: action.value, |
| 57 |
}, |
| 58 |
}; |
| 59 |
|
| 60 |
case TYPES.SET_LAST_SAVED: |
| 61 |
return { |
| 62 |
...state, |
| 63 |
lastSaved: action.timestamp, |
| 64 |
}; |
| 65 |
|
| 66 |
default: |
| 67 |
return state; |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
// Actions |
| 72 |
const actions = { |
| 73 |
setSettings(settings) { |
| 74 |
return { |
| 75 |
type: TYPES.SET_SETTINGS, |
| 76 |
settings, |
| 77 |
}; |
| 78 |
}, |
| 79 |
|
| 80 |
setLoading(loading) { |
| 81 |
return { |
| 82 |
type: TYPES.SET_LOADING, |
| 83 |
loading, |
| 84 |
}; |
| 85 |
}, |
| 86 |
|
| 87 |
setSaving(saving) { |
| 88 |
return { |
| 89 |
type: TYPES.SET_SAVING, |
| 90 |
saving, |
| 91 |
}; |
| 92 |
}, |
| 93 |
|
| 94 |
updateSetting(key, value) { |
| 95 |
return { |
| 96 |
type: TYPES.UPDATE_SETTING, |
| 97 |
key, |
| 98 |
value, |
| 99 |
}; |
| 100 |
}, |
| 101 |
|
| 102 |
setLastSaved(timestamp) { |
| 103 |
return { |
| 104 |
type: TYPES.SET_LAST_SAVED, |
| 105 |
timestamp, |
| 106 |
}; |
| 107 |
}, |
| 108 |
|
| 109 |
// Async actions |
| 110 |
*loadSettings() { |
| 111 |
yield actions.setLoading(true); |
| 112 |
|
| 113 |
try { |
| 114 |
const settings = yield apiFetch({ |
| 115 |
path: '/thinkrank/v1/settings', |
| 116 |
}); |
| 117 |
|
| 118 |
yield actions.setSettings(settings || {}); |
| 119 |
|
| 120 |
} catch (error) { |
| 121 |
console.error('Failed to load settings:', error); |
| 122 |
// Set default settings on error |
| 123 |
yield actions.setSettings({ |
| 124 |
ai_provider: 'openai', |
| 125 |
openai_api_key: '', |
| 126 |
claude_api_key: '', |
| 127 |
max_tokens: 1000, |
| 128 |
temperature: 0.7, |
| 129 |
cache_duration: 3600, |
| 130 |
}); |
| 131 |
} finally { |
| 132 |
yield actions.setLoading(false); |
| 133 |
} |
| 134 |
}, |
| 135 |
|
| 136 |
*saveSettings(settingsToSave = null) { |
| 137 |
yield actions.setSaving(true); |
| 138 |
|
| 139 |
try { |
| 140 |
const currentSettings = yield wp.data.select('thinkrank/settings').getSettings(); |
| 141 |
const settings = settingsToSave || currentSettings; |
| 142 |
|
| 143 |
const response = yield apiFetch({ |
| 144 |
path: '/thinkrank/v1/settings', |
| 145 |
method: 'POST', |
| 146 |
data: settings, |
| 147 |
}); |
| 148 |
|
| 149 |
if (response.success) { |
| 150 |
yield actions.setSettings(response.settings); |
| 151 |
yield actions.setLastSaved(Date.now()); |
| 152 |
return response; |
| 153 |
} else { |
| 154 |
throw new Error(response.message || 'Failed to save settings'); |
| 155 |
} |
| 156 |
|
| 157 |
} catch (error) { |
| 158 |
console.error('Failed to save settings:', error); |
| 159 |
throw error; |
| 160 |
} finally { |
| 161 |
yield actions.setSaving(false); |
| 162 |
} |
| 163 |
}, |
| 164 |
}; |
| 165 |
|
| 166 |
// Selectors |
| 167 |
const selectors = { |
| 168 |
getSettings(state) { |
| 169 |
return state.settings; |
| 170 |
}, |
| 171 |
|
| 172 |
getSetting(state, key, defaultValue = null) { |
| 173 |
return state.settings[key] ?? defaultValue; |
| 174 |
}, |
| 175 |
|
| 176 |
getIsLoading(state) { |
| 177 |
return state.isLoading; |
| 178 |
}, |
| 179 |
|
| 180 |
getIsSaving(state) { |
| 181 |
return state.isSaving; |
| 182 |
}, |
| 183 |
|
| 184 |
getLastSaved(state) { |
| 185 |
return state.lastSaved; |
| 186 |
}, |
| 187 |
|
| 188 |
// Computed selectors |
| 189 |
hasUnsavedChanges(state) { |
| 190 |
// This would need to track original vs current settings |
| 191 |
// For now, return false |
| 192 |
return false; |
| 193 |
}, |
| 194 |
|
| 195 |
getApiKey(state) { |
| 196 |
return state.settings.openai_api_key || ''; |
| 197 |
}, |
| 198 |
|
| 199 |
getAiProvider(state) { |
| 200 |
return state.settings.ai_provider || 'openai'; |
| 201 |
}, |
| 202 |
}; |
| 203 |
|
| 204 |
// Create and export store |
| 205 |
const store = createReduxStore('thinkrank/settings', { |
| 206 |
reducer, |
| 207 |
actions, |
| 208 |
selectors, |
| 209 |
}); |
| 210 |
|
| 211 |
export default store; |
| 212 |
|