Skip to content

Commit

Permalink
added year submission check for kitchen outcomes
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesjin123 committed Jan 9, 2025
1 parent 5d7c7fe commit 96156c0
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions client/src/SubmissionForms/KitchenOutcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from '@mui/material';
import { number } from 'prop-types';
import { RootState } from '../util/redux/store';
import { getData } from '../util/api';

export default function KitchenOutcome() {
// Define the form state type
Expand Down Expand Up @@ -106,6 +107,8 @@ export default function KitchenOutcome() {
const [genderOpen, setGenderOpen] = useState(false);
const [racialOpen, setRacialOpen] = useState(false);
const [orgOpen, setOrgOpen] = useState(false);
const [yearOpen, setYearOpen] = useState(false);
const [yearError, setYearError] = useState('');
// Initialize formState with the FormState type
const noFormState: FormState = {
email: user.email,
Expand Down Expand Up @@ -174,7 +177,7 @@ export default function KitchenOutcome() {
{ label: 'November', value: '11' },
{ label: 'December', value: '12' },
];
function validateInputs() {
async function validateInputs() {
const racialPercentageSum =
formState.mealsAmericanIndian +
formState.mealsAsian +
Expand Down Expand Up @@ -202,6 +205,35 @@ export default function KitchenOutcome() {
formState.mealsSeniors +
formState.mealsAgeUnknown;
let works = true;

const year = formState.year.getFullYear();

if (
Number.isNaN(year) ||
year < 2017 ||
year > new Date().getFullYear() + 5
) {
works = false;
setYearError('The year is too old or too far in the future.');
setYearOpen(true);
} else {
try {
console.log('Checking year:', year);
const response = await getData(
`kitchen_outcomes/${year - 2}/${formState.orgId}`,
);
console.log('Response:', response);
console.log(response.data != null);
if (response.data != null && response.data !== '') {
works = false;
setYearError('A submission for this year already exists.');
setYearOpen(true);
}
} catch (error) {
console.error('Error checking year:', error);
}
}

if (racialPercentageSum !== 100) {
works = false;
setRacialOpen(true);
Expand All @@ -225,7 +257,7 @@ export default function KitchenOutcome() {
return works;
}
const handleSubmit = async () => {
if (validateInputs()) {
if (await validateInputs()) {
axios
.post('http://localhost:4000/api/kitchen_outcomes/add/', formState)
.then((response) => {
Expand Down Expand Up @@ -1329,6 +1361,19 @@ export default function KitchenOutcome() {
// eslint-disable-next-line react/jsx-no-useless-fragment
<></>
)}
{yearOpen ? (
<Alert
severity="warning"
onClose={() => {
setYearOpen(false);
}}
>
{yearError}
</Alert>
) : (
// eslint-disable-next-line react/jsx-no-useless-fragment
<></>
)}
<div
style={{
display: 'flex',
Expand Down

0 comments on commit 96156c0

Please sign in to comment.