Skip to content

Commit

Permalink
EducationsPage (based on SchoolsPage)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaVR committed Jan 6, 2019
1 parent 4e6d6c3 commit 433c24e
Show file tree
Hide file tree
Showing 12 changed files with 459 additions and 247 deletions.
File renamed without changes.
180 changes: 0 additions & 180 deletions web/src/components/EducationsTable.tsx

This file was deleted.

1 change: 1 addition & 0 deletions web/src/components/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const Root: React.FunctionComponent = () => (
<PrivateRoute exact={true} path={routes.planningsRoute.url} component={routes.planningsRoute.component} />
<PrivateRoute exact={true} path={routes.studentsRoute.url} component={routes.studentsRoute.component} />
<PrivateRoute exact={true} path={routes.schoolsRoute.url} component={routes.schoolsRoute.component} />
<PrivateRoute exact={true} path={routes.educationsRoute.url} component={routes.educationsRoute.component} />
<PrivateRoute exact={true} path={routes.departmentsRoute.url} component={routes.departmentsRoute.component} />

<Redirect to={routes.logInRoute.url} />
Expand Down
118 changes: 118 additions & 0 deletions web/src/components/educations/EducationsFormModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { Form, Input, Modal } from "antd";
import { FormComponentProps } from "antd/lib/form";
import FormItem from "antd/lib/form/FormItem";
import React from "react";
import { FormValidationTrigger } from "../../helpers/types";
import { IEducation } from "../../models/Education";

interface IEducationFormModalProps {
title: string;
okText: string;
isVisible: boolean;
educationToEdit: IEducation | undefined;
onCloseRequest: () => void;
submitEducation(education: IEducation): Promise<void>;
}

type EducationFormModalProps = IEducationFormModalProps & FormComponentProps;

interface IEducationFormModalState {
isSubmitting: boolean;
formValidateTrigger: FormValidationTrigger;
}

class EducationFormModal extends React.Component<EducationFormModalProps, IEducationFormModalState> {

constructor(props: EducationFormModalProps) {
super(props);

this.state = {
isSubmitting: false,
formValidateTrigger: "",
};

this.doClose = this.doClose.bind(this);
this.handleOk = this.handleOk.bind(this);
}

public componentDidUpdate(prevProps: EducationFormModalProps): void {
// Populate the form if it is just opened and if a education to edit is passed.
if (this.props.educationToEdit !== undefined
&& this.props.isVisible === true
&& prevProps.isVisible === false) {
this.props.form.setFieldsValue({
name: this.props.educationToEdit.name,
});
}
}

public render(): React.ReactNode {
const { getFieldDecorator } = this.props.form;

return (
<Modal
visible={this.props.isVisible}
title={this.props.title}
onCancel={this.doClose}
onOk={this.handleOk}
okText={this.props.okText}
confirmLoading={this.state.isSubmitting}
>
<Form>
<FormItem>
{getFieldDecorator<IEducation>("name", {
validateTrigger: this.state.formValidateTrigger,
rules: [
{ required: true, message: "Naam mag niet leeg zijn" },
],
})(
<Input
autoFocus={true}
placeholder="Naam"
disabled={this.state.isSubmitting}
/>,
)}
</FormItem>
</Form>
</Modal>
);
}

private doClose(): void {
this.props.form.resetFields();
this.props.onCloseRequest();
}

private handleOk(): void {
// Do real-time validation (while typing) only after the first submit.
this.setState({
formValidateTrigger: "onChange",
});

const fields: Array<keyof IEducation> = ["name"];
this.props.form.validateFieldsAndScroll(fields, (errors, values) => {
if (!errors) {
this.setState({
isSubmitting: true,
});

const education: IEducation = {
...values,
};
this.props.submitEducation(education)
.then(() => {
this.doClose();
})
.finally(() => {
this.setState({
isSubmitting: false,
});
});
}
});
}
}

const WrappedEducationFormModal = Form.create<IEducationFormModalProps>()(EducationFormModal);

export default WrappedEducationFormModal;
Loading

2 comments on commit 433c24e

@BenjaVR
Copy link
Owner

@BenjaVR BenjaVR commented on 433c24e Jan 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#12

@BenjaVR
Copy link
Owner

@BenjaVR BenjaVR commented on 433c24e Jan 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#11

Please sign in to comment.