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

validation custom forms #21

Open
wants to merge 24 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
79 changes: 79 additions & 0 deletions src/webapp/components/select/Select.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
FormControl,
InputLabel,
MenuItem,
Select as MuiSelect,
SelectProps as MuiSelectProps,
} from "@material-ui/core";
import { createStyles, makeStyles } from "@material-ui/core/styles";
import _ from "lodash";
import React, { useMemo, useState } from "react";

export type SelectOption = { value: string; label: string };

export interface SelectProps extends Omit<MuiSelectProps, "onChange"> {
placeholder?: string;
options: Array<SelectOption>;
onChange: (option: SelectOption) => void;
defaultValue?: SelectOption;
value?: string;
allowEmpty?: boolean;
emptyLabel?: string;
}

export const Select: React.FC<SelectProps> = ({
placeholder,
options,
onChange,
defaultValue,
value,
allowEmpty = false,
emptyLabel = "",
...rest
}) => {
const classes = useStyles();
const [stateValue, setValue] = useState(defaultValue ? defaultValue.value : "");
const optionsByValue = useMemo(() => _.keyBy(options, option => option.value), [options]);
const defaultOption = allowEmpty ? { label: "", value: "" } : undefined;

const handleChange = (event: React.ChangeEvent<{ value: unknown }>) => {
const newValue = event.target.value as string;
const option = _(optionsByValue).get(newValue, defaultOption);
setValue(newValue);
if (option) onChange(option);
};

const defaultLabel = allowEmpty ? emptyLabel : placeholder;

return (
<div>
<FormControl className={classes.formControl}>
{!!placeholder && <InputLabel id="demo-simple-select-label">{placeholder}</InputLabel>}
<MuiSelect onChange={handleChange} value={value ?? stateValue} autoWidth={true} {...rest}>
{!!defaultLabel && (
<MenuItem value="" disabled={!allowEmpty} className={classes.menuItem}>
{defaultLabel}
</MenuItem>
)}
{options.map(option => (
<MenuItem key={option.value} value={option.value} className={classes.menuItem}>
{option.label}
</MenuItem>
))}
</MuiSelect>
</FormControl>
</div>
);
};

const useStyles = makeStyles(() =>
createStyles({
formControl: {
margin: 0,
display: "flex",
},
menuItem: {
minHeight: 35,
},
})
);
4 changes: 4 additions & 0 deletions src/webapp/reports/Reports.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import AdminReport from "./admin/AdminReport";
import NHWACommentsReport from "./nhwa-comments/NHWACommentsReport";
import NHWADataApprovalStatusReport from "./nhwa-approval-status/NHWADataApprovalStatusReport";
import ValidateCustomFormsReport from "./validate-custom-forms/ValidateCustomFormsReport";

const widget = process.env.REACT_APP_REPORT_VARIANT || "";

Expand All @@ -16,6 +17,9 @@ const Component: React.FC = () => {
case "admin": {
return <AdminReport />;
}
case "validatecustomforms": {
idelcano marked this conversation as resolved.
Show resolved Hide resolved
return <ValidateCustomFormsReport />;
}
default: {
return <p>{`Please provide a valid REACT_APP_REPORT_VARIANT`}</p>;
}
Expand Down
10 changes: 10 additions & 0 deletions src/webapp/reports/validate-custom-forms/CustomFormErrorsList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react";

export const CustomFormErrorsList: React.FC = React.memo(() => {
idelcano marked this conversation as resolved.
Show resolved Hide resolved
//await compositionRoot.dataComments.get({})
return (
<div>
{ <p>{ "ERROR: UID xxx does not exist in dataset xxxx" }</p> }{ <p>{ "ERROR: DataElement xxx is not associated to dataset xxxx" }</p> }
</div>
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Typography, makeStyles } from "@material-ui/core";
import React from "react";
import { useState } from "react";
import i18n from "../../../locales";
import { Select, SelectOption } from "../../components/select/Select";
import { CustomFormErrorsList } from "./CustomFormErrorsList";


const onModuleChange = ({ value }: SelectOption) => {
//execute use case to get the CustomFormErrorsList from a given dataset
return value

};
const AdminReport: React.FC = () => {
const classes = useStyles();
//hardcoded list of modules
const [modules] = useState<{ value: string; label: string }[]>([]);
return (
<React.Fragment>
<h1 className={classes.title} >{i18n.t("Custom Form Validation")}</h1>

<div className={classes.select}>
<Select
placeholder={i18n.t("Select custom form to validate...")}
onChange={onModuleChange}
options={modules}
value="test"
/>
</div>


<div className={classes.row}>
<Typography variant="h5">
{i18n.t("Result:")}
</Typography>
</div>
<div className={classes.row}>
<CustomFormErrorsList />
</div>
</React.Fragment>
);
};

const useStyles = makeStyles({
row: {
display: "flex",
flexFlow: "row nowrap",
justifyContent: "space-around",
marginRight: "1em",
marginLeft: "1%"
},
title: { marginBottom: 0,
marginLeft: "1%" },
select: { flexBasis: "100%", margin: "0.5em", marginTop: "1em",
flexFlow: "row nowrap",
justifyContent: "space-around",
marginRight: "75%",
marginLeft: "1%" },
checkbox: { marginTop: "1em" },
fullWidth: { width: "25%" },
});


export default AdminReport;