Add main ui logic
This commit is contained in:
parent
a9c604102b
commit
8fa7247835
7
Dockerfile.dev
Normal file
7
Dockerfile.dev
Normal file
|
@ -0,0 +1,7 @@
|
|||
FROM golang:1.20
|
||||
|
||||
WORKDIR /src
|
||||
COPY . /src
|
||||
RUN go build -o / ./cmd/...
|
||||
|
||||
ENTRYPOINT /broadcast -debug /echo
|
28
devloop/nginx/default.conf
Normal file
28
devloop/nginx/default.conf
Normal file
|
@ -0,0 +1,28 @@
|
|||
server {
|
||||
listen 80 default_server;
|
||||
|
||||
location / {
|
||||
proxy_pass http://ui:3000;
|
||||
}
|
||||
|
||||
# needed for hot module reloading
|
||||
location /ws {
|
||||
proxy_pass http://ui:3000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "Upgrade";
|
||||
proxy_set_header Host $host;
|
||||
}
|
||||
|
||||
location /liveness {
|
||||
proxy_pass http://api:8080;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "Upgrade";
|
||||
proxy_set_header Host $host;
|
||||
}
|
||||
|
||||
location ~* /(start|stop|status) {
|
||||
proxy_pass http://api:8080;
|
||||
}
|
||||
}
|
31
docker-compose.yml
Normal file
31
docker-compose.yml
Normal file
|
@ -0,0 +1,31 @@
|
|||
version: '3'
|
||||
|
||||
services:
|
||||
api:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile.dev
|
||||
ports:
|
||||
- 8080:8080
|
||||
|
||||
ui:
|
||||
image: node
|
||||
volumes:
|
||||
- ./ui:/src
|
||||
ports:
|
||||
- 3000:3000
|
||||
environment:
|
||||
WDS_SOCKET_HOST: 0.0.0.0
|
||||
WDS_SOCKET_PORT: 0
|
||||
working_dir: /src
|
||||
command: yarn start
|
||||
|
||||
proxy:
|
||||
image: nginx
|
||||
volumes:
|
||||
- ./devloop/nginx:/etc/nginx/conf.d
|
||||
depends_on:
|
||||
- api
|
||||
- ui
|
||||
ports:
|
||||
- 80:80
|
23
ui/.gitignore
vendored
Normal file
23
ui/.gitignore
vendored
Normal file
|
@ -0,0 +1,23 @@
|
|||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
46
ui/README.md
Normal file
46
ui/README.md
Normal file
|
@ -0,0 +1,46 @@
|
|||
# Getting Started with Create React App
|
||||
|
||||
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app), using the [Redux](https://redux.js.org/) and [Redux Toolkit](https://redux-toolkit.js.org/) TS template.
|
||||
|
||||
## Available Scripts
|
||||
|
||||
In the project directory, you can run:
|
||||
|
||||
### `yarn start`
|
||||
|
||||
Runs the app in the development mode.\
|
||||
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
|
||||
|
||||
The page will reload if you make edits.\
|
||||
You will also see any lint errors in the console.
|
||||
|
||||
### `yarn test`
|
||||
|
||||
Launches the test runner in the interactive watch mode.\
|
||||
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
|
||||
|
||||
### `yarn build`
|
||||
|
||||
Builds the app for production to the `build` folder.\
|
||||
It correctly bundles React in production mode and optimizes the build for the best performance.
|
||||
|
||||
The build is minified and the filenames include the hashes.\
|
||||
Your app is ready to be deployed!
|
||||
|
||||
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
|
||||
|
||||
### `yarn eject`
|
||||
|
||||
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
|
||||
|
||||
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
|
||||
|
||||
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
|
||||
|
||||
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
|
||||
|
||||
## Learn More
|
||||
|
||||
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
|
||||
|
||||
To learn React, check out the [React documentation](https://reactjs.org/).
|
46
ui/package.json
Normal file
46
ui/package.json
Normal file
|
@ -0,0 +1,46 @@
|
|||
{
|
||||
"name": "ui",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@reduxjs/toolkit": "^1.8.1",
|
||||
"@testing-library/jest-dom": "^5.16.4",
|
||||
"@testing-library/react": "^13.0.1",
|
||||
"@testing-library/user-event": "^14.1.1",
|
||||
"@types/jest": "^27.4.1",
|
||||
"@types/node": "^17.0.25",
|
||||
"@types/react": "^18.0.6",
|
||||
"@types/react-dom": "^18.0.2",
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0",
|
||||
"react-redux": "^8.0.1",
|
||||
"react-scripts": "5.0.1",
|
||||
"react-use-websocket": "^4.3.1",
|
||||
"typescript": "^4.6.0",
|
||||
"web-vitals": "^2.1.0"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "react-scripts start",
|
||||
"build": "react-scripts build",
|
||||
"test": "react-scripts test",
|
||||
"eject": "react-scripts eject"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": [
|
||||
"react-app",
|
||||
"react-app/jest"
|
||||
]
|
||||
},
|
||||
"browserslist": {
|
||||
"production": [
|
||||
">0.2%",
|
||||
"not dead",
|
||||
"not op_mini all"
|
||||
],
|
||||
"development": [
|
||||
"last 1 chrome version",
|
||||
"last 1 firefox version",
|
||||
"last 1 safari version"
|
||||
]
|
||||
}
|
||||
}
|
BIN
ui/public/favicon.ico
Normal file
BIN
ui/public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.5 KiB |
43
ui/public/index.html
Normal file
43
ui/public/index.html
Normal file
|
@ -0,0 +1,43 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta name="theme-color" content="#000000" />
|
||||
<meta
|
||||
name="description"
|
||||
content="Web site created using create-react-app"
|
||||
/>
|
||||
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
|
||||
<!--
|
||||
manifest.json provides metadata used when your web app is installed on a
|
||||
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
|
||||
-->
|
||||
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
|
||||
<!--
|
||||
Notice the use of %PUBLIC_URL% in the tags above.
|
||||
It will be replaced with the URL of the `public` folder during the build.
|
||||
Only files inside the `public` folder can be referenced from the HTML.
|
||||
|
||||
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
|
||||
work correctly both with client-side routing and a non-root public URL.
|
||||
Learn how to configure a non-root public URL by running `npm run build`.
|
||||
-->
|
||||
<title>React Redux App</title>
|
||||
</head>
|
||||
<body>
|
||||
<noscript>You need to enable JavaScript to run this app.</noscript>
|
||||
<div id="root"></div>
|
||||
<!--
|
||||
This HTML file is a template.
|
||||
If you open it directly in the browser, you will see an empty page.
|
||||
|
||||
You can add webfonts, meta tags, or analytics to this file.
|
||||
The build step will place the bundled scripts into the <body> tag.
|
||||
|
||||
To begin the development, run `npm start` or `yarn start`.
|
||||
To create a production bundle, use `npm run build` or `yarn build`.
|
||||
-->
|
||||
</body>
|
||||
</html>
|
BIN
ui/public/logo192.png
Normal file
BIN
ui/public/logo192.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.1 KiB |
BIN
ui/public/logo512.png
Normal file
BIN
ui/public/logo512.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
25
ui/public/manifest.json
Normal file
25
ui/public/manifest.json
Normal file
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"short_name": "React App",
|
||||
"name": "Create React App Sample",
|
||||
"icons": [
|
||||
{
|
||||
"src": "favicon.ico",
|
||||
"sizes": "64x64 32x32 24x24 16x16",
|
||||
"type": "image/x-icon"
|
||||
},
|
||||
{
|
||||
"src": "logo192.png",
|
||||
"type": "image/png",
|
||||
"sizes": "192x192"
|
||||
},
|
||||
{
|
||||
"src": "logo512.png",
|
||||
"type": "image/png",
|
||||
"sizes": "512x512"
|
||||
}
|
||||
],
|
||||
"start_url": ".",
|
||||
"display": "standalone",
|
||||
"theme_color": "#000000",
|
||||
"background_color": "#ffffff"
|
||||
}
|
3
ui/public/robots.txt
Normal file
3
ui/public/robots.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
# https://www.robotstxt.org/robotstxt.html
|
||||
User-agent: *
|
||||
Disallow:
|
39
ui/src/App.css
Normal file
39
ui/src/App.css
Normal file
|
@ -0,0 +1,39 @@
|
|||
.App {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.App-logo {
|
||||
height: 40vmin;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: no-preference) {
|
||||
.App-logo {
|
||||
animation: App-logo-float infinite 3s ease-in-out;
|
||||
}
|
||||
}
|
||||
|
||||
.App-header {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: calc(10px + 2vmin);
|
||||
}
|
||||
|
||||
.App-link {
|
||||
color: rgb(112, 76, 182);
|
||||
}
|
||||
|
||||
@keyframes App-logo-float {
|
||||
0% {
|
||||
transform: translateY(0);
|
||||
}
|
||||
50% {
|
||||
transform: translateY(10px);
|
||||
}
|
||||
100% {
|
||||
transform: translateY(0px);
|
||||
}
|
||||
}
|
15
ui/src/App.test.tsx
Normal file
15
ui/src/App.test.tsx
Normal file
|
@ -0,0 +1,15 @@
|
|||
import React from 'react';
|
||||
import { render } from '@testing-library/react';
|
||||
import { Provider } from 'react-redux';
|
||||
import { store } from './app/store';
|
||||
import App from './App';
|
||||
|
||||
test('renders learn react link', () => {
|
||||
const { getByText } = render(
|
||||
<Provider store={store}>
|
||||
<App />
|
||||
</Provider>
|
||||
);
|
||||
|
||||
expect(getByText(/learn/i)).toBeInTheDocument();
|
||||
});
|
17
ui/src/App.tsx
Normal file
17
ui/src/App.tsx
Normal file
|
@ -0,0 +1,17 @@
|
|||
import React from 'react';
|
||||
import logo from './logo.svg';
|
||||
import { RadioToggler } from './features/radio/Radio';
|
||||
import './App.css';
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<div className="App">
|
||||
<header className="App-header">
|
||||
<img src={logo} className="App-logo" alt="logo" />
|
||||
<RadioToggler />
|
||||
</header>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
6
ui/src/app/hooks.ts
Normal file
6
ui/src/app/hooks.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
|
||||
import type { RootState, AppDispatch } from './store';
|
||||
|
||||
// Use throughout your app instead of plain `useDispatch` and `useSelector`
|
||||
export const useAppDispatch = () => useDispatch<AppDispatch>();
|
||||
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
|
17
ui/src/app/store.ts
Normal file
17
ui/src/app/store.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import { configureStore, ThunkAction, Action } from '@reduxjs/toolkit';
|
||||
import radioReducer from '../features/radio/radioSlice';
|
||||
|
||||
export const store = configureStore({
|
||||
reducer: {
|
||||
globalState: radioReducer,
|
||||
},
|
||||
});
|
||||
|
||||
export type AppDispatch = typeof store.dispatch;
|
||||
export type RootState = ReturnType<typeof store.getState>;
|
||||
export type AppThunk<ReturnType = void> = ThunkAction<
|
||||
ReturnType,
|
||||
RootState,
|
||||
unknown,
|
||||
Action<string>
|
||||
>;
|
9
ui/src/app/types.ts
Normal file
9
ui/src/app/types.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
export enum RadioState {
|
||||
STARTED = "STARTED",
|
||||
STOPPED = "STOPPED",
|
||||
}
|
||||
|
||||
export enum ConnectionState {
|
||||
CONNECTED = "CONNECTED",
|
||||
DISCONNECTED = "DISCONNECTED",
|
||||
}
|
79
ui/src/features/radio/Radio.module.css
Normal file
79
ui/src/features/radio/Radio.module.css
Normal file
|
@ -0,0 +1,79 @@
|
|||
.row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.row > button {
|
||||
margin-left: 4px;
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
.row:not(:last-child) {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.value {
|
||||
font-size: 78px;
|
||||
padding-left: 16px;
|
||||
padding-right: 16px;
|
||||
margin-top: 2px;
|
||||
font-family: 'Courier New', Courier, monospace;
|
||||
}
|
||||
|
||||
.button {
|
||||
appearance: none;
|
||||
background: none;
|
||||
font-size: 32px;
|
||||
padding-left: 12px;
|
||||
padding-right: 12px;
|
||||
outline: none;
|
||||
border: 2px solid transparent;
|
||||
color: rgb(112, 76, 182);
|
||||
padding-bottom: 4px;
|
||||
cursor: pointer;
|
||||
background-color: rgba(112, 76, 182, 0.1);
|
||||
border-radius: 2px;
|
||||
transition: all 0.15s;
|
||||
}
|
||||
|
||||
.textbox {
|
||||
font-size: 32px;
|
||||
padding: 2px;
|
||||
width: 64px;
|
||||
text-align: center;
|
||||
margin-right: 4px;
|
||||
}
|
||||
|
||||
.button:hover,
|
||||
.button:focus {
|
||||
border: 2px solid rgba(112, 76, 182, 0.4);
|
||||
}
|
||||
|
||||
.button:active {
|
||||
background-color: rgba(112, 76, 182, 0.2);
|
||||
}
|
||||
|
||||
.asyncButton {
|
||||
composes: button;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.asyncButton:after {
|
||||
content: '';
|
||||
background-color: rgba(112, 76, 182, 0.15);
|
||||
display: block;
|
||||
position: absolute;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
left: 0;
|
||||
top: 0;
|
||||
opacity: 0;
|
||||
transition: width 1s linear, opacity 0.5s ease 1s;
|
||||
}
|
||||
|
||||
.asyncButton:active:after {
|
||||
width: 0%;
|
||||
opacity: 1;
|
||||
transition: 0s;
|
||||
}
|
65
ui/src/features/radio/Radio.tsx
Normal file
65
ui/src/features/radio/Radio.tsx
Normal file
|
@ -0,0 +1,65 @@
|
|||
import React, { useEffect } from 'react';
|
||||
import useWebSocket from 'react-use-websocket';
|
||||
|
||||
import { useAppSelector, useAppDispatch } from '../../app/hooks';
|
||||
import { RadioState, ConnectionState } from '../../app/types';
|
||||
import {
|
||||
setRadioStarted,
|
||||
setRadioStopped,
|
||||
toggleRadioState,
|
||||
setConnected,
|
||||
setDisconnected,
|
||||
toggleConnection,
|
||||
selectLoading,
|
||||
selectRadio,
|
||||
selectConnection,
|
||||
} from './radioSlice';
|
||||
import {
|
||||
getOnWSOpen,
|
||||
getOnWSClose,
|
||||
getOnWSMessage,
|
||||
} from './radioWS';
|
||||
|
||||
const getWSUrl = async () => {
|
||||
const wsUrl = process.env.WS_ADDR ? process.env.WS_ADDR : "ws://localhost/liveness";
|
||||
console.log("WS ADDR: %s", wsUrl);
|
||||
return wsUrl;
|
||||
};
|
||||
|
||||
export function RadioToggler() {
|
||||
const loadingState = useAppSelector(selectLoading);
|
||||
const radioState = useAppSelector(selectRadio);
|
||||
const connectionState = useAppSelector(selectConnection);
|
||||
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
const { lastJsonMessage } = useWebSocket(getWSUrl,
|
||||
{
|
||||
onOpen: getOnWSOpen(dispatch),
|
||||
onClose: getOnWSClose(dispatch),
|
||||
});
|
||||
|
||||
const onWSMessage = getOnWSMessage(dispatch);
|
||||
useEffect(() => {
|
||||
if (lastJsonMessage != null) {
|
||||
onWSMessage(lastJsonMessage);
|
||||
}
|
||||
});
|
||||
|
||||
if (loadingState) {
|
||||
return <div>Loading...</div>;
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div>
|
||||
<button
|
||||
aria-label="toggle-radio-state"
|
||||
onClick={() => dispatch(toggleRadioState())}
|
||||
>TOGGLE</button>
|
||||
<div>Radio: {radioState}</div>
|
||||
<div>Connection: {connectionState}</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
48
ui/src/features/radio/radioAPI.ts
Normal file
48
ui/src/features/radio/radioAPI.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
import { RadioState } from '../../app/types';
|
||||
|
||||
export const requestToStart = async () => {
|
||||
console.log("[REST] request to start");
|
||||
|
||||
let success = false;
|
||||
|
||||
try {
|
||||
const startUrl = window.location.href + 'start';
|
||||
const resp = await fetch(startUrl, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ "change": true, "newState": RadioState.STARTED }),
|
||||
});
|
||||
|
||||
if (resp.ok) {
|
||||
console.log("[REST] Start: success");
|
||||
success = true;
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("[REST] Start: failure: %s", err);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
export const requestToStop = async () => {
|
||||
console.log("[REST] request to stop");
|
||||
|
||||
let success = false;
|
||||
|
||||
try {
|
||||
const stopUrl = window.location.href + 'stop';
|
||||
const resp = await fetch(stopUrl, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ "change": true, "newState": RadioState.STOPPED }),
|
||||
});
|
||||
|
||||
if (resp.ok) {
|
||||
console.log("[REST] Stop: success");
|
||||
success = true;
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("[REST] Stop: failure: %s", err);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
75
ui/src/features/radio/radioSlice.spec.ts
Normal file
75
ui/src/features/radio/radioSlice.spec.ts
Normal file
|
@ -0,0 +1,75 @@
|
|||
import { RadioState, ConnectionState } from '../../app/types';
|
||||
import reducer, {
|
||||
AppState,
|
||||
toggleLoading,
|
||||
setRadioStarted,
|
||||
setRadioStopped,
|
||||
toggleRadio,
|
||||
setConnected,
|
||||
setDisconnected,
|
||||
toggleConnection,
|
||||
selectLoading,
|
||||
selectRadio,
|
||||
selectConnection,
|
||||
} from './radioSlice';
|
||||
|
||||
describe('radio reducer', () => {
|
||||
const initialState: AppState = {
|
||||
loading: false,
|
||||
connection: ConnectionState.DISCONNECTED,
|
||||
};
|
||||
|
||||
const connectedStarted: AppState = {
|
||||
radio: RadioState.STARTED,
|
||||
loading: false,
|
||||
connection: ConnectionState.CONNECTED,
|
||||
};
|
||||
|
||||
const connectedStopped: AppState = {
|
||||
radio: RadioState.STOPPED,
|
||||
loading: false,
|
||||
connection: ConnectionState.CONNECTED,
|
||||
};
|
||||
|
||||
it('should handle initial state', () => {
|
||||
expect(reducer(undefined, { type: 'unknown' })).toEqual({
|
||||
loading: false,
|
||||
connection: ConnectionState.DISCONNECTED,
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle set loading', () => {
|
||||
const actual = reducer(initialState, toggleLoading());
|
||||
expect(actual.loading).toEqual(true);
|
||||
});
|
||||
|
||||
it('should hande toggle radio started', () => {
|
||||
const actual = reducer(connectedStopped, toggleRadio());
|
||||
expect(actual.radio).toEqual(RadioState.STARTED);
|
||||
})
|
||||
|
||||
it('should hande toggle radio stopped', () => {
|
||||
const actual = reducer(connectedStarted, toggleRadio());
|
||||
expect(actual.radio).toEqual(RadioState.STOPPED);
|
||||
})
|
||||
|
||||
it('should hande set radio started', () => {
|
||||
const actual = reducer(connectedStopped, setRadioStarted());
|
||||
expect(actual.radio).toEqual(RadioState.STARTED);
|
||||
})
|
||||
|
||||
it('should hande set radio stopped', () => {
|
||||
const actual = reducer(connectedStarted, setRadioStopped());
|
||||
expect(actual.radio).toEqual(RadioState.STOPPED);
|
||||
})
|
||||
|
||||
it('should noop set radio started', () => {
|
||||
const actual = reducer(connectedStarted, setRadioStarted());
|
||||
expect(actual.radio).toEqual(RadioState.STARTED);
|
||||
})
|
||||
|
||||
it('should noop set radio stopped', () => {
|
||||
const actual = reducer(connectedStopped, setRadioStopped());
|
||||
expect(actual.radio).toEqual(RadioState.STOPPED);
|
||||
})
|
||||
});
|
112
ui/src/features/radio/radioSlice.ts
Normal file
112
ui/src/features/radio/radioSlice.ts
Normal file
|
@ -0,0 +1,112 @@
|
|||
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
|
||||
import { RootState, AppThunk } from '../../app/store';
|
||||
import { RadioState, ConnectionState } from '../../app/types';
|
||||
import { requestToStart, requestToStop } from './radioAPI';
|
||||
|
||||
export interface AppState {
|
||||
radio?: RadioState,
|
||||
loading: boolean,
|
||||
connection: ConnectionState
|
||||
}
|
||||
|
||||
const initialState: AppState = {
|
||||
radio: undefined,
|
||||
loading: false,
|
||||
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;
|
||||
}
|
||||
);
|
||||
|
||||
export const radioSlice = createSlice({
|
||||
name: 'radio',
|
||||
initialState,
|
||||
reducers: {
|
||||
toggleLoading: (state) => { state.loading = false ? state.loading : true },
|
||||
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) => {
|
||||
state.loading = true;
|
||||
return state;
|
||||
})
|
||||
.addCase(requestToStartThunk.fulfilled, (state, action) => {
|
||||
state.loading = false;
|
||||
if (action.payload) {
|
||||
state.radio = RadioState.STARTED;
|
||||
}
|
||||
return state;
|
||||
})
|
||||
.addCase(requestToStopThunk.pending, (state, _) => {
|
||||
state.loading = true;
|
||||
return state;
|
||||
})
|
||||
.addCase(requestToStopThunk.fulfilled, (state, action) => {
|
||||
state.loading = false;
|
||||
if (action.payload) {
|
||||
state.radio = RadioState.STOPPED;
|
||||
}
|
||||
return state;
|
||||
})
|
||||
},
|
||||
});
|
||||
|
||||
export const {
|
||||
toggleLoading,
|
||||
setRadioStarted,
|
||||
setRadioStopped,
|
||||
unsetRadio,
|
||||
toggleRadio,
|
||||
setConnected,
|
||||
setDisconnected,
|
||||
toggleConnection,
|
||||
} = radioSlice.actions;
|
||||
|
||||
export const selectLoading = (state: RootState) => state.globalState.loading;
|
||||
export const selectRadio = (state: RootState) => state.globalState.radio;
|
||||
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
|
41
ui/src/features/radio/radioWS.ts
Normal file
41
ui/src/features/radio/radioWS.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
import { AppDispatch } from '../../app/store';
|
||||
import {
|
||||
setRadioStarted,
|
||||
setRadioStopped,
|
||||
unsetRadio,
|
||||
setConnected,
|
||||
setDisconnected,
|
||||
} from './radioSlice';
|
||||
|
||||
|
||||
export const getOnWSOpen = (dispatch: AppDispatch) => {
|
||||
return () => {
|
||||
console.log("[WS] Opened");
|
||||
dispatch(setConnected());
|
||||
}
|
||||
};
|
||||
|
||||
export const getOnWSClose = (dispatch: AppDispatch) => {
|
||||
return () => {
|
||||
console.log("[WS] Closed")
|
||||
dispatch(setDisconnected());
|
||||
return true
|
||||
}
|
||||
};
|
||||
|
||||
export const getOnWSMessage = (dispatch: AppDispatch) => {
|
||||
return (message: any) => {
|
||||
console.log("[WS] Message: %s", JSON.stringify(message));
|
||||
|
||||
switch (message.status) {
|
||||
case "STARTED":
|
||||
dispatch(setRadioStarted());
|
||||
break;
|
||||
case "STOPPED":
|
||||
dispatch(setRadioStopped());
|
||||
break;
|
||||
default:
|
||||
dispatch(unsetRadio());
|
||||
}
|
||||
}
|
||||
};
|
13
ui/src/index.css
Normal file
13
ui/src/index.css
Normal file
|
@ -0,0 +1,13 @@
|
|||
body {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
|
||||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
||||
sans-serif;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
code {
|
||||
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
|
||||
monospace;
|
||||
}
|
17
ui/src/index.tsx
Normal file
17
ui/src/index.tsx
Normal file
|
@ -0,0 +1,17 @@
|
|||
import React from 'react';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
import { Provider } from 'react-redux';
|
||||
import { store } from './app/store';
|
||||
import App from './App';
|
||||
import './index.css';
|
||||
|
||||
const container = document.getElementById('root')!;
|
||||
const root = createRoot(container);
|
||||
|
||||
root.render(
|
||||
<React.StrictMode>
|
||||
<Provider store={store}>
|
||||
<App />
|
||||
</Provider>
|
||||
</React.StrictMode>
|
||||
);
|
1
ui/src/logo.svg
Normal file
1
ui/src/logo.svg
Normal file
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 1.1 KiB |
1
ui/src/react-app-env.d.ts
vendored
Normal file
1
ui/src/react-app-env.d.ts
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/// <reference types="react-scripts" />
|
5
ui/src/setupTests.ts
Normal file
5
ui/src/setupTests.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
// jest-dom adds custom jest matchers for asserting on DOM nodes.
|
||||
// allows you to do things like:
|
||||
// expect(element).toHaveTextContent(/react/i)
|
||||
// learn more: https://github.com/testing-library/jest-dom
|
||||
import '@testing-library/jest-dom/extend-expect';
|
26
ui/tsconfig.json
Normal file
26
ui/tsconfig.json
Normal file
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"lib": [
|
||||
"dom",
|
||||
"dom.iterable",
|
||||
"esnext"
|
||||
],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"strict": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true,
|
||||
"jsx": "react-jsx"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
9340
ui/yarn.lock
Normal file
9340
ui/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user