Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ adds patch requests to all contexts #75

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions ui/src/context/antennaContext.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React, { useContext, useState } from 'react';
import PropTypes from 'prop-types';
import axios from 'axios';
const antennaContext = React.createContext();
const updateAntennaContext = React.createContext();

const url = config[process.env.REACT_APP_NODE_ENV || 'development'].apiUrl;

const defaultAntennaContext = [
{
id: 1,
Expand Down Expand Up @@ -50,9 +53,25 @@ export const AntennaProvider = ({ children }) => {
});

const updateAntenna = update => {
console.log('updateAntenna');
window.sewApp.socket.emit('updateAntenna', { user: window.sewApp.socket.id, signals: update });
setAntenna(update);
console.log('updateAntenna', update);
// send patch request to server

// I think we're going to need to attach ?id={$id} to the end of the url
axios.patch(`${url}/data/antenna`, update)
.then(res => {
console.log(res);
if (res.status === 200) {
window.sewApp.socket.emit('updateAntenna', { user: window.sewApp.socket.id, signals: update });
setAntenna(update);
}
else {
console.log('error patching antenna');
window.alert("Error patching antenna");
}
})
.catch(err => {
console.log("Error patching receiver", err);
});
};

return (
Expand Down
23 changes: 20 additions & 3 deletions ui/src/context/rxContext.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React, { useContext, useState } from 'react';
import PropTypes from 'prop-types';
import axios from 'axios';
const rxContext = React.createContext();
const updateRxContext = React.createContext();

const url = config[process.env.REACT_APP_NODE_ENV || 'development'].apiUrl;

const defaultRxContext = [
{
id: 1,
Expand Down Expand Up @@ -227,7 +230,7 @@ export const RxProvider = ({ children }) => {

window.sewApp.socket.on('updateRxClient', (data) => {
if (data.user != window.sewApp.socket.id) {
console.log('actually updating the Rx');
console.log('received update for Rx');
let tmpUpdate = [...rx];
tmpUpdate[data.index] = data.update;
setRx(tmpUpdate);
Expand All @@ -239,8 +242,22 @@ export const RxProvider = ({ children }) => {
tmpUpdate[index] = update;
// patch request to update database
// if patch request is good
window.sewApp.socket.emit('updateRx', { user: window.sewApp.socket.id, update: update, index: index });
setRx(tmpUpdate);
// send patch request to server
axios.patch(`${url}/data/receiver`, update)
.then(res => {
console.log(res);
if (res.status === 200) {
window.sewApp.socket.emit('updateRx', { user: window.sewApp.socket.id, update: update, index: index });
setRx(tmpUpdate);
}
else {
console.log('error patching receiver');
window.alert("Error patching receiver");
}
})
.catch(err => {
window.alert("Error patching receiver", err);
});
};

return (
Expand Down
38 changes: 35 additions & 3 deletions ui/src/context/sewAppContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { RfEnvironment } from '../RfEnvironment';
import { io, Socket } from 'socket.io-client';
import { antennas, satellites } from '../constants';
import { CRUDdataTable } from './../crud/crud';
import axios from 'axios';

const url = config[process.env.REACT_APP_NODE_ENV || 'development'].apiUrl;

// Create a sync global context for the RF Environments
const sewApp = {
teamInfo: {
team: '',
server: '',
},
//updateTxData: useUpdateTx(),
init: () => {
window.sewApp.socketInit(window.sewApp.socket);
},
Expand All @@ -34,7 +36,20 @@ const sewApp = {
team: 'default', // TODO: Replace this properly
server: '',
};
socket.emit('updateTeam', { team: sewApp.team });

// patch the server with the team info
axios.patch(`${url}/data/server?id=${window.sewApp.teamInfo.server.id}`, window.sewApp.teamInfo)
.then(res => {
if (res.status === 200) {
socket.emit('updateTeam', { team: sewApp.team });
} else {
console.log('error patching team');
window.alert('error patching team');
}
})
.catch(err => {
console.log('error patching team', err);
});

socket.on('updateSignals', update => {
window.sewApp.environment.updateSignals(update);
Expand Down Expand Up @@ -87,12 +102,29 @@ const sewApp = {
antenna_id: specA.antenna_id,
};
console.log('announceSpecAChange', sewApp.socket.id);
sewApp.socket.emit('updateSpecA', patchData);

// send patch request to server
axios.patch(`${url}/data/spec_a`, patchData)
.then(res => {
if (res.status === 200) {
sewApp.socket.emit('updateSpecA', patchData);
}
else {
console.log('Error patching SpecA');
}
})
.catch(err => {
console.log('Error patching SpecA', err);
});

// I don't think this is needed if we use axios
/*
CRUDdataTable({
method: 'PATCH',
path: 'spec_a',
data: patchData,
});
*/
},
};

Expand Down
16 changes: 15 additions & 1 deletion ui/src/context/signalContext.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React, { useContext, useState } from 'react';
import PropTypes from 'prop-types';
import axios from 'axios';
const signalContext = React.createContext();
const updateSignalContext = React.createContext();

const url = config[process.env.REACT_APP_NODE_ENV || 'development'].apiUrl;

const defaultSignalContext = [
{id: 1, server_id: 1, target_id: 1, frequency: 4710, power: -45, bandwidth: 5, modulation: '8QAM', fec: '1/2', feed:'red 1.mp4', operational: 'false'},
{id: 2, server_id: 1, target_id: 1, frequency: 4720, power: -50, bandwidth: 3, modulation: '8QAM', fec: '3/4', feed:'red 2.mp4', operational: 'false'},
Expand Down Expand Up @@ -30,7 +33,18 @@ export const useUpdateSignal = () => {
export const SignalProvider = ({ children }) => {
const [signal, setSignal] = useState(defaultSignalContext);
const updateSignal = (update) => {
setSignal(update);
axios.patch(`${url}/data/signal`, update)
.then(res => {
if (res.status === 200) {
setSignal(res.data);
}
else {
console.log('error patching signal');
}
})
.catch(err => {
console.log('error patching signal', err);
});
};

return (
Expand Down
Empty file removed ui/src/context/specAContext.jsx
Empty file.
24 changes: 18 additions & 6 deletions ui/src/context/txContext.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React, { useContext, useState } from 'react';
import PropTypes from 'prop-types';
import axios from 'axios';
const txContext = React.createContext();
const updateTxContext = React.createContext();

const url = config[process.env.REACT_APP_NODE_ENV || 'development'].apiUrl;

const defaultTxContext = [
{
id: 1,
Expand Down Expand Up @@ -227,17 +230,26 @@ export const TxProvider = ({ children }) => {

window.sewApp.socket.on('updateTxClient', data => {
if (data.user != window.sewApp.socket.id) {
console.log('actually updating the Tx');
console.log('received update for Tx');
setTx(data.signals);
}
});

const updateTx = update => {
// patch request to update database
// if patch request is good
window.sewApp.socket.emit('updateTx', { user: window.sewApp.socket.id, signals: update });
setTx(update);
console.log(update);
axios.patch(`${url}/data/tx`, update)
.then(res => {
if (res.status === 200) {
window.sewApp.socket.emit('updateTx', { user: window.sewApp.socket.id, signals: update });
setTx(update);
}
else {
console.log('error patching transmitter');
window.alert("Error patching transmitter");
}
})
.catch(err => {
console.log('error patching signal', err);
});
};

return (
Expand Down
23 changes: 19 additions & 4 deletions ui/src/context/userContext.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React, { useContext, useState } from 'react';
import PropTypes from 'prop-types';
import axios from 'axios';
const userContext = React.createContext();
const updateUserContext = React.createContext();

const url = config[process.env.REACT_APP_NODE_ENV || 'development'].apiUrl;

const defaultUserContext = { server_id: 1, team_id: 1};

export const useUser = () => {
Expand All @@ -25,10 +28,22 @@ export const UserProvider = ({ children }) => {

const updateUser = (update) => {
console.log('updateUser', update);
// patch request to update database
// if patch request is good
window.sewApp.socket.emit('updateUser', { user: window.sewApp.socket.id, signals: update });
setUser(update);
// send patch request to server
axios.patch(`${url}/data/player/${user.id}`, update)
.then(res => {
if(res.status === 200) {
window.sewApp.socket.emit('updateUser', { user: window.sewApp.socket.id, signals: update });
setUser(update);
}
else {
console.log('error patching user');
window.alert("Error patching user");
}
})
.catch(err => {
console.log("Error patching user", err);
}
);
};

return (
Expand Down