2023-03-07 00:56:35 +01:00
|
|
|
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
|
|
|
|
import { RootState, AppThunk } from '../../app/store';
|
|
|
|
import { RadioState, ConnectionState } from '../../app/types';
|
2023-03-13 23:41:23 +01:00
|
|
|
import { requestToStart, requestToStop, requestLogs } from './radioAPI';
|
|
|
|
|
|
|
|
export interface LoadingState {
|
|
|
|
radio: boolean,
|
|
|
|
logs: boolean,
|
|
|
|
}
|
2023-03-07 00:56:35 +01:00
|
|
|
|
|
|
|
export interface AppState {
|
|
|
|
radio?: RadioState,
|
2023-03-13 23:41:23 +01:00
|
|
|
logs?: Array<string>,
|
|
|
|
loading: LoadingState,
|
2023-03-07 00:56:35 +01:00
|
|
|
connection: ConnectionState
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialState: AppState = {
|
|
|
|
radio: undefined,
|
2023-03-13 23:41:23 +01:00
|
|
|
loading: { radio: false, logs: false },
|
2023-03-07 00:56:35 +01:00
|
|
|
connection: ConnectionState.DISCONNECTED,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const requestToStartThunk = createAsyncThunk(
|
|
|
|
'radio/restStart',
|
|
|
|
async () => {
|
|
|
|
const success = await requestToStart();
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const requestToStopThunk = createAsyncThunk(
|
|
|
|
'radio/restStop',
|
|
|
|
async () => {
|
|
|
|
const success = await requestToStop();
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-03-13 23:41:23 +01:00
|
|
|
export const requestLogsThunk = createAsyncThunk(
|
|
|
|
'radio/restStatus',
|
|
|
|
async () => {
|
|
|
|
return await requestLogs();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-03-07 00:56:35 +01:00
|
|
|
export const radioSlice = createSlice({
|
|
|
|
name: 'radio',
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
2023-03-13 23:41:23 +01:00
|
|
|
toggleRadioLoading: (state) => { state.loading.radio = !state.loading.radio },
|
|
|
|
toggleLogsLoading: (state) => { state.loading.logs = !state.loading.logs },
|
2023-03-07 00:56:35 +01:00
|
|
|
setRadioStarted: (state) => { console.log("[Redux] STARTED"); state.radio = RadioState.STARTED },
|
|
|
|
setRadioStopped: (state) => { console.log("[Redux] STOPPED"); state.radio = RadioState.STOPPED },
|
|
|
|
unsetRadio: (state) => { state.radio = undefined },
|
|
|
|
toggleRadio: (state) => {
|
|
|
|
if (state.radio === RadioState.STARTED) {
|
|
|
|
state.radio = RadioState.STOPPED
|
|
|
|
} else if (state.radio === RadioState.STOPPED) {
|
|
|
|
state.radio = RadioState.STARTED
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setConnected: (state) => { state.connection = ConnectionState.CONNECTED },
|
|
|
|
setDisconnected: (state) => { state.connection = ConnectionState.DISCONNECTED },
|
|
|
|
toggleConnection: (state) => {
|
|
|
|
if (state.connection === ConnectionState.CONNECTED) {
|
|
|
|
state.connection = ConnectionState.DISCONNECTED
|
|
|
|
} else if (state.connection === ConnectionState.DISCONNECTED) {
|
|
|
|
state.connection = ConnectionState.CONNECTED
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
extraReducers: builder => {
|
|
|
|
builder
|
|
|
|
.addCase(requestToStartThunk.pending, (state) => {
|
2023-03-13 23:41:23 +01:00
|
|
|
state.loading.radio = true;
|
2023-03-07 00:56:35 +01:00
|
|
|
return state;
|
|
|
|
})
|
|
|
|
.addCase(requestToStartThunk.fulfilled, (state, action) => {
|
2023-03-13 23:41:23 +01:00
|
|
|
state.loading.radio = false;
|
2023-03-07 00:56:35 +01:00
|
|
|
if (action.payload) {
|
|
|
|
state.radio = RadioState.STARTED;
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
})
|
|
|
|
.addCase(requestToStopThunk.pending, (state, _) => {
|
2023-03-13 23:41:23 +01:00
|
|
|
state.loading.radio = true;
|
2023-03-07 00:56:35 +01:00
|
|
|
return state;
|
|
|
|
})
|
|
|
|
.addCase(requestToStopThunk.fulfilled, (state, action) => {
|
2023-03-13 23:41:23 +01:00
|
|
|
state.loading.radio = false;
|
2023-03-07 00:56:35 +01:00
|
|
|
if (action.payload) {
|
|
|
|
state.radio = RadioState.STOPPED;
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
})
|
2023-03-13 23:41:23 +01:00
|
|
|
.addCase(requestLogsThunk.pending, (state, _) => {
|
|
|
|
state.loading.logs = true;
|
|
|
|
return state;
|
|
|
|
})
|
|
|
|
.addCase(requestLogsThunk.fulfilled, (state, action) => {
|
|
|
|
state.loading.logs = false;
|
|
|
|
state.logs = action.payload;
|
|
|
|
return state;
|
|
|
|
})
|
2023-03-07 00:56:35 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const {
|
2023-03-13 23:41:23 +01:00
|
|
|
toggleRadioLoading,
|
|
|
|
toggleLogsLoading,
|
2023-03-07 00:56:35 +01:00
|
|
|
setRadioStarted,
|
|
|
|
setRadioStopped,
|
|
|
|
unsetRadio,
|
|
|
|
toggleRadio,
|
|
|
|
setConnected,
|
|
|
|
setDisconnected,
|
|
|
|
toggleConnection,
|
|
|
|
} = radioSlice.actions;
|
|
|
|
|
2023-03-13 23:41:23 +01:00
|
|
|
export const selectRadioLoading = (state: RootState) => state.globalState.loading.radio;
|
|
|
|
export const selectLogsLoading = (state: RootState) => state.globalState.loading.logs;
|
2023-03-07 00:56:35 +01:00
|
|
|
export const selectRadio = (state: RootState) => state.globalState.radio;
|
2023-03-13 23:41:23 +01:00
|
|
|
export const selectLogs = (state: RootState) => state.globalState.logs;
|
2023-03-07 00:56:35 +01:00
|
|
|
export const selectConnection = (state: RootState) => state.globalState.connection;
|
|
|
|
|
|
|
|
export const toggleRadioState =
|
|
|
|
(): AppThunk =>
|
|
|
|
async (dispatch, getState) => {
|
|
|
|
const currentRadioState = selectRadio(getState());
|
|
|
|
if (currentRadioState === RadioState.STARTED) {
|
|
|
|
dispatch(requestToStopThunk());
|
|
|
|
} else if (currentRadioState === RadioState.STOPPED) {
|
|
|
|
dispatch(requestToStartThunk());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default radioSlice.reducer
|