Merge branch 'release/0.0.3'

This commit is contained in:
jwu-fisker
2023-03-13 17:16:28 -07:00
28 changed files with 1549 additions and 56 deletions

View File

@@ -1,6 +1,6 @@
import React, { useContext, useState } from "react";
import api from "../../services/CANFiltersAPI";
import { validateCANID, validateFilter } from "../../utils/validationSupplier";
import { validateCANID, validateFilter, validateVIN } from "../../utils/validationSupplier";
const CANFiltersContext = React.createContext();
@@ -100,10 +100,4 @@ export const CANFiltersProvider = ({ children }) => {
);
};
const validateVIN = (vin) => {
if (vin == null || vin.length !== 17) {
throw new Error("Invalid VIN");
}
};
export const useCANFiltersContext = () => useContext(CANFiltersContext);

View File

@@ -0,0 +1,73 @@
import React, { useContext, useState } from "react";
import api from "../../services/CANSignalAPI";
import { validateVIN } from "../../utils/validationSupplier";
const CANSignalsExportContext = React.createContext();
export const CANSignalsExportProvider = ({ children }) => {
const [busy, setBusy] = useState(false);
const [canSignals, setCanSignals] = useState([]);
const getCANSignalList = async (token) => {
try {
setBusy(true);
const result = await api.getCanSignalList(token);
if (result.error) {
throw new Error(`Get can signal list error. ${result.message}`);
}
setCanSignals(result.data ?? []);
return result;
} finally {
setBusy(false);
}
};
const getDynamicColumnCANSignals = async (vin, timestart, timeend, cansingals, token) => {
try {
setBusy(true)
if (!vin) return;
validateVIN(vin);
if (timestart > timeend) throw new Error("Start time cannot be after end time");
const result = await api.getCanSignalsVin(vin, timestart, timeend, cansingals, token);
if (result.error || !result.ok)
throw new Error(`Get CAN signals error. ${result.message}`);
const blob = await result.blob();
const reader = new FileReader();
reader.onload = () => {
const csvData = reader.result;
const blob = new Blob([csvData], { type: 'text/csv;charset=utf-8' });
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = 'CAN_signals.csv';
link.click();
};
reader.readAsText(blob);
} catch (e) {
throw new Error(e)
}
finally {
setBusy(false)
}
}
return (
<CANSignalsExportContext.Provider
value={{
busy,
canSignals,
getCANSignalList,
getDynamicColumnCANSignals
}}
>
{children}
</CANSignalsExportContext.Provider>
);
};
export const useCANSignalsExportContext = () => useContext(CANSignalsExportContext);

View File

@@ -1,6 +1,8 @@
import React, { useContext, useState } from "react";
import { logger } from "../../services/monitoring";
import api from "../../services/vehiclesAPI";
import { validateVIN } from "../../utils/validationSupplier";
const VehicleContext = React.createContext();
@@ -285,10 +287,4 @@ const validateVehicle = (v) => {
validateVIN(v.vin);
};
const validateVIN = (vin) => {
if (vin == null || vin.length !== 17) {
throw new Error("Invalid VIN");
}
};
export const useVehicleContext = () => useContext(VehicleContext);

View File

@@ -0,0 +1,23 @@
let busy = false;
let canSignals = [
{
signal_name: "123",
},
{
signal_name: "456",
},
{
signal_name: "789",
},
];
export const CANSignalsExportProvider = ({ children }) => {
return <div data-testid="mocked-cansignalsprovider">{children}</div>;
};
export const useCANSignalsExportContext= () => ({
busy,
canSignals,
getCANSignalList: jest.fn(),
getDynamicColumnCANSignals: jest.fn(),
});