Skip to content

Commit

Permalink
add error message and validation rule for federal waste codes
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgraham4401 committed Aug 31, 2023
1 parent 266e742 commit 3c31944
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { HtForm } from 'components/Ht';
import { ManifestContext, ManifestContextType } from 'components/Manifest/ManifestForm';
import { StateWasteCodeSelect } from 'components/Manifest/WasteLine/HazardousWasteForm/StateWasteCodeSelect';
import { Code } from 'components/Manifest/WasteLine/wasteLineSchema';
import { Code, HazardousWaste, WasteLine } from 'components/Manifest/WasteLine/wasteLineSchema';
import React, { useContext } from 'react';
import { Col, Row } from 'react-bootstrap';
import { Controller, useFormContext } from 'react-hook-form';
import Select, { components, GroupBase, MultiValueProps, StylesConfig } from 'react-select';
import { useGetFedWasteCodesQuery } from 'store/wasteCode.slice';
import { ErrorMessage } from '@hookform/error-message';

interface HazardousWasteFormProps {
epaWaste: boolean;
Expand All @@ -18,7 +19,10 @@ interface HazardousWasteFormProps {
* @constructor
*/
export function HazardousWasteForm({ epaWaste }: HazardousWasteFormProps) {
const { control } = useFormContext();
const {
control,
formState: { errors },
} = useFormContext<WasteLine>();
const { generatorStateCode, tsdfStateCode } = useContext<ManifestContextType>(ManifestContext);
// Retrieve federal waste codes from the server
const {
Expand Down Expand Up @@ -56,6 +60,8 @@ export function HazardousWasteForm({ epaWaste }: HazardousWasteFormProps) {
<components.MultiValue {...props}>{props.data.code}</components.MultiValue>
);

console.log('federal erros', errors);

return (
<>
<Row className="mb-2">
Expand All @@ -78,6 +84,7 @@ export function HazardousWasteForm({ epaWaste }: HazardousWasteFormProps) {
options={federalWasteCodes}
isLoading={federalLoading}
getOptionLabel={(option) =>
// @ts-ignore
`${option.code}: ${option.description.toLowerCase()}`
}
getOptionValue={(option) => option.code}
Expand All @@ -87,10 +94,21 @@ export function HazardousWasteForm({ epaWaste }: HazardousWasteFormProps) {
isMulti
isClearable
hideSelectedOptions
classNames={{
control: () =>
`form-control p-0 rounded-2 ${
errors.hazardousWaste?.federalWasteCodes && 'border-danger'
} `,
}}
/>
);
}}
></Controller>
/>
<ErrorMessage
errors={errors}
name={'hazardousWaste.federalWasteCodes'}
render={({ message }) => <span className="text-danger">{message}</span>}
/>
{federalError ? (
<i className="text-danger">
We experienced an error retrieving the federal waste codes
Expand Down
2 changes: 2 additions & 0 deletions client/src/components/Manifest/WasteLine/WasteLineForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export function WasteLineForm({
* @param wasteLine the data submitted from the form
*/
const onSubmit = (wasteLine: WasteLine) => {
console.log('epaWaste', wasteLine.epaWaste);
console.log('waste codes', wasteLine.hazardousWaste?.federalWasteCodes);
if (waste) {
wasteArrayMethods.update(lineNumber, wasteLine); // append the new waste line to the manifest
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface WasteRowActionProps {
}

/**
* WasteRowActions - actions for controlling waste lines on a manifest
* WasteRowActions - actions for controlling wast lines on a manifest
* @constructor
*/
function WasteRowActions({
Expand Down
36 changes: 15 additions & 21 deletions client/src/components/Manifest/WasteLine/wasteLineSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const hazardousWasteSchema = z.object({
generatorStateWasteCodes: z.array(codeSchema).optional(),
});

export type HazardousWaste = z.infer<typeof hazardousWasteSchema>;

const dotInformationSchema = z.object({
idNumber: codeSchema,
printedDotInformation: z.string(),
Expand Down Expand Up @@ -100,6 +102,16 @@ export const wasteLineSchema = z
message: 'DOT ID number is required',
}
)
.refine(
(wasteLine) => {
// If stream is federally hazardous waste, a federal waste code is required
return wasteLine.epaWaste && wasteLine.hazardousWaste?.federalWasteCodes;
},
{
path: ['hazardousWaste.federalWasteCodes'],
message: 'Federal waste code is required if the waste is EPA Waste',
}
)
.refine(
(wasteLine) => {
// If material is DOT hazardous, then dotInformation.idNumber.code is required
Expand All @@ -111,26 +123,8 @@ export const wasteLineSchema = z
}
);

/**
* WasteLine is the interface for the waste line object in the manifest
*/
export type WasteLine = z.infer<typeof wasteLineSchema>;

// /**
// * Represents waste information captures on EPA's hazardous waste manifest
// */
// interface WasteLine {
// dotHazardous: boolean;
// epaWaste: boolean;
// pcb: boolean;
// lineNumber: number;
// dotInformation?: DotInformation;
// wasteDescription?: string;
// quantity?: Quantity;
// brInfo?: BrInfo;
// br: boolean;
// hazardousWaste?: HazardousWaste;
// pcbInfos?: PcbInfo[];
// discrepancyResidueInfo?: DiscrepancyResidueInfo;
// managementMethod?: Code;
// additionalInfo?: AdditionalInfo;
// }

export type { ContainerDescription };

0 comments on commit 3c31944

Please sign in to comment.