Fakultas Ilmu Komputer UI

FormStatus.jsx 2.14 KB
Newer Older
Jeffrey's avatar
Jeffrey committed
1
2
3
4
5
6
7
8
9
10
11
import React from "react";
import { useForm } from "react-hook-form";
import useSendData from "../../utils/useSendData";
import {
  ErrorDiv,
  InputSelectForm,
  LabelInput,
  RowInput,
} from "../../component/html/html";
import { css } from "@emotion/core";
import Button from "@material-ui/core/Button";
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
12
import SaveIcon from "@material-ui/icons/Save";
Jeffrey's avatar
Jeffrey committed
13
14

const FormStatus = ({ idTransaksi, defaultStatus, paymentMethod }) => {
15
  const url = `${process.env.REACT_APP_BASE_URL}/transactions/${idTransaksi}/`;
Jeffrey's avatar
Jeffrey committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  const { register, handleSubmit } = useForm({
    defaultValues: {
      transaction_status: defaultStatus,
    },
  });
  const [send, errorSend] = useSendData({
    url,
    header: { "Content-Type": "application/json" },
    method: "PUT",
    redirect: `/transaksi/`,
  });

  const onSubmit = (data) => {
    send(JSON.stringify({ ...data }));
  };
  return (
    <div data-testid="form-status">
      {errorSend && <ErrorDiv>Status transaksi tidak dapat disimpan</ErrorDiv>}
      <form
        onSubmit={handleSubmit(onSubmit)}
        css={css`
          display: flex;
        `}
      >
        <RowInput>
          <LabelInput
            htmlFor="status"
            css={css`
              margin-right: 1rem;
            `}
          />
          <InputSelectForm
            data-testid="dropdown-status"
            id="status"
            ref={register({ required: true })}
            name="transaction_status"
          >
            {paymentMethod === "TRF" && (
              <option value="001">Waiting for proof of payment</option>
            )}
            <option value="002">Waiting for seller confirmation</option>
            <option value="003">In process</option>
            <option value="004">Being shipped</option>
            <option value="005">Completed</option>
            <option value="006">Canceled</option>
          </InputSelectForm>
        </RowInput>
        <Button
          data-testid="button-submit-status"
          type="submit"
          variant="contained"
          color="primary"
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
68
          startIcon={<SaveIcon />}
Jeffrey's avatar
Jeffrey committed
69
70
71
72
73
74
75
76
77
        >
          Simpan
        </Button>
      </form>
    </div>
  );
};

export default FormStatus;