swimtracker-app/state/Reducer.js

76 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-06-02 17:19:09 +02:00
import { combineReducers } from 'redux';
import { List } from 'immutable';
2020-06-02 22:43:48 +02:00
import { CHANGE_THEME, CHANGE_USER_NAME, NEW_DEVICE_DATA, START_SESSION, STOP_SESSION } from './ActionCreators';
2020-06-02 17:19:09 +02:00
const INITIAL_SETTINGS = {
theme: "hot",
username: "",
2020-06-02 22:43:48 +02:00
deviceURL: "http://192.168.178.105",
2020-06-02 17:19:09 +02:00
peaksPerLap: 30,
// advanced
peakDetector: 'SIMPLE', // either 'SIMPLE' or 'ZSCORE'
2020-06-02 22:43:48 +02:00
peakDetectorSimpleThreshold: 2000,
2020-06-02 17:19:09 +02:00
peakDetectorZScoreLag: 8, // peak detector z-score values
peakDetectorZScoreThreshold: 2,
peakDetectorZScoreInfluence: 0.1,
};
const INITIAL_CURRENT_SESSION = {
running: false,
rawData: List(),
analysis: {
'peaks': List(),
'totalTime': null,
'activeTime': null,
'totalMomentum': null,
'peakFrequency': null,
'peakMax': null,
// windowed quantities
'momentumWindow': null,
'frequencyWindow': null,
'peakMaxWindow': null,
}
};
const settingsReducer = (state = INITIAL_SETTINGS, action) => {
switch (action.type) {
case CHANGE_THEME:
return { ...state, theme: action.newThemeName };
case CHANGE_USER_NAME:
return { ...state, username: action.newUsername };
default:
return state
}
};
const currentSessionReducer = (state = INITIAL_CURRENT_SESSION, action) => {
switch (action.type) {
case START_SESSION:
return {
running: true,
rawData: List(),
analysis: INITIAL_CURRENT_SESSION.analysis
};
2020-06-02 22:43:48 +02:00
case STOP_SESSION:
return {
running: false,
rawData: List(),
analysis: INITIAL_CURRENT_SESSION.analysis
};
2020-06-02 17:19:09 +02:00
case NEW_DEVICE_DATA:
return {
2020-06-02 22:43:48 +02:00
running: action.data.size > 0,
2020-06-02 17:19:09 +02:00
rawData: action.data,
2020-06-02 22:43:48 +02:00
analysis: { ...state.analysis, ...action.analysis },
2020-06-02 17:19:09 +02:00
}
default:
return state
}
};
export default combineReducers({
settings: settingsReducer,
session: currentSessionReducer,
});