Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
7955125-front-end/src/components/editdog.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
119 lines (99 sloc)
3.67 KB
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
import React from 'react'; | |
import { Form, Input, Button, message,Upload} from 'antd'; | |
import { status, json } from '../utilities/requestHandlers'; | |
import UserContext from '../contexts/user'; | |
import {withRouter} from 'react-router-dom'; | |
import { UploadOutlined } from '@ant-design/icons'; | |
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 } }, | |
}; | |
class EditDog extends React.Component{ | |
constructor(props){ | |
super(props) | |
this.edit = this.edit.bind(this) | |
} | |
static contextType = UserContext; | |
edit(values){ | |
const id = this.props.match.params.id; | |
console.log(values) | |
values.authorID=this.context.user.ID | |
if(values.imageURL){ | |
values.imageURL=`https://melody-annex-3000.codio-box.uk${values.imageURL.file.response.links.path}` | |
} | |
const { confirm, ...data } = values; | |
fetch(`https://melody-annex-3000.codio-box.uk/api/v1/dogs/${id}`, { | |
method: "PUT", | |
body: JSON.stringify(data), | |
headers: { | |
"Authorization": this.context.user.Authorization, | |
"Content-Type":"application/json" | |
} | |
}) | |
.then(status) | |
.then(json) | |
.then(data => { | |
console.log("updated succesfully"); | |
console.log(data) | |
this.props.history.push(`/dog/${id}`); | |
}) | |
.catch(errorResponse => { | |
alert("Update failed" +errorResponse) | |
console.log('Update failed'); | |
alert(`Error: ${JSON.stringify(errorResponse)}`); | |
}); | |
}; | |
render() { | |
if (this.context.user.role!=='admin'){ | |
this.props.history.push("/"); | |
} | |
const uploadProps = { | |
name: 'upload', | |
action: 'https://melody-annex-3000.codio-box.uk/api/v1/images', | |
headers: { | |
authorization: this.context.user.Authorization | |
}, | |
onChange(info) { | |
if (info.file.status !== 'uploading') { | |
console.log(info.file, info.fileList); | |
} | |
if (info.file.status === 'done') { | |
message.success(`${info.file.name} file uploaded successfully`); | |
} else if (info.file.status === 'error') { | |
message.error(`${info.file.name} file upload failed.`); | |
} | |
}, | |
}; | |
return ( | |
<Form {...formItemLayout} name="update" onFinish={this.edit} scrollToFirstError > | |
<Form.Item name="Name" label="Name"> | |
<Input /> | |
</Form.Item> | |
<Form.Item name="Breed" label="Breed"> | |
<Input /> | |
</Form.Item> | |
<Form.Item name="Age" label="Age"> | |
<Input /> | |
</Form.Item> | |
<Form.Item name="imageURL" label="imageURL"> | |
<Upload {...uploadProps}> | |
<Button icon={<UploadOutlined />}>Click to Upload</Button> | |
</Upload> | |
</Form.Item> | |
<Form.Item name="Colour" label="Colour"> | |
<Input /> | |
</Form.Item> | |
<Form.Item name="about" label="About"> | |
<Input /> | |
</Form.Item> | |
<Form.Item {...tailFormItemLayout}> | |
<Button type="primary" htmlType="submit">Edit</Button> | |
</Form.Item> | |
</Form> | |
); | |
}; | |
}; | |
export default withRouter(EditDog); |