Commit 91e4c288 by Vijay Gawariya

create recipe xml upload api

parent 2b292b16
...@@ -28,8 +28,8 @@ class Api::V1::EmployeesController < ApplicationController ...@@ -28,8 +28,8 @@ class Api::V1::EmployeesController < ApplicationController
def uploadFile def uploadFile
# print{'aaaaaaaaaaaaaaaa'} # print{'aaaaaaaaaaaaaaaa'}
print(params.permit(:file),'aaaaaaaaaaaaaaaaa') # print(params.permit(:file),'aaaaaaaaaaaaaaaaa')
render json: {name: 'vijay'} render json: {status: 'sucess'}
end end
private private
......
...@@ -26,6 +26,10 @@ class Api::V1::RecipesController < ApplicationController ...@@ -26,6 +26,10 @@ class Api::V1::RecipesController < ApplicationController
render json: { message: 'Recipe deleted!' } render json: { message: 'Recipe deleted!' }
end end
def uploadFile
render json: { status: 'sucess'}
end
private private
def recipe_params def recipe_params
......
...@@ -44,10 +44,6 @@ class NewEmployee extends React.Component { ...@@ -44,10 +44,6 @@ class NewEmployee extends React.Component {
if (file.length == 0) if (file.length == 0)
return; return;
const body = {
file
};
console.log(body);
const token = document.querySelector('meta[name="csrf-token"]').content; const token = document.querySelector('meta[name="csrf-token"]').content;
const formData = new FormData() const formData = new FormData()
formData.append("file", file) formData.append("file", file)
...@@ -58,15 +54,12 @@ class NewEmployee extends React.Component { ...@@ -58,15 +54,12 @@ class NewEmployee extends React.Component {
// "Content-Type": "application/json" // "Content-Type": "application/json"
}, },
body: formData body: formData
}) }).then(response => {
.then(response => {
if (response.ok) { if (response.ok) {
alert("AAAAAAAAAAA") this.props.history.push(`/employees`)
return response.json();
} }
throw new Error("Network response was not ok."); throw new Error("Network response was not ok.");
}) }).catch(error => console.log(error.message));
.catch(error => console.log(error.message));
} }
onSubmit(event) { onSubmit(event) {
...@@ -121,6 +114,7 @@ class NewEmployee extends React.Component { ...@@ -121,6 +114,7 @@ class NewEmployee extends React.Component {
className="form-control" className="form-control"
required required
onChange={this.uploadFile} onChange={this.uploadFile}
accept=".xml"
/> />
</div> </div>
<button type="submit" className="btn custom-button mt-3"> <button type="submit" className="btn custom-button mt-3">
......
...@@ -7,11 +7,14 @@ class NewRecipe extends React.Component { ...@@ -7,11 +7,14 @@ class NewRecipe extends React.Component {
this.state = { this.state = {
name: "", name: "",
ingredients: "", ingredients: "",
instruction: "" instruction: "",
file: {}
}; };
this.onChange = this.onChange.bind(this); this.onChange = this.onChange.bind(this);
this.onSubmit = this.onSubmit.bind(this); this.onSubmit = this.onSubmit.bind(this);
this.fileUpload = this.fileUpload.bind(this);
this.submitFile = this.submitFile.bind(this);
this.stripHtmlEntities = this.stripHtmlEntities.bind(this); this.stripHtmlEntities = this.stripHtmlEntities.bind(this);
} }
...@@ -60,8 +63,34 @@ class NewRecipe extends React.Component { ...@@ -60,8 +63,34 @@ class NewRecipe extends React.Component {
} }
fileUpload (event) { fileUpload (event) {
console.log(event); this.setState({ [event.target.name]: event.target.files[0] });
console.log('event'); }
submitFile (event) {
event.preventDefault();
const url = "/api/v1/recipes/uploadFile";
const { file } = this.state;
if (file.length == 0)
return;
const formData = new FormData();
formData.append('file', file);
const token = document.querySelector('meta[name="csrf-token"]').content;
fetch(url, {
method: "POST",
headers: {
"X-CSRF-Token": token,
},
body: formData
})
.then(response => {
if (response.ok) {
this.props.history.push(`/recipes`)
}
throw new Error("Network response was not ok.");
})
.catch(error => console.log(error.message));
} }
render() { render() {
...@@ -72,7 +101,13 @@ class NewRecipe extends React.Component { ...@@ -72,7 +101,13 @@ class NewRecipe extends React.Component {
<h1 className="font-weight-normal mb-5"> <h1 className="font-weight-normal mb-5">
Add a new recipe to our awesome recipe collection. Add a new recipe to our awesome recipe collection.
</h1> </h1>
<input onChange={this.fileUpload} type="file" accept=".xml"></input> <form onSubmit={this.submitFile}>
<input onChange={this.fileUpload} name="file" type="file" accept=".xml" required />
<button type="submit" className="btn custom-button mt-3">
Upload File
</button>
</form>
<p> Upload a file or </p> <p> Upload a file or </p>
<form onSubmit={this.onSubmit}> <form onSubmit={this.onSubmit}>
<div className="form-group"> <div className="form-group">
......
...@@ -14,6 +14,7 @@ Rails.application.routes.draw do ...@@ -14,6 +14,7 @@ Rails.application.routes.draw do
post 'recipes/create' post 'recipes/create'
get '/show/:id', to: 'recipes#show' get '/show/:id', to: 'recipes#show'
delete '/destroy/:id', to: 'recipes#destroy' delete '/destroy/:id', to: 'recipes#destroy'
post 'recipes/uploadFile'
end end
end end
root 'homepage#index' root 'homepage#index'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment