vandasche

Untitled

Jun 5th, 2020
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { connect } from "react-redux";
  3. import * as actStock from "../_actions/storage";
  4. import * as actCategory from "../_actions/category";
  5. import * as actUser from "../_actions/customer";
  6. import NumberFormat from "react-number-format";
  7. import { Table, Button, Modal, Form, Alert } from "react-bootstrap";
  8. import { RiAddCircleLine } from "react-icons/ri";
  9. import { API } from "../config/api";
  10. import { Redirect } from "react-router-dom";
  11.  
  12. class Order extends Component {
  13. state = {
  14. modalAddItem: false,
  15. customerId: "",
  16. quantity: "",
  17. id: "",
  18. products: [],
  19. success: false,
  20. };
  21.  
  22. componentDidMount() {
  23. this.props.dispatch(actStock.getStocks());
  24. this.props.dispatch(actCategory.getCategories());
  25. this.props.dispatch(actUser.getUsers());
  26. }
  27.  
  28. handleChangeTxt = (e) => {
  29. this.setState({ [e.target.name]: e.target.value });
  30. };
  31.  
  32. showAdd = () => {
  33. this.setState({ modalAddItem: true });
  34. };
  35.  
  36. hideAdd = () => {
  37. this.setState({ modalAddItem: false });
  38. };
  39.  
  40. addItem = async () => {
  41. const getData = await API.get(`/product/${this.state.id}`);
  42. if (this.state.quantity > getData.data.quantity) {
  43. alert(
  44. `Jumlah barang melebihi stock, stock barang tersedia adalah ${getData.data.quantity}`
  45. );
  46. this.setState({ quantity: "" });
  47. }
  48. const data = {
  49. id: this.state.id,
  50. quantity: this.state.quantity,
  51. };
  52. if (!data) {
  53. console.log("failed to make data");
  54. }
  55. this.state.products.push(data);
  56.  
  57. this.setState({ modalAddItem: false });
  58. };
  59.  
  60. handleSubmit = async () => {
  61. const data = {
  62. customerId: this.state.customerId,
  63. products: this.state.products,
  64. };
  65. console.log(data);
  66. const sendData = await API.post(`/transaction`, {
  67. customerId: this.state.customerId,
  68. products: this.state.products,
  69. });
  70. if (sendData) {
  71. this.setState({ success: true });
  72. }
  73. };
  74.  
  75. render() {
  76. const { data: stocks, loading, error } = this.props.stock;
  77. const { data: categories } = this.props.category;
  78. const { data: customer } = this.props.customer;
  79. const produk = this.state.products;
  80. if (this.state.success) return <Redirect to="/transaction" />;
  81.  
  82. return (
  83. <div>
  84. <h4>Cart</h4>
  85. <Button
  86. variant="primary"
  87. style={{ marginBottom: 20, marginLeft: 20 }}
  88. onClick={this.showAdd}
  89. >
  90. <RiAddCircleLine /> Tambah Item
  91. </Button>
  92. <Form>
  93. <Form.Group>
  94. <Form.Label className="bold">Customer</Form.Label>
  95. <select
  96. onChange={this.handleChangeTxt}
  97. defaultValue={"select"}
  98. name="customerId"
  99. id="customerId"
  100. className="form-control"
  101. >
  102. <option value="select" disabled>
  103. Pilih Customer
  104. </option>
  105. {customer &&
  106. customer.length > 0 &&
  107. customer.map((item) => (
  108. <option value={item.id}>{item.nama}</option>
  109. ))}
  110. </select>
  111. </Form.Group>
  112. <Form.Group>
  113. <Form.Label className="bold">Barang</Form.Label>
  114. <Table striped bordered hover>
  115. <thead>
  116. <tr>
  117. <th>No</th>
  118. <th>Kode Barang</th>
  119. <th>Jumlah</th>
  120. </tr>
  121. </thead>
  122. <tbody>
  123. {produk &&
  124. produk.length > 0 &&
  125. produk.map((item, index) => (
  126. <tr key={index}>
  127. <th>{index + 1}</th>
  128. <th>{item.id}</th>
  129. <th>{item.quantity}</th>
  130. </tr>
  131. ))}
  132. </tbody>
  133. </Table>
  134. </Form.Group>
  135. <Button variant="success" onClick={this.handleSubmit}>
  136. Buy
  137. </Button>
  138. </Form>
  139. {/* Modal ADD ITEM */}
  140. <Modal
  141. show={this.state.modalAddItem}
  142. onHide={this.hideAdd}
  143. size="xs"
  144. aria-labelledby="contained-modal-title-vcenter"
  145. centered
  146. >
  147. <Form>
  148. <Form.Group>
  149. <Form.Label className="bold">Tambah Barang</Form.Label>
  150. <select
  151. onChange={this.handleChangeTxt}
  152. defaultValue={"select"}
  153. name="id"
  154. id="id"
  155. className="form-control"
  156. >
  157. <option value="select" disabled>
  158. Pilih Item
  159. </option>
  160. {stocks &&
  161. stocks.length > 0 &&
  162. stocks.map((item) => (
  163. <option value={item.id}>{item.namaProduk}</option>
  164. ))}
  165. </select>
  166. </Form.Group>
  167. <Form.Group>
  168. <Form.Label className="bold">Jumlah</Form.Label>
  169. <Form.Control
  170. name="quantity"
  171. type="text"
  172. placeholder="Masukan Jumlah Barang"
  173. onChange={this.handleChangeTxt}
  174. value={this.state.quantity}
  175. />
  176. </Form.Group>
  177. </Form>
  178. <Modal.Footer>
  179. <Button variant="secondary" onClick={this.hideAdd}>
  180. NO
  181. </Button>
  182. <Button variant="primary" onClick={this.addItem}>
  183. YES
  184. </Button>
  185. </Modal.Footer>
  186. </Modal>
  187. </div>
  188. );
  189. }
  190. }
  191.  
  192. const mapStateToProps = (state) => {
  193. return {
  194. stock: state.stock,
  195. category: state.category,
  196. customer: state.customer,
  197. };
  198. };
  199.  
  200. export default connect(mapStateToProps)(Order);
Add Comment
Please, Sign In to add comment