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

issue #259: fix label display #265

Merged
merged 1 commit into from
Oct 7, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/Components/Label/Label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const Label = ({ sections }: LabelProps) => {
const renderObjectInput = (inputInfo: Input) => {
const keys = Object.keys(
(inputInfo.value as { [key: string]: string }[])[0],
);
).filter((key) => !inputInfo.fieldsToAvoidDisplaying.includes(key));

return (
<div id={inputInfo.id} className="object-input">
Expand Down
41 changes: 31 additions & 10 deletions src/Pages/LabelPage/LabelPage.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import axios from "axios";
import merge from "deepmerge";
import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import Label from "../../Components/Label/Label";
import { createDefaultInspection } from "../../interfaces/Inspection";
import Data from "../../Model/Data-Model";
import { useAlert } from "../../Utils/AlertContext";
import { combineMerge } from "../../Utils/deepMerge";
import { FertiliserForm, populateFromJSON } from "../../Utils/FormCreator";
import "./LabelPage.css";

function LabelPage() {
const params = useParams();
const labelId = params.labelId;
const [label, setLabel] = useState<Data>(FertiliserForm());
const { showAlert } = useAlert();
useEffect(() => {
if (process.env.REACT_APP_ACTIVATE_USING_JSON == "true") {
fetch("/debug/" + labelId + ".json")
Expand All @@ -17,16 +23,31 @@ function LabelPage() {
setLabel(populateFromJSON(label, data));
});
} else {
fetch(process.env.VITE_API_URL + "/inspections/" + labelId, {
headers: {
Authorization:
"Basic " + document.cookie.split("auth=")[1].split(";")[0],
},
})
.then((r) => r.json())
.then((data) => {
data = JSON.parse(data);
setLabel(populateFromJSON(label, data));
axios
.get(process.env.VITE_API_URL + "/inspections/" + labelId, {
headers: {
Authorization:
"Basic " + document.cookie.split("auth=")[1].split(";")[0],
},
})
.then((response) => {
let data = response.data;

if (typeof data === "string") {
try {
data = JSON.parse(data);
} catch (e) {
console.error("Error parsing JSON string:", e);
}
}
const inspection = merge(createDefaultInspection(), data, {
arrayMerge: combineMerge,
});
setLabel(populateFromJSON(label, inspection));
})
.catch((error) => {
console.error("Error fetching the inspection:", error);
showAlert(String(error), "error");
});
}
// eslint-disable-next-line
Expand Down
22 changes: 11 additions & 11 deletions src/interfaces/Inspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ export interface LocalizedSubLabel {

export interface Metrics {
weight: Value[];
volume: Value;
density: Value;
volume: Value | null;
density: Value | null;
}

export interface ProductInformation {
name: string | null;
label_id: string | null;
registration_number: string | null;
lot_number: string | null;
metrics: Metrics;
metrics: Metrics | null;
npk: string | null;
warranty: string | null;
n: number | null;
Expand All @@ -51,22 +51,22 @@ export interface Title {
}

export interface GuaranteedAnalysis {
title: Title;
title: Title | null;
en: NamedValue[];
fr: NamedValue[];
}

export default interface Inspection {
inspection_id: string | null;
verified: boolean;
company: OrganizationInformation;
manufacturer: OrganizationInformation;
product: ProductInformation;
cautions: LocalizedSubLabel;
instructions: LocalizedSubLabel;
ingredients: LocalizedValues;
company: OrganizationInformation | null;
manufacturer: OrganizationInformation | null;
product: ProductInformation | null;
cautions: LocalizedSubLabel | null;
instructions: LocalizedSubLabel | null;
ingredients: LocalizedValues | null;
inspection_comment: string | null;
guaranteed_analysis: GuaranteedAnalysis;
guaranteed_analysis: GuaranteedAnalysis | null;
}

export const createDefaultValue = (): Value => ({
Expand Down
Loading