From a363f3c563edd21879660478a0b73632256d8d34 Mon Sep 17 00:00:00 2001 From: DoParkEQ Date: Sun, 11 Jul 2021 15:30:50 -0400 Subject: [PATCH] updated storage model according to the backend routes --- client/src/configs.js | 3 ++- client/src/context/DashboardContext.js | 15 +++++++++++---- client/src/controller/DashboardController.js | 12 +++++++++--- client/src/lib/api/user.js | 10 ++++++++++ client/src/lib/api/vitals.js | 7 ++++--- client/src/model/storage.js | 11 +++++++++-- client/src/view/Dashboard.js | 5 +++-- 7 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 client/src/lib/api/user.js diff --git a/client/src/configs.js b/client/src/configs.js index d076589..9cad0dc 100644 --- a/client/src/configs.js +++ b/client/src/configs.js @@ -122,7 +122,8 @@ export const defaultOxyData = { export const defaultVital = new storage( process.env.REACT_APP_SERVICE_UUID, - process.env.REACT_APP_CHT_UUID); + process.env.REACT_APP_CHT_UUID, + 'ajsdo222'); export const tabsList = [{ name: 'Dashboard 📈', diff --git a/client/src/context/DashboardContext.js b/client/src/context/DashboardContext.js index 779250d..5cac2bb 100644 --- a/client/src/context/DashboardContext.js +++ b/client/src/context/DashboardContext.js @@ -1,14 +1,19 @@ import React, { createContext, useReducer } from 'react'; import { initialState, defaultOxyData } from '../configs'; import { getSummary } from '../utils'; +import { postVital } from '../lib/api/vitals'; export const DashboardContext = createContext(initialState); const reducer = (state, action) => { switch (action.type) { - case 'LOAD_SAVED_DATA': { - const { userVital } = action.payload; - return { ...state, userVital }; + case 'LOAD_DATA': { + const { userVital, userInfo } = action.payload; + if (userVital.length > 0) { + state.userVital.addHistory(userVital); + // state.userVital.addUUIDs(userInfo.serviceUUID, userInfo.chtUUID) + } + return { ...state }; } case 'CONNECT': { @@ -29,8 +34,11 @@ const reducer = (state, action) => { const history = { runTime: state.oxyData.elapsedTime, date: state.userStatus.startTime, + vitalLog: [...state.userVital.vitalLog], }; + getSummary(state.userVital.vitalLog); + postVital(history, state.userVital.userID); state.userVital.updateHistory(history); const userStatus = { ...state.userStatus, @@ -38,7 +46,6 @@ const reducer = (state, action) => { deviceName: null, startTime: new Date(), }; - console.log(state.userVital); const oxyData = { ...defaultOxyData }; return { ...state, userStatus, oxyData }; } diff --git a/client/src/controller/DashboardController.js b/client/src/controller/DashboardController.js index 12f5bf4..15cc8a0 100644 --- a/client/src/controller/DashboardController.js +++ b/client/src/controller/DashboardController.js @@ -2,7 +2,8 @@ import React, { useEffect, useContext } from 'react'; import Dashboard from '../view/Dashboard'; import { checkVitalAnomalies, getTime } from '../utils'; import { DashboardContext } from '../context/DashboardContext'; -import { getSavedVitals } from '../lib/api/vitals'; +import { getVitals, postVital } from '../lib/api/vitals'; +import { getUserInfo } from '../lib/api/user'; const timeInterval = 1000; const flags = { @@ -10,14 +11,19 @@ const flags = { graph: 7, vital: 8, }; + const DashboardController = () => { const { dispatch, state } = useContext(DashboardContext); const { oximetry, userStatus, userVital, vitalSnapshot, rollbackCount, device } = state; const signal = []; - useEffect(() => { - getSavedVitals().then(([userVital]) => dispatch({ type: 'LOAD_SAVED_DATA', payload: { userVital } })); + useEffect(async () => { + const fetchedUserVital = await getVitals(userVital.userID); + const fetchedUserInfo = await getUserInfo(userVital.userID); + console.log('userInfo:', fetchedUserInfo); + console.log('userVital:', fetchedUserVital); + dispatch({ type: 'LOAD_DATA', payload: { userVital: fetchedUserVital, userInfo: fetchedUserInfo } }); }, []); useEffect(() => { diff --git a/client/src/lib/api/user.js b/client/src/lib/api/user.js new file mode 100644 index 0000000..9e2d702 --- /dev/null +++ b/client/src/lib/api/user.js @@ -0,0 +1,10 @@ +import axios from 'axios'; + +const PORT = 5000; +const URL = `http://localhost:${PORT}/user`; + +//TODO: get userinfo to fill out UUIDs +export const getUserInfo = async (userId) => { + const res = await axios.get(`${URL}/${userId}`); + return res.data; +}; diff --git a/client/src/lib/api/vitals.js b/client/src/lib/api/vitals.js index 2bff7f6..292996b 100644 --- a/client/src/lib/api/vitals.js +++ b/client/src/lib/api/vitals.js @@ -1,10 +1,11 @@ import axios from 'axios'; const PORT = 5000; -const url = `http://localhost:${PORT}/vitals`; +const URL = `http://localhost:${PORT}/vitals`; -export const getSavedVitals = async () => { - const res = await axios.get(url); +export const getVitals = async (userId) => { + const res = await axios.get(`${URL}/${userId}`); return res.data; }; +export const postVital = (history, userId) => axios.post(`${URL}/${userId}/post`, history); diff --git a/client/src/model/storage.js b/client/src/model/storage.js index 40d236c..568f4f8 100644 --- a/client/src/model/storage.js +++ b/client/src/model/storage.js @@ -1,10 +1,10 @@ - class storage { - constructor(serviceUUID, chtUUID) { + constructor(serviceUUID, chtUUID, userID) { this.vitalLog = []; this.history = []; this.serviceUUID = serviceUUID || null; this.chtUUID = chtUUID || null; + this.userID = userID || null; } @@ -25,6 +25,13 @@ class storage { this.serviceUUID = serviceUUID; this.chtUUID = chtUUID; } + + addHistory(data) { + if (data.length > 0) { + this.history = data.map(d => d.history); + } + } } export default storage; + diff --git a/client/src/view/Dashboard.js b/client/src/view/Dashboard.js index ab6f0cd..39eef3d 100644 --- a/client/src/view/Dashboard.js +++ b/client/src/view/Dashboard.js @@ -66,7 +66,7 @@ const Dashboard = ({ onSubscribe, onDisconnect }) => { return n; }); }; - + console.log(userVital.serviceUUID, userVital.chtUUID); return ( <> {openModal && @@ -151,7 +151,8 @@ const Dashboard = ({ onSubscribe, onDisconnect }) => { {`Total run time: ${runTime}`} - {`Date: ${formatDate(date)}`} + {/* {`Date: ${formatDate(date)}`} */} + {`Date: ${date}`} , )}