diff --git a/app/src/App.tsx b/app/src/App.tsx index 3130f29..7ab7833 100644 --- a/app/src/App.tsx +++ b/app/src/App.tsx @@ -11,7 +11,7 @@ import { AppState } from './reducers' import { bindActionCreators } from 'redux' import { connect } from 'react-redux' import { default as SplitPane } from 'react-split-pane' -import { globalActions } from './actions' +import { globalActions, settingsActions } from './actions' import { Theme, withStyles } from '@material-ui/core/styles' const Settings = React.lazy(() => import('./components/Settings')) @@ -22,7 +22,8 @@ interface Props { classes: any settingsVisible: boolean error?: string - actions: any + actions: typeof globalActions + settingsActions: typeof settingsActions } class App extends React.PureComponent { @@ -31,6 +32,10 @@ class App extends React.PureComponent { this.state = { } } + public componentDidMount() { + this.props.settingsActions.loadSettings() + } + private renderError() { if (this.props.error) { const error = typeof this.props.error === 'string' ? this.props.error : JSON.stringify(this.props.error) @@ -143,6 +148,7 @@ const styles = (theme: Theme) => { const mapDispatchToProps = (dispatch: any) => { return { actions: bindActionCreators(globalActions, dispatch), + settingsActions: bindActionCreators(settingsActions, dispatch), } } diff --git a/app/src/PersistantStorage.ts b/app/src/PersistantStorage.ts index 2d9be57..e6f7e4c 100644 --- a/app/src/PersistantStorage.ts +++ b/app/src/PersistantStorage.ts @@ -44,7 +44,7 @@ class RemoteStorage implements PersistantStorage { }) } - public store(identifier: StorageIdentifier, data: Model): Promise { + public store(identifier: StorageIdentifier, data: Model): Promise { const transactionId = v4() const expectation = this.expectAck(transactionId) rendererEvents.emit(storageStoreEvent, { data, transactionId, store: identifier.id }) diff --git a/app/src/actions/ConnectionManager.ts b/app/src/actions/ConnectionManager.ts index af1f1f3..0a687d9 100644 --- a/app/src/actions/ConnectionManager.ts +++ b/app/src/actions/ConnectionManager.ts @@ -35,6 +35,7 @@ export const loadConnectionSettings = () => async (dispatch: Dispatch, getS export const saveConnectionSettings = () => async (dispatch: Dispatch, getState: () => AppState) => { try { + console.log('store settings') await persistantStorage.store(storedConnectionsIdentifier, getState().connectionManager.connections) } catch (error) { dispatch(showError(error)) diff --git a/app/src/actions/Settings.ts b/app/src/actions/Settings.ts index 4f401cc..e4144c8 100644 --- a/app/src/actions/Settings.ts +++ b/app/src/actions/Settings.ts @@ -1,37 +1,76 @@ -import { Action, ActionTypes, TopicOrder } from '../reducers/Settings' -import { ActionTypes as TreeActionTypes } from '../reducers/Tree' -import { Dispatch } from 'redux' -import { showTree } from './Tree' -import { AppState } from '../reducers' import * as q from '../../../backend/src/Model' -import { batchActions } from 'redux-batched-actions' +import { AppState } from '../reducers' import { autoExpandLimitSet } from '../components/Settings' +import { batchActions } from 'redux-batched-actions' +import { default as persistantStorage, StorageIdentifier } from '../PersistantStorage' +import { Dispatch } from 'redux' +import { showError } from './Global' +import { showTree } from './Tree' import { TopicViewModel } from '../TopicViewModel' +import { + ActionTypes, + SettingsState, + TopicOrder, +} from '../reducers/Settings' -export const setAutoExpandLimit = (autoExpandLimit: number = 0): Action => { - return { +const settingsIdentifier: StorageIdentifier> = { + id: 'Settings', +} + +export const loadSettings = () => async (dispatch: Dispatch, _getState: () => AppState) => { + try { + const settings = await persistantStorage.load(settingsIdentifier) + dispatch({ + settings, + type: ActionTypes.SETTINGS_DID_LOAD_SETTINGS, + }) + } catch (error) { + dispatch(showError(error)) + } +} + +export const storeSettings = () => async (dispatch: Dispatch, getState: () => AppState) => { + const settings = { + ...getState().settings, + topicFilter: undefined, + visible: undefined, + } + + try { + await persistantStorage.store(settingsIdentifier, settings) + } catch (error) { + dispatch(showError(error)) + } +} + +export const setAutoExpandLimit = (autoExpandLimit: number = 0) => (dispatch: Dispatch, getState: () => AppState) => { + dispatch({ autoExpandLimit, type: ActionTypes.SETTINGS_SET_AUTO_EXPAND_LIMIT, - } + }) + dispatch(storeSettings()) } -export const toggleSettingsVisibility = (): Action => { - return { +export const toggleSettingsVisibility = () => (dispatch: Dispatch, _getState: () => AppState) => { + dispatch({ type: ActionTypes.SETTINGS_TOGGLE_VISIBILITY, - } + }) + dispatch(storeSettings()) } -export const togglehighlightTopicUpdates = (): Action => { - return { +export const togglehighlightTopicUpdates = () => (dispatch: Dispatch, _getState: () => AppState) => { + dispatch({ type: ActionTypes.SETTINGS_TOGGLE_HIGHLIGHT_ACTIVITY, - } + }) + dispatch(storeSettings()) } -export const setTopicOrder = (topicOrder: TopicOrder = TopicOrder.none): Action => { - return { +export const setTopicOrder = (topicOrder: TopicOrder = TopicOrder.none) => (dispatch: Dispatch, _getState: () => AppState) => { + dispatch({ topicOrder, type: ActionTypes.SETTINGS_SET_TOPIC_ORDER, - } + }) + dispatch(storeSettings()) } export const filterTopics = (filterStr: string) => (dispatch: Dispatch, getState: () => AppState) => { diff --git a/app/src/reducers/Settings.ts b/app/src/reducers/Settings.ts index 9a35850..80e7b7b 100644 --- a/app/src/reducers/Settings.ts +++ b/app/src/reducers/Settings.ts @@ -23,6 +23,7 @@ export enum ActionTypes { SETTINGS_SET_TOPIC_ORDER = 'SETTINGS_SET_TOPIC_ORDER', SETTINGS_FILTER_TOPICS = 'SETTINGS_FILTER_TOPICS', SETTINGS_TOGGLE_HIGHLIGHT_ACTIVITY = 'SETTINGS_TOGGLE_HIGHLIGHT_ACTIVITY', + SETTINGS_DID_LOAD_SETTINGS = 'SETTINGS_DID_LOAD_SETTINGS', } const initialState: SettingsState = { @@ -38,8 +39,21 @@ export const settingsReducer = createReducer(initialState, { SETTINGS_SET_TOPIC_ORDER: setTopicOrder, SETTINGS_FILTER_TOPICS: filterTopics, SETTINGS_TOGGLE_HIGHLIGHT_ACTIVITY: togglehighlightTopicUpdates, + SETTINGS_DID_LOAD_SETTINGS: didLoadSettings, }) +export interface DidLoadSettings { + type: ActionTypes.SETTINGS_DID_LOAD_SETTINGS + settings: Partial +} + +function didLoadSettings(state: SettingsState, action: DidLoadSettings) { + return { + ...state, + ...action.settings, + } +} + export interface SetAutoExpandLimit { type: ActionTypes.SETTINGS_SET_AUTO_EXPAND_LIMIT autoExpandLimit: number diff --git a/backend/src/ConfigStorage.ts b/backend/src/ConfigStorage.ts index 5e247cd..c55f245 100644 --- a/backend/src/ConfigStorage.ts +++ b/backend/src/ConfigStorage.ts @@ -33,8 +33,8 @@ export default class ConfigStorage { await db.set(event.store, event.data).write() backendEvents.emit(ack, undefined) } catch (error) { - console.error(error) backendEvents.emit(ack, { error, transactionId: event.transactionId, store: event.store }) + throw error } }) @@ -45,8 +45,8 @@ export default class ConfigStorage { const data = await db.get(event.store).value() backendEvents.emit(responseEvent, { data, transactionId: event.transactionId, store: event.store }) } catch (error) { - console.error(error) backendEvents.emit(responseEvent, { error, transactionId: event.transactionId, store: event.store }) + throw error } }) @@ -60,8 +60,8 @@ export default class ConfigStorage { backendEvents.emit(makeStorageAcknoledgementEvent(event.transactionId), undefined) } catch (error) { backendEvents.emit(makeStorageAcknoledgementEvent(event.transactionId), { error, transactionId: event.transactionId }) + throw error } - }) } }