Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.68 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import { Button, Table, ThemedAppbar, MinimizedAppbar, LeaveAnnualModal, LinearProgress, DialogError, DialogErrorHelper } from '../../components';
  4. import { leave_getLeaveRequest, leave_submit, leave_cancel } from '../../services/dataService';
  5. import { Link } from 'react-router-dom';
  6. import MuiPickersUtilsProvider from 'material-ui-pickers/utils/MuiPickersUtilsProvider';
  7. import DateFnsUtils from 'material-ui-pickers/utils/date-fns-utils';
  8. import { DatePicker } from 'material-ui-pickers';
  9. import { Add as AddIcon } from '@material-ui/icons';
  10. import moment from 'moment';
  11. import axios from 'axios';
  12. import MediaQuery from 'react-responsive';
  13.  
  14. const mapStateToProps = state => {
  15. return {
  16. dataSourceLeave: state.leave.leaveRequest.dataSourceLeave,
  17. startDateLeave: state.leave.leaveRequest.startDate,
  18. endDateLeave: state.leave.leaveRequest.endDate
  19. };
  20. };
  21.  
  22. const mapDispatchToProps = dispatch => {
  23. return {
  24. saveStartDateLeaveRequest: startDate => {
  25. dispatch({
  26. type: 'LEAVE_VIEWER-SAVED_STARTDATE_LEAVE',
  27. payload: startDate
  28. });
  29. },
  30. saveEndDateLeaveRequest: endDate => {
  31. dispatch({
  32. type: 'LEAVE_VIEWER-SAVED_END-DATE-LEAVE',
  33. payload: endDate
  34. });
  35. },
  36. saveDataSourceLeaveRequest: dataSource => {
  37. dispatch({
  38. type: 'LEAVE_VIEWER-LEAVE_EMPLOYEE_DATASOURCE',
  39. payload: dataSource
  40. });
  41. }
  42. };
  43. };
  44.  
  45. class LeaveView extends Component {
  46. constructor(props) {
  47. super(props);
  48. this.state = {
  49. tabChosen: 0,
  50. isFetchingAPI: false,
  51. isOpenLeaveAnnual: false
  52. };
  53.  
  54. this.state = DialogErrorHelper.registerState(this.state);
  55. this.signal = axios.CancelToken.source();
  56. this.tabList = ['Leave Request', 'Leave Balance'];
  57. }
  58.  
  59. extTabChanged = de_tab => {
  60. this.setState({
  61. tabChosen: de_tab
  62. });
  63. };
  64.  
  65. extMenuChanged = de_menu => {
  66. this.setState({
  67. tabChosen: de_menu
  68. });
  69. };
  70.  
  71. //handle datetime change from startDate
  72. handleDateStartChange = async startDate => {
  73. await this.props.saveStartDateLeaveRequest(startDate);
  74. await this.leaveViewerTableIntegrateRefresh();
  75. };
  76.  
  77. //handle datetime change from untilDate
  78. handleDateEndChange = async endDate => {
  79. await this.props.saveEndDateLeaveRequest(endDate);
  80. await this.leaveViewerTableIntegrateRefresh();
  81. };
  82.  
  83. //leave Viewer Foreach and add Column Action button
  84. leaveViewerTableIntegrationAction = dataSource => {
  85. let modifiedDataSource = [];
  86. let ButtonTypeStatus;
  87.  
  88. dataSource.forEach(row => {
  89. //checked statusid
  90. switch (row.statusId) {
  91. case 0: // case entry status
  92. case 2: // case revised status
  93. ButtonTypeStatus = (
  94. <React.Fragment>
  95. <Button color='primary' onClick={() => this.onSubmitLeave(row.id)}>
  96. Submit
  97. </Button>
  98. <Link className='btn-remove-link' to={'/leave/edit/' + row.id}>
  99. <Button color='primary'>Edit</Button>
  100. </Link>
  101. <Button color='primary' onClick={() => this.onCancelLeave(row.id)}>
  102. Cancel
  103. </Button>
  104. </React.Fragment>
  105. );
  106. break;
  107. case 1: // case submitted status
  108. ButtonTypeStatus = (
  109. <React.Fragment>
  110. <Link className='btn-remove-link' to={'/leave/edit/' + row.id}>
  111. <Button color='primary'>View</Button>
  112. </Link>
  113. <Button color='primary' onClick={() => this.onCancelLeave(row.id)}>
  114. Cancel
  115. </Button>
  116. </React.Fragment>
  117. );
  118. }
  119. modifiedDataSource.push(
  120. Object.assign({}, row, {
  121. action: <React.Fragment>{ButtonTypeStatus}</React.Fragment>
  122. })
  123. );
  124. });
  125. return modifiedDataSource;
  126. };
  127.  
  128. //leave viewet Execute API / intergrate API into Table
  129. leaveViewerTableIntegrateRefresh = () => {
  130. const { isFetchingAPI } = this.state;
  131. const { startDateLeave, endDateLeave } = this.props;
  132. let paramBody = {
  133. startDate: moment(startDateLeave).format('YYYY-MM-DD'),
  134. endDate: moment(endDateLeave).format('YYYY-MM-DD')
  135. };
  136. if (!isFetchingAPI) {
  137. this.setState({ isFetchingAPI: true });
  138. leave_getLeaveRequest(paramBody, this.signal.token)
  139. .then(response => {
  140. this.setState({ isFetchingAPI: false });
  141. this.props.saveDataSourceLeaveRequest(response);
  142. })
  143. .catch(() => {
  144. this.setState(
  145. DialogErrorHelper.setToggleDialog(
  146. {
  147. isFetchingAPI: false
  148. },
  149. <p>Error</p>,
  150. <p>Exception</p>
  151. )
  152. );
  153. });
  154. }
  155. };
  156.  
  157. // onSubmitLeave = id => {
  158. // const {isFetchingAPI} = this.state;
  159.  
  160. // if(isFetchingAPI){
  161. // this.setState({isFetchingAPI : true}, () => {
  162. // leave_submit(id, this.setState.token)
  163. // .then(submitResponse => {
  164. // console.log({ submitResponse });
  165. // this.leaveViewerTableIntegrateRefresh();
  166. // })
  167. // .catch(submit_exception => {
  168. // console.log({ submit_exception });
  169. // this.setState({ isFetchingAPI: false });
  170. // });
  171. // })
  172. // }
  173. // };
  174.  
  175. // onCancelLeave = id => {
  176. // const { isFetchingAPI } = this.state;
  177.  
  178. // if (isFetchingAPI) {
  179. // this.setState({ isFetchingAPI: true }, () => {
  180. // leave_cancel(id, this.setState.token)
  181. // .then(cancelResponse => {
  182. // console.log({ cancelResponse });
  183. // this.leaveViewerTableIntegrateRefresh();
  184. // })
  185. // .catch(cancelation_exception => {
  186. // console.log({ cancelation_exception });
  187. // this.setState({ isFetchingAPI: false });
  188. // });
  189. // });
  190. // }
  191. // };
  192.  
  193. onSubmitLeave = id => {
  194. if (this.state.isFetchingAPI) return false;
  195.  
  196. this.setState({ isFetchingAPI: true }, () => {
  197. const param = { id };
  198. leave_submit(param, this.signal.token)
  199. .then(submit_response => {
  200. // seperitnya tidak perlu set fetching false, karena ini ada fetching lagi
  201. console.log({ submit_response });
  202. this.getLeaveEmployeeFromAPI();
  203.  
  204. // tidak bisa sama dengan leave editor karnea tampilan beda
  205. // console.log({submit_response});
  206. // this.setState(
  207. // {
  208. // isFetchingAPI: false,
  209. // statusId: submit_response.data.statusId,
  210. // status: submit_response.data.status,
  211. // },
  212. // this
  213. // )
  214. })
  215. .catch(submit_exception => {
  216. console.log({ submit_exception });
  217. this.setState({ isFetchingAPI: false });
  218. });
  219. });
  220. };
  221.  
  222. onCancelLeave = id => {
  223. if (this.state.isFetchingAPI) return false;
  224.  
  225. this.setState({ isFetchingAPI: true }, () => {
  226. const param = { id };
  227. leave_cancel(param, this.signal.token)
  228. .then(cancelation_response => {
  229. console.log({ cancelation_response });
  230. this.getLeaveEmployeeFromAPI();
  231. })
  232. .catch(cancelation_exception => {
  233. console.log({ cancelation_exception });
  234. this.setState({ isFetchingAPI: false });
  235. });
  236. });
  237. };
  238.  
  239. onShowAnnualLeave = () => {
  240. this.setState({ isOpenLeaveAnnual: true });
  241. };
  242.  
  243. onCloseAnnualLeave = () => {
  244. this.setState({ isOpenLeaveAnnual: false });
  245. };
  246.  
  247. componentDidMount() {
  248. this.leaveViewerTableIntegrateRefresh();
  249. }
  250.  
  251. render() {
  252. const { startDateLeave, endDateLeave, dataSourceLeave } = this.props;
  253. const { isFetchingAPI } = this.state;
  254.  
  255. return (
  256. <React.Fragment>
  257. <MuiPickersUtilsProvider utils={DateFnsUtils}>
  258. <div className='container-fluid no-breadcrumb'>
  259. {/* Row Box */}
  260. <div className='row'>
  261. <div className='col-md-12'>
  262. <div className='box box-default' style={{ marginBottom: '0px' }}>
  263. {/* Header */}
  264. <div className='box-header' style={{ paddingLeft: '0px', paddingRight: '0px' }}>
  265. {/* Untuk bagian header yg text dan button, sepertinya masih bisa diakali pakai bootstrap daripada dibuatkan komponen baru. Tapi spertinya lebih mending dibuatkan komponen sendiri karnea button pakai float (masih kurang paham cara kerja float) */}
  266. {/* Header Big */}
  267. <MediaQuery minDeviceWidth={1025}>
  268. <div className='col-md-12' style={{ paddingLeft: '1.25em', paddingRight: '1.25em' }}>
  269. <LeaveAnnualModal modalOpen={this.state.isOpenLeaveAnnual} onCloseAnnualLeave={this.onCloseAnnualLeave} />
  270. <Button
  271. variant='contained'
  272. className='S_Leave_leaveEditor_btn-theme-solid'
  273. style={{ float: 'right', marginTop: '10px', marginRight: '20px' }}
  274. onClick={this.onShowAnnualLeave}
  275. >
  276. Show Annual Leave
  277. </Button>
  278.  
  279. <Button
  280. variant='contained'
  281. style={{
  282. color: 'white',
  283. backgroundColor: '#F17C16',
  284. float: 'right',
  285. marginTop: '10px',
  286. marginRight: '20px',
  287. paddingLeft: '8px'
  288. }}
  289. >
  290. <AddIcon style={{ marginRight: '5px' }} /> New Leave
  291. </Button>
  292. <h5 style={{ fontWeight: 'bold' }}>Leave</h5>
  293. <p>Lorem Ipsum Dummy Text</p>
  294. </div>
  295.  
  296. {/* Tabs */}
  297. <div className='col-md-12' style={{ paddingLeft: '0px', paddingRight: '0px' }}>
  298. <ThemedAppbar onTabChange={this.extTabChanged} currentTab={this.state.tabChosen} tabList={this.tabList} />
  299. </div>
  300. {/* End of Tabs */}
  301. </MediaQuery>
  302. {/* End of Header Big */}
  303.  
  304. {/* Header Small */}
  305. <MediaQuery maxDeviceWidth={1024}>
  306. <div className='col-md-12' style={{ paddingLeft: '0.75em', paddingRight: '0.75em' }}>
  307. <Button
  308. variant='contained'
  309. style={{
  310. color: 'white',
  311. backgroundColor: '#F17C16',
  312. float: 'right',
  313. marginTop: '10px',
  314. marginRight: '20px',
  315. paddingLeft: '8px'
  316. }}
  317. >
  318. <AddIcon style={{ marginRight: '5px' }} /> New Leave
  319. </Button>
  320. {/* Ini entah perlu teks atau tidak */}
  321. <h6 style={{ fontWeight: 'bold' }}>Leave</h6>
  322. <p>Lorem Ipsum Dolores</p>
  323. </div>
  324.  
  325. <div className='col-md-12' style={{ paddingLeft: '0px', paddingRight: '0px' }}>
  326. {/* appbar ini ada opsi supaya bisa tetap ada walau di-scroll. Mungkin fitur tersebut bisa dipakai */}
  327. <MinimizedAppbar onMenuChange={this.extMenuChanged} currentMenu={this.state.tabChosen} menuList={this.tabList} />
  328. </div>
  329. </MediaQuery>
  330. {/* End of Header Small */}
  331. </div>
  332. {/* End of Header */}
  333.  
  334. {/* Body */}
  335. <div className='box-body'>
  336. {/* Leave Request */}
  337. {this.state.tabChosen === 0 && (
  338. <React.Fragment>
  339. <div className='row'>
  340. <div className='col-md-7'>
  341. {/* Date Range */}
  342. <div className='row'>
  343. <div className='col-md-3' style={{ height: '50px', marginTop: '10px' }}>
  344. Date Range:
  345. </div>
  346.  
  347. <div className='col-md-9'>
  348. <DatePicker
  349. disabled={isFetchingAPI}
  350. autoOk
  351. animateYearScrolling={false}
  352. label='Start Date'
  353. showTodayButton
  354. value={moment(startDateLeave).format('DD MMMM YYYY')}
  355. onChange={event => this.handleDateStartChange(event)}
  356. style={{ marginRight: '20px', width: '47%' }}
  357. format='YYYY/MM/DD'
  358. />
  359.  
  360. <DatePicker
  361. disabled={isFetchingAPI}
  362. autoOk
  363. animateYearScrolling={false}
  364. label='Until Date'
  365. showTodayButton
  366. value={moment(endDateLeave).format('DD MMMM YYYY')}
  367. onChange={event => this.handleDateEndChange(event)}
  368. format='YYYY/MM/DD'
  369. style={{ width: '48%' }}
  370. />
  371. </div>
  372. </div>
  373. {/* End of DateRange */}
  374. </div>
  375. </div>
  376. <div className='row'>
  377. <div className='col-md-12'>
  378. {/* Table */}
  379. <div className='mt-4'>
  380. <hr />
  381. {isFetchingAPI && <LinearProgress />}
  382. <Table
  383. pageSize={20}
  384. columns={[
  385. { name: 'code', title: 'Code' },
  386. { name: 'userEntry', title: 'User Entry' },
  387. { name: 'employee', title: 'Employee' },
  388. { name: 'leaveType', title: 'Leave Type' },
  389. { name: 'action', title: '#' }
  390. ]}
  391. rows={this.leaveViewerTableIntegrationAction(dataSourceLeave)}
  392. columnExtensions={[
  393. { columnName: 'code', width: 300, wordWrapEnabled: true },
  394. { columnName: 'userEntry', width: 250, wordWrapEnabled: true },
  395. { columnName: 'employee', width: 250, wordWrapEnabled: true },
  396. { columnName: 'leaveType', width: 200, wordWrapEnabled: true },
  397. { columnName: 'action', width: 250 }
  398. ]}
  399. />
  400. </div>
  401. {/* End of Table */}
  402. </div>
  403. </div>
  404. </React.Fragment>
  405. )}
  406. {/* End of Leave Request */}
  407.  
  408. {/* LeaveBalance */}
  409. {this.state.tabChosen === 1 && (
  410. <React.Fragment>
  411. <div className='row'>
  412. <div className='col-md-12'>
  413. <h5>Leave Balance</h5>
  414. </div>
  415. </div>
  416. </React.Fragment>
  417. )}
  418. {/* End of Leave Balance */}
  419. </div>
  420. {/* End of Body */}
  421. </div>
  422. </div>
  423. </div>
  424. {/* End of Row Box */}
  425. </div>
  426. {/* end of container-fluid-breadcrumb */}
  427. </MuiPickersUtilsProvider>
  428. </React.Fragment>
  429. );
  430. }
  431. }
  432.  
  433. export default connect(
  434. mapStateToProps,
  435. mapDispatchToProps
  436. )(LeaveView);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement