-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EducationsPage (based on SchoolsPage)
- Loading branch information
BenjaVR
committed
Jan 6, 2019
1 parent
4e6d6c3
commit 433c24e
Showing
12 changed files
with
459 additions
and
247 deletions.
There are no files selected for viewing
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.
433c24e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#12
433c24e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#11