diff --git a/src/CustomerDetails.js b/src/CustomerDetails.js index c9c7be66..759113ab 100644 --- a/src/CustomerDetails.js +++ b/src/CustomerDetails.js @@ -1,56 +1,57 @@ -import React, {Component} from 'react'; +import React, { Component } from 'react'; import Panel from 'react-bootstrap/lib/Panel' -import axios from 'axios' +// Видалено: import axios from 'axios' +import { getCustomerDetailData } from './services/CustomerService'; -//This Component is a child Component of Customers Component +// This Component is a child Component of Customers Component export default class CustomerDetails extends Component { - constructor(props) { - super(props); - this.state = {} - } + constructor(props) { + super(props); + this.state = {} + } - //Function which is called when the component loads for the first time - componentDidMount() { - this.getCustomerDetails(this.props.val) - } + // Function which is called when the component loads for the first time + componentDidMount() { + this.getCustomerDetails(this.props.val) + } - //Function which is called whenver the component is updated - componentDidUpdate(prevProps) { + // Function which is called whenver the component is updated + componentDidUpdate(prevProps) { - //get Customer Details only if props has changed - if (this.props.val !== prevProps.val) { - this.getCustomerDetails(this.props.val) + // get Customer Details only if props has changed + if (this.props.val !== prevProps.val) { + this.getCustomerDetails(this.props.val) + } } - } - //Function to Load the customerdetails data from json. - getCustomerDetails(id) { - axios.get('assets/samplejson/customer' + id + '.json').then(response => { - this.setState({customerDetails: response}) - }) - }; + // Function to Load the customerdetails data from json. - тепер використовує сервіс + getCustomerDetails = (id) => { + getCustomerDetailData(id).then(response => { + this.setState({customerDetails: response}) + }) + }; - render() { - if (!this.state.customerDetails) - return (

Loading Data

) - return (
- - - {this.state.customerDetails.data.name} - - -

Name : {this.state.customerDetails.data.name}

-

Email : {this.state.customerDetails.data.email}

-

Phone : {this.state.customerDetails.data.phone}

-

City : {this.state.customerDetails.data.city}

-

State : {this.state.customerDetails.data.state}

-

Country : {this.state.customerDetails.data.country}

-

Organization : {this.state.customerDetails.data.organization}

-

Job Profile : {this.state.customerDetails.data.jobProfile}

-

Additional Info : {this.state.customerDetails.data.additionalInfo}

-
-
-
) - } -} + render() { + if (!this.state.customerDetails) + return (

Loading Data

) + return (
+ + + {this.state.customerDetails.data.name} + + +

Name : {this.state.customerDetails.data.name}

+

Email : {this.state.customerDetails.data.email}

+

Phone : {this.state.customerDetails.data.phone}

+

City : {this.state.customerDetails.data.city}

+

State : {this.state.customerDetails.data.state}

+

Country : {this.state.customerDetails.data.country}

+

Organization : {this.state.customerDetails.data.organization}

+

Job Profile : {this.state.customerDetails.data.jobProfile}

+

Additional Info : {this.state.customerDetails.data.additionalInfo}

+
+
+
) + } +} \ No newline at end of file diff --git a/src/Customers.js b/src/Customers.js index 73f9a70f..82942004 100644 --- a/src/Customers.js +++ b/src/Customers.js @@ -2,57 +2,62 @@ import React, {Component} from 'react'; import Panel from 'react-bootstrap/lib/Panel' import Button from 'react-bootstrap/lib/Button' import CustomerDetails from './CustomerDetails' -import axios from 'axios' +// Видалено: import axios from 'axios' +import { getCustomerListData } from './services/CustomerService'; export default class Customers extends Component { - constructor(props) { - super(props) - this.state = { - selectedCustomer: 1 - } - } - - //function which is called the first time the component loads - componentDidMount() { - this.getCustomerData(); - } - - //Function to get the Customer Data from json - getCustomerData() { - axios.get('assets/samplejson/customerlist.json').then(response => { - this.setState({customerList: response}) - }) - }; - - render() { - if (!this.state.customerList) - return (

Loading data

) - return (
-
- { - - this.state.customerList.data.map(customer => - - {customer.name} - - -

{customer.email}

-

{customer.phone}

- - -
-
) + constructor(props) { + super(props) + this.state = { + selectedCustomer: 1 } -
-
- -
-
) - } - -} + } + + // function which is called the first time the component loads + componentDidMount() { + this.getCustomerData(); + } + + // Function to get the Customer Data from json - тепер використовує сервіс + getCustomerData = () => { + // Викликаємо функцію з сервісу, яка робить запит + getCustomerListData().then(response => { + // Оновлюємо стан, як і раніше + this.setState({customerList: response}) + }) + // NOTE: Краще використовувати стрілочну функцію для методу класу, + // щоб уникнути проблем з 'this' при його передачі. + }; + + render() { + if (!this.state.customerList) + return (

Loading data

) + return (
+
+ { + + this.state.customerList.data.map(customer => + + {customer.name} + + +

{customer.email}

+

{customer.phone}

+ + +
+
) + } +
+
+ +
+
) + } + +} \ No newline at end of file diff --git a/src/services/CustomerService.js b/src/services/CustomerService.js new file mode 100644 index 00000000..8f0ae45e --- /dev/null +++ b/src/services/CustomerService.js @@ -0,0 +1,9 @@ +import axios from 'axios'; + +export const getCustomerListData = () => { + return axios.get('assets/samplejson/customerlist.json'); +}; + +export const getCustomerDetailData = (id) => { + return axios.get(`assets/samplejson/customer${id}.json`); +}; \ No newline at end of file