Commit b5e494f7 by Vijay Gawariya

add employee through xml file and redirect on employee show page and redirect…

add employee through xml file and redirect on employee show page and redirect recipee on show recipee page
parent d2770ae9
...@@ -30,8 +30,18 @@ class Api::V1::EmployeesController < ApplicationController ...@@ -30,8 +30,18 @@ class Api::V1::EmployeesController < ApplicationController
@filename = params[:file]; @filename = params[:file];
@lines = File.read(@filename.path) @lines = File.read(@filename.path)
doc = Nokogiri.XML(@lines, nil, 'utf-8') doc = Nokogiri.XML(@lines, nil, 'utf-8')
print doc firstName = doc.search('FIRSTNAME').text
render json: { name: 'vijay' } lastName = doc.search('LASTNAME').text
email = doc.search('EMAIL').text
phone = doc.search('PHONE').text
dob = doc.search('DOB').text
address = doc.search('ADDRESS').text
employee = Employee.create!(firstName: firstName, lastName: lastName, email: email, phone: phone, dob: dob, address: address)
if(employee)
render json: employee
else
reader json: employee.errors
end
end end
private private
......
...@@ -56,10 +56,11 @@ class NewEmployee extends React.Component { ...@@ -56,10 +56,11 @@ class NewEmployee extends React.Component {
body: formData body: formData
}).then(response => { }).then(response => {
if (response.ok) { if (response.ok) {
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)); }).then(response => this.props.history.push(`/employee/${response.id}`))
.catch(error => console.log(error.message));
} }
onSubmit(event) { onSubmit(event) {
......
...@@ -86,43 +86,43 @@ class NewRecipe extends React.Component { ...@@ -86,43 +86,43 @@ class NewRecipe extends React.Component {
}) })
.then(response => { .then(response => {
if (response.ok) { if (response.ok) {
this.props.history.push(`/recipes`) return response.json()
} }
throw new Error("Network response was not ok."); throw new Error("Network response was not ok.");
}) }).then(response => this.props.history.push(`/recipe/${response.id}`))
.catch(error => console.log(error.message)); .catch(error => console.log(error.message));
} }
submitFile(event) { // submitFile(event) {
event.preventDefault(); // event.preventDefault();
const url = "/api/v1/recipes/uploadFile"; // const url = "/api/v1/recipes/uploadFile";
const { file } = this.state; // const { file } = this.state;
if (file.length == 0) // if (file.length == 0)
return; // return;
const body = { file }; // const body = { file };
console.log(body); // 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)
fetch(url, { // fetch(url, {
method: "POST", // method: "POST",
headers: { // headers: {
"X-CSRF-Token": token, // "X-CSRF-Token": token,
}, // },
body: formData // body: formData
}) // })
.then(response => { // .then(response => {
if (response.ok) { // if (response.ok) {
alert("AAAAAAAAAAA") // alert("AAAAAAAAAAA")
// return response.json(); // // return response.json();
console.log(response.json); // console.log(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));
} // }
render() { render() {
return ( return (
......
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