vandasche

Untitled

Jun 4th, 2020
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { connect } from "react-redux";
  3. import * as actUser from "../_actions/customer";
  4. import { Table, Button, Modal, Form } from "react-bootstrap";
  5. import ModalAdd from "./modals/addCustomer";
  6. import { RiAddCircleLine } from "react-icons/ri";
  7. import { API } from "../config/api";
  8.  
  9. class Cust extends Component {
  10. state = {
  11. addModal: false,
  12. editModal: false,
  13. nama: "",
  14. gender: "",
  15. phone: "",
  16. alamat: "",
  17. id:""
  18. };
  19.  
  20. componentDidMount() {
  21. this.props.dispatch(actUser.getUsers());
  22. }
  23.  
  24. handleChangeTxt = (e) => {
  25. this.setState({ [e.target.name]: e.target.value });
  26. };
  27.  
  28. editData = async (e) => {
  29. try {
  30. console.log(e.target.id);
  31. const newData = await API.patch(`/customer/${e.target.id}`, {
  32. nama: this.state.nama,
  33. gender: this.state.gender,
  34. phone: this.state.phone,
  35. alamat: this.state.alamat,
  36. });
  37.  
  38. console.log(newData);
  39. } catch (error) {
  40. console.log(error);
  41. }
  42. };
  43.  
  44. showAdd = () => {
  45. this.setState({ addModal: true });
  46. };
  47. hideAdd = () => {
  48. this.setState({ addModal: false });
  49. };
  50.  
  51. showEdit = (e) => {
  52. this.setState({id:e.target.id })
  53. this.setState({ editModal: true });
  54. };
  55. hideEdit = () => {
  56. this.setState({ editModal: false });
  57. };
  58.  
  59. render() {
  60. const { data: customer, loading, error } = this.props.customer;
  61.  
  62. if (loading) return <h1>Loading</h1>;
  63. if (error) return <h1>ERROR</h1>;
  64.  
  65. return (
  66. <div>
  67. <ModalAdd show={this.state.addModal} onHide={this.hideAdd} />
  68. <h1>DATA CUSTOMER</h1>
  69. <Button
  70. variant="secondary"
  71. style={{ marginBottom: 20, marginLeft: 20 }}
  72. onClick={this.showAdd}
  73. >
  74. <RiAddCircleLine /> Tambah Data Customer
  75. </Button>
  76. <Table striped bordered hover>
  77. <thead>
  78. <tr>
  79. <th>No</th>
  80. <th>Nama</th>
  81. <th>Gender</th>
  82. <th>No HP</th>
  83. <th>Alamat</th>
  84. <th>Action</th>
  85. </tr>
  86. </thead>
  87. <tbody>
  88. {customer.map((item, index) => (
  89. <tr key={index}>
  90. <th>{index + 1}</th>
  91. <td>{item.nama}</td>
  92. <td>{item.gender}</td>
  93. <td>{item.phone}</td>
  94. <td>{item.alamat}</td>
  95. <td>
  96. <button
  97. className="button"
  98. id={item.id}
  99. onClick={this.showEdit}
  100. >
  101. EDIT
  102. </button>{" "}
  103. <button style={{ marginLeft: 20 }} className="button">
  104. DELETE
  105. </button>
  106. </td>
  107. </tr>
  108. ))}
  109. </tbody>
  110. </Table>
  111.  
  112. <Modal
  113. show={this.state.editModal}
  114. onHide={this.hideEdit}
  115. size="lg"
  116. aria-labelledby="contained-modal-title-vcenter"
  117. centered
  118. >
  119. <Modal.Header closeButton>
  120. <Modal.Title>Tambah Customer</Modal.Title>
  121. </Modal.Header>
  122. <Modal.Body>
  123. <Form>
  124. <Form.Group>
  125. <Form.Label className="bold">Nama</Form.Label>
  126. <Form.Control
  127. name="nama"
  128. type="text"
  129. placeholder="Masukan Nama Customer"
  130. onChange={this.handleChangeTxt}
  131. value={this.state.nama}
  132. />
  133. </Form.Group>
  134. <Form.Group>
  135. <Form.Label className="bold">Gender</Form.Label>
  136. <select
  137. onChange={this.handleChangeTxt}
  138. defaultValue={"select"}
  139. name="gender"
  140. id="gender"
  141. className="form-control"
  142. >
  143. <option value="select" disabled>
  144. Pilih Jenis Kelamin
  145. </option>
  146. <option value="Laki-laki">Laki-laki</option>
  147. <option value="Perempuan">Perempuan</option>
  148. </select>
  149. </Form.Group>
  150. <Form.Group>
  151. <Form.Label className="bold">No Telepon</Form.Label>
  152. <Form.Control
  153. name="phone"
  154. type="text"
  155. placeholder="Masukan No Telepon"
  156. onChange={this.handleChangeTxt}
  157. value={this.state.phone}
  158. />
  159. </Form.Group>
  160. <Form.Group>
  161. <Form.Label className="bold">Alamat</Form.Label>
  162. <Form.Control
  163. name="alamat"
  164. type="text"
  165. placeholder="Masukan Alamat Customer"
  166. onChange={this.handleChangeTxt}
  167. value={this.state.alamat}
  168. />
  169. </Form.Group>
  170. </Form>
  171. </Modal.Body>
  172. <Modal.Footer>
  173. <Button variant="secondary" onClick={this.props.onHide}>
  174. Close
  175. </Button>
  176. <Button variant="primary" onClick={this.editData}>
  177. Edit Data
  178. </Button>
  179. </Modal.Footer>
  180. </Modal>
  181. </div>
  182. );
  183. }
  184. }
  185.  
  186. const mapStateToProps = (state) => {
  187. return {
  188. customer: state.customer,
  189. };
  190. };
  191.  
  192. export default connect(mapStateToProps)(Cust);
Add Comment
Please, Sign In to add comment