- import React from 'react'
-import UserContext from '../contexts/user'
-import { Form, Input, Button, Row,Col, DatePicker } from 'antd'
-import { status, json } from '../utilities/requestHandlers'
-
-const formItemLayout = {
- labelCol: { xs: { span: 24 }, sm: { span: 6 } },
- wrapperCol: { xs: { span: 24 }, sm: { span: 12 } }
- }
-const tailFormItemLayout = {
-wrapperCol: { xs: { span: 24, offset: 0 }, sm: { span: 16, offset: 6 } },
-}
-const { TextArea } = Input
-// Field Rules for input validation in accordance with server-side schema:
-const titleRule=[
- {required:true,message:'Please type the name of the compnay.',whitespace:false, min:5,max:150}
-]
-const crnRule=[
- {required:true,message:'Please type the company reference number.',whitespace:false, min:8,max:8}
-]
-const addressRule=[
- {required:true,message:'Please enter the address where the company is located.',whitespace:false, min:5,max:32}
-]
-const emailRule=[
- {required:true,message:'Please enter the company email.',whitespace:false, min:5,max:32}
-]
-const dateFoundedRule=[
- {required:true,message:'Please enter the foundation date of the company.',whitespace:false}
-]
-/**
- * Form to add new application:
- */
-class NewApplication extends React.Component{
-
- constructor(props){
- super(props)
- this.state={
- companyName:"",
- crn:"",
- address:"",
- email:"",
- dateFunded:"",
- imageURL:""
- }
- this.handleSubmit =this.handleSubmit.bind(this)
- this.handleFile =this.handleFile.bind(this)
- }
- static contextType = UserContext
-
-/**
- * @function
- * @name handleSubmit
- * Function that handles the input of the user and passes that data to the DB
- * @param {object} values - Values obtained from form fields
- * @returns {object} Redirects user to the main page and creates the application
-*/
- handleSubmit =(values)=>{
- // creating form-data object
- const formData =new FormData()
- const {companyName,crn,address,email,dateFunded}=values
- console.log(values)
- const username=this.context.user.username
- const password=this.context.user.password
- console.log(values)
- this.setState({companyName:companyName})
- this.setState({crn:crn})
- this.setState({address:address})
- this.setState({email:email})
- this.setState({dateFunded:dateFunded})
- formData.append('companyName',this.state.companyName)
- formData.append('crn',this.state.crn)
- formData.append('address',this.state.address)
- formData.append('email',this.state.email)
- formData.append('dateFunded',this.state.dateFunded)
- formData.append('imageURL',this.state.imageURL)
- fetch('https://animal-hello-3000.codio-box.uk/api/v1/applications',{
- method:"POST",
- body:formData,
- headers:{
- "Authorization": "Basic " + btoa(username + ":" + password),
- }
- })
- .then(status)
- .then(json)
- .then(application=>{
- alert("Successfully added new application")
- this.setState({application:application})
- this.setState({redirect:"/"})// change this to all users applications pages
- })
- .catch(err => {
- alert("Error trying to add new application")
- console.log(err)
- })
- }
-
-/**
- * @function
- * @name handleFile
- * @param {object} e event to hanldle (file upload in this case)
- * @return {null} nothing return but component state is changes
-*/
- handleFile =(e)=>{
- let files = e.target.files[0]
- console.log(files)
- this.setState({upload:e.target.files[0]})
- }
-
-/** Form to get the input values to create the application */
- render(){
- return(
- <>
- <Row type="flex" justify="space-around" align="middle">
- <Col>
- <h1>Create New Application</h1>
- </Col>
- </Row>
- <Form {... formItemLayout} onFinish={this.handleSubmit}>
- <Form.Item name="companyName" label="Name of the Company" rules={titleRule}>
- <Input />
- </Form.Item>
- <Form.Item name="crn" label="Company Reference Number" rules={crnRule}>
- <TextArea showCount={true} maxLength={8}/>
- </Form.Item>
- <Form.Item name="address" label="Address" rules={addressRule}>
- <TextArea showCount={true} maxLength={500}/>
- </Form.Item>
- <Form.Item name="email" label="Email" rules={emailRule}>
- <TextArea showCount={true} maxLength={500}/>
- </Form.Item>
- <Form.Item name="dateFunded" label="Foundation Date">
- <DatePicker onChange={this.state.dateFunded} name="dateFunded" label="Foundation Date" rules={dateFoundedRule} />
- </Form.Item>
- <Form.Item name="imageURL" label="Application Image">
- <Input type="file" onChange={this.handleFile} accept="image/png, image/jpeg"/>
- </Form.Item>
- <Form.Item {...tailFormItemLayout}>
- <Button type="primary" htmlType="submit">Add new Application</Button>
- </Form.Item>
- </Form>
- </>
- )
- }
-}
-
-export default NewApplication
-
-