Advertisement
langbung01

Untitled

Dec 19th, 2018
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { connect } from 'react-redux';
  4. import swal from 'sweetalert2';
  5.  
  6. import BusinessListOwner from '../components/BusinessListOwner';
  7. import BusinessNewOwnerModal from '../components/BusinessNewOwnerModal';
  8. import axios from "axios";
  9.  
  10. class BusinessOwner extends Component {
  11.   constructor(props) {
  12.     super(props);
  13.     this.state = {
  14.       bizRoleUser:{},
  15.       bizRole:[],
  16.       openModal: false,
  17.     };
  18.   }
  19.   handleModal = (openModel) => {
  20.     this.setState({
  21.       bussiness_owner:[],
  22.       bussiness_role:[],
  23.       openModal: openModel,
  24.     });
  25.   };
  26.  
  27.     componentDidMount() {
  28.       this.getRoleOfBiz();
  29.     }
  30.  
  31.  
  32.     //get role form this api
  33.     getRoleOfBiz= async ()=>{
  34.         const bizID = this.props.business.business.id;
  35.         const roleDataApi = await axios.get(`/get_role_biz/${bizID}`);
  36.         const roles = roleDataApi.data.data.map((position)=>(position.name));
  37.         this.setState({
  38.             bizRole:roles
  39.         });
  40.     }
  41.  
  42.   getRoleEachUser=(ownerList, roleList) =>{
  43.       let ownerInfo={};
  44.       let isFound;
  45.       let accountWithRole = ownerList.map((owner) => {
  46.           isFound=false;
  47.           ownerInfo={};
  48.           roleList.forEach((role)=>{
  49.               if(owner.id===role.account_id){
  50.                   isFound=true;
  51.                   Object.assign(ownerInfo,owner,{biz_role:role.getrole[0].name});
  52.                   // console.log(ownerInfo);
  53.               }
  54.           });
  55.           if(isFound)
  56.               return ownerInfo;
  57.           return Object.assign(ownerInfo,owner,{biz_role:"-"});
  58.       });
  59.       return accountWithRole;
  60.   }
  61.  
  62.   handleUnassignedOwner = async (id) => {
  63.     const url = '/service/request_business';
  64.     const businessId = document.querySelector('meta[name="account-id"]').getAttribute('content');
  65.     try {
  66.       await axios.post(url, {
  67.         businessId,
  68.         type: 'UNASSIGNED_OWNER',
  69.         ownerId: [id],
  70.       });
  71.       swal({
  72.         title: 'request success',
  73.         type: 'success',
  74.         showConfirmButton: false,
  75.         timer: 3000,
  76.       });
  77.     } catch (e) {
  78.       console.log(e);
  79.     }
  80.   };
  81.  
  82.   render() {
  83.       console.log(this.state.bizRole);
  84.       const bizOwner = this.props.business.business.business_owner;
  85.       const bizRole = this.props.business.business.business_roles;
  86.       this.state.bizRoleUser=this.getRoleEachUser(bizOwner,bizRole);
  87.     return (
  88.       <div className="row">
  89.         <div className="col-lg-12">
  90.           <div className="ibox float-e-margins">
  91.             <div className="ibox-title">
  92.               <h5>ข้อมูลบัญชีนิติบุคคล</h5>
  93.               <div className="ibox-tools">
  94.                 <a className="btn btn-xs btn-primary" onClick={() => this.handleModal(true)}>
  95.                   <i className="fa fa-pencil" />
  96.                   {' '}เพิ่มพนักงาน
  97.                 </a>
  98.               </div>
  99.             </div>
  100.             <div className="ibox-content">
  101.               <table className="table table-hover">
  102.                 <thead>
  103.                   <tr>
  104.                     <th>#</th>
  105.                     <th>ลำดับที่</th>
  106.                     <th>ชื่อ</th>
  107.                     <th>นามสกุล</th>
  108.                     <th>ตำแหน่ง</th>
  109.                     <th>อีเมล</th>
  110.                     <th />
  111.                   </tr>
  112.                 </thead>
  113.                 <BusinessListOwner
  114.                   accounts={this.state.bizRoleUser}
  115.                   handleUnassignedOwner={this.handleUnassignedOwner}
  116.                 />
  117.               </table>
  118.             </div>
  119.           </div>
  120.         </div>
  121.         <BusinessNewOwnerModal
  122.             business={this.props.business.business}
  123.             bizRole={this.state.bizRole} //pass here
  124.             openModal={this.state.openModal}
  125.             handleModal={this.handleModal}
  126.         />
  127.       </div>
  128.     );
  129.   }
  130. }
  131.  
  132. BusinessOwner.propTypes = {
  133.   business: PropTypes.shape({
  134.     business: PropTypes.object,
  135.   }).isRequired,
  136. };
  137. BusinessOwner.defaultProps = {};
  138.  
  139. function mapStateToProps({ business }) {
  140.   return { business };
  141. }
  142.  
  143. export default connect(mapStateToProps)(BusinessOwner);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement