Fakultas Ilmu Komputer UI

DetailSubkategori.jsx 4.07 KB
Newer Older
1
import React, { useState } from "react";
Jeffrey's avatar
Jeffrey committed
2
3
import useFetchSingleData from "../../utils/useFetchSingleData";
import { css } from "@emotion/core";
4
5
import { ButtonDeleteStyled, ErrorDiv } from "../../component/html/html";
import TableComponent from "../../component/TableComponent";
Jeffrey's avatar
Jeffrey committed
6
7
import LinkYellow from "../../component/LinkYellow";
import { Link, navigate } from "@reach/router";
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
8
import ArrowBackIcon from "@material-ui/icons/ArrowBack";
Jeffrey's avatar
Jeffrey committed
9
import useDelete from "../../utils/useDelete";
10
11
12
13
14
15
16
17
import {
  Dialog,
  DialogActions,
  DialogContent,
  DialogContentText,
  DialogTitle,
  Button,
} from "@material-ui/core";
Jeffrey's avatar
Jeffrey committed
18
19
20
21
22

const DetailSubkategori = ({ idSubKategori }) => {
  const url = `${process.env.REACT_APP_BASE_URL}/subcategories/${idSubKategori}/`;
  const [subcategory, error] = useFetchSingleData(url);
  const [deleteProduct, errorDelete] = useDelete(url);
23
  const [openModal, setOpenModal] = useState(false);
Jeffrey's avatar
Jeffrey committed
24
25
26
27
28
29
30
31
32
33
34
  const data = {
    url: `${process.env.REACT_APP_BASE_URL}/products/`,
    pageDefault: 1,
    searchDefault: "",
    title: "",
    keyValuePairs: [
      ["id", "id"],
      ["name", "Nama"],
      ["price", "Harga"],
      ["stock", "Stok"],
    ],
35
    argument: `subcategory=${idSubKategori}`,
Jeffrey's avatar
Jeffrey committed
36
  };
37
  const handleClose = () => setOpenModal(false);
Jeffrey's avatar
Jeffrey committed
38
39
40
41
42
43
44
45
46
  return (
    <div
      data-testid="page-detail-subkategori"
      css={css`
        display: flex;
        margin: 2rem 3rem 3rem 3rem;
        flex-direction: column;
      `}
    >
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
      <Dialog open={openModal} onClose={handleClose}>
        <DialogTitle>{`Hapus subkategori ${subcategory.name}`}</DialogTitle>
        <DialogContent>
          <DialogContentText>
            Apakah yakin untuk menghapus subkategori {subcategory.name}?
          </DialogContentText>
          <DialogActions>
            <Button onClick={handleClose} color="primary">
              TIDAK
            </Button>
            <Button
              data-testid="button-delete-subcategory"
              color="secondary"
              onClick={() => {
                deleteProduct();
                handleClose();
              }}
            >
              HAPUS
            </Button>
          </DialogActions>
        </DialogContent>
      </Dialog>
Jeffrey's avatar
Jeffrey committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
      {error && <ErrorDiv>Error !, Please relogin..</ErrorDiv>}
      {errorDelete && (
        <ErrorDiv>
          Tidak dapat menghapus subkategori, mohon periksa apakah ada produk
          didalam kategori ini.
        </ErrorDiv>
      )}
      <div
        css={css`
          display: flex;
          flex-direction: column;
        `}
      >
        <button
          css={css`
            align-self: start;
            background-color: Transparent;
            background-repeat: no-repeat;
            border: none;
            cursor: pointer;
            overflow: hidden;
            outline: none;
          `}
          onClick={() => navigate("/subkategori")}
        >
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
95
          <ArrowBackIcon fontSize="large" />
Jeffrey's avatar
Jeffrey committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
        </button>
        <div
          css={css`
            font-size: 2rem;
            display: flex;
            flex-direction: row;
            align-items: baseline;
          `}
        >
          <div
            css={css`
              flex-grow: 2;
            `}
          >
            Subkategori: {subcategory.name}
          </div>
          <div
            css={css`
              flex-grow: 1;
            `}
          >
            <LinkYellow to="ubah">EDIT</LinkYellow>
          </div>
          <div
            css={css`
              flex-grow: 1;
            `}
          >
124
125
126
            <ButtonDeleteStyled
              data-testid="button-delete-subcategory-modal"
              onClick={() => setOpenModal(true)}
Jeffrey's avatar
Jeffrey committed
127
128
            >
              HAPUS
129
            </ButtonDeleteStyled>
Jeffrey's avatar
Jeffrey committed
130
131
132
133
134
135
136
          </div>
        </div>
        <div
          css={css`
            font-size: 1.5rem;
          `}
        >
137
          Kategori:
Jeffrey's avatar
Jeffrey committed
138
139
140
141
142
143
144
          <span>
            <Link to={`/kategori/${subcategory.category}`}>
              {subcategory.category_name}
            </Link>
          </span>
        </div>
      </div>
145
      <TableComponent {...data} />
Jeffrey's avatar
Jeffrey committed
146
147
148
149
150
    </div>
  );
};

export default DetailSubkategori;