Skip to content

Commit

Permalink
Properly type form values in CompanySemesterGUI form
Browse files Browse the repository at this point in the history
  • Loading branch information
eikhr committed Oct 21, 2023
1 parent 97863d0 commit 26047cc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/actions/CompanyActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export function addSemester({
},
meta: {
errorMessage: 'Legge til semester feilet',
successMessage: 'Semest lagt til!',
successMessage: 'Semester lagt til!',
},
})
);
Expand Down
2 changes: 1 addition & 1 deletion app/components/CommentForm/CommentForm.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
margin: 0;
}

.submittable {
.submitButton:disabled {
opacity: 0;
transition: opacity var(--easing-medium);
}
4 changes: 3 additions & 1 deletion app/components/CommentForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ const CommentForm = ({
/>
</div>

<SubmitButton>{submitText}</SubmitButton>
<SubmitButton className={styles.submitButton}>
{submitText}
</SubmitButton>
</Flex>

<SubmissionError />
Expand Down
10 changes: 8 additions & 2 deletions app/components/Form/SubmitButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import type { MouseEventHandler, ReactNode } from 'react';
type Props = {
children: ReactNode;
onClick?: MouseEventHandler<HTMLButtonElement>;
className?: string;
};

export const SubmitButton = ({ children, onClick }: Props) =>
export const SubmitButton = ({ children, onClick, className }: Props) =>
spySubmittable((submittable) => (
<Button submit disabled={!submittable} onClick={onClick}>
<Button
submit
disabled={!submittable}
onClick={onClick}
className={className}
>
{children}
</Button>
));
25 changes: 18 additions & 7 deletions app/routes/companyInterest/components/CompanySemesterGUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SubmissionError from 'app/components/Form/SubmissionError';
import { SubmitButton } from 'app/components/Form/SubmitButton';
import Icon from 'app/components/Icon';
import Flex from 'app/components/Layout/Flex';
import { Semester } from 'app/store/models';
import type CompanySemester from 'app/store/models/CompanySemester';
import { createValidator, required } from 'app/utils/validation';
import { semesterToText, SemesterNavigation } from '../utils';
Expand All @@ -22,9 +23,16 @@ type Props = {
editSemester: (semester: CompanySemester) => Promise<void>;
addSemester: (semester: Omit<CompanySemester, 'id'>) => Promise<void>;
activeSemesters: Array<CompanySemester>;
initialValues: Record<string, any>;
initialValues: Partial<FormValues>;
};

type FormValues = {
year: string;
semester: Semester;
};

const TypedLegoForm = LegoFinalForm<FormValues>;

const validate = createValidator({
year: [required()],
semester: [required()],
Expand Down Expand Up @@ -68,7 +76,10 @@ const AddSemesterForm = ({
editSemester,
initialValues,
}: AddSemesterProps) => {
const onSubmit = async ({ year, semester }, form: FormApi) => {
const onSubmit = async (
{ year, semester }: FormValues,
form: FormApi<FormValues>
) => {
const existingCompanySemester = semesters.find((companySemester) => {
return (
Number(companySemester.year) === Number(year) &&
Expand All @@ -82,15 +93,15 @@ const AddSemesterForm = ({
});
else
await addSemester({
year,
year: Number(year),
semester,
}); // Default is activeInterestForm: true

form.restart();
};

return (
<LegoFinalForm
<TypedLegoForm
onSubmit={onSubmit}
validate={validate}
initialValues={initialValues}
Expand All @@ -113,21 +124,21 @@ const AddSemesterForm = ({
name="Spring"
label="Vår"
component={RadioButton.Field}
inputValue="spring"
inputValue={Semester.Spring}
/>
<Field
name="Autumn"
label="Høst"
component={RadioButton.Field}
inputValue="autumn"
inputValue={Semester.Autumn}
/>
</MultiSelectGroup>

<SubmissionError />
<SubmitButton>Legg til semester</SubmitButton>
</Form>
)}
</LegoFinalForm>
</TypedLegoForm>
);
};

Expand Down

0 comments on commit 26047cc

Please sign in to comment.