Fakultas Ilmu Komputer UI

Commit 013c876b authored by FadhilP's avatar FadhilP
Browse files

[GREEN] Implement invalid date warning

parent b601cb8c
Pipeline #78105 passed with stages
in 3 minutes and 43 seconds
import React from "react"; import React, { useState } from "react";
import { css } from "@emotion/core"; import { css } from "@emotion/core";
import FormBatch from "./FormBatch"; import FormBatch from "./FormBatch";
import ArrowBackIcon from "@material-ui/icons/ArrowBack"; import ArrowBackIcon from "@material-ui/icons/ArrowBack";
...@@ -7,19 +7,25 @@ import useFetchSingleData from "../../utils/useFetchSingleData"; ...@@ -7,19 +7,25 @@ import useFetchSingleData from "../../utils/useFetchSingleData";
import useSendData from "../../utils/useSendData"; import useSendData from "../../utils/useSendData";
import { navigate } from "@reach/router"; import { navigate } from "@reach/router";
import {ErrorDiv } from "../../component/html/html"; import {ErrorDiv } from "../../component/html/html";
import moment from "moment";
const EditBatch = ({ batchId }) => { const EditBatch = ({ batchId }) => {
const url = `${process.env.REACT_APP_BASE_URL}/batch/${batchId}/`; const url = `${process.env.REACT_APP_BASE_URL}/batch/${batchId}/`;
// const url = `http://localhost:8000/batch/${batchId}/`; // for local testing purposes // const url = `http://localhost:8000/batch/${batchId}/`; // for local testing purposes
const [initialData, errorState] = useFetchSingleData(url); const [initialData, errorState] = useFetchSingleData(url);
const [send, error] = useSendData({ url, method: "PATCH", redirect: -1 }); const [send, error] = useSendData({ url, method: "PATCH", redirect: -1 });
const [errorMessage, setErrorMessage] = useState("")
const onSubmit = (data) => { const onSubmit = (data) => {
if (moment(data.start_date) > moment(data.end_date)) {
setErrorMessage("Tanggal mulai tidak boleh melebihi tanggal akhir!")
} else {
const formData = new FormData(); const formData = new FormData();
formData.append("batch_name", data["batch_name"]); formData.append("batch_name", data["batch_name"]);
formData.append("start_date", data["start_date"]); formData.append("start_date", data["start_date"]);
formData.append("end_date", data["end_date"]); formData.append("end_date", data["end_date"]);
formData.append("shipping_cost", data["shipping_cost"]) formData.append("shipping_cost", data["shipping_cost"]);
send(formData); send(formData);
}
}; };
if (errorState || Object.keys(initialData).length === 0) if (errorState || Object.keys(initialData).length === 0)
return ( return (
...@@ -90,7 +96,7 @@ const EditBatch = ({ batchId }) => { ...@@ -90,7 +96,7 @@ const EditBatch = ({ batchId }) => {
Informasi Batch Informasi Batch
</div> </div>
</div> </div>
<FormBatch {...{ onSubmit, initialData, error }} /> <FormBatch {...{ onSubmit, initialData, error, errorMessage }} />
</div> </div>
); );
}; };
......
import React from "react"; import React, { useState } from "react";
import { css } from "@emotion/core"; import { css } from "@emotion/core";
import FormBatch from "./FormBatch"; import FormBatch from "./FormBatch";
import useSendData from "../../utils/useSendData"; import useSendData from "../../utils/useSendData";
import { Link } from "@reach/router"; import { Link } from "@reach/router";
import ArrowBackIcon from "@material-ui/icons/ArrowBack"; import ArrowBackIcon from "@material-ui/icons/ArrowBack";
import moment from "moment";
const TambahBatch = () => { const TambahBatch = () => {
const url = `${process.env.REACT_APP_BASE_URL}/batch/`; const url = `${process.env.REACT_APP_BASE_URL}/batch/`;
...@@ -12,14 +13,18 @@ const TambahBatch = () => { ...@@ -12,14 +13,18 @@ const TambahBatch = () => {
method: "POST", method: "POST",
redirect: "/batch", redirect: "/batch",
}); });
const [errorMessage, setErrorMessage] = useState("")
const onSubmit = (data) => { const onSubmit = (data) => {
const formData = new FormData(); if (moment(data.start_date) > moment(data.end_date)) {
formData.append("batch_name", data["batch_name"]); setErrorMessage("Tanggal mulai tidak boleh melebihi tanggal akhir!")
formData.append("start_date", data["start_date"]); } else {
formData.append("end_date", data["end_date"]); const formData = new FormData();
formData.append("shipping_cost", data["shipping_cost"]); formData.append("batch_name", data["batch_name"]);
formData.append("start_date", data["start_date"]);
send(formData); formData.append("end_date", data["end_date"]);
formData.append("shipping_cost", data["shipping_cost"]);
send(formData);
}
}; };
return ( return (
<div <div
...@@ -57,7 +62,7 @@ const TambahBatch = () => { ...@@ -57,7 +62,7 @@ const TambahBatch = () => {
> >
Informasi Batch Informasi Batch
</div> </div>
<FormBatch {...{ onSubmit, error }} /> <FormBatch {...{ onSubmit, error, errorMessage }} />
</div> </div>
); );
}; };
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment