import Head from "next/head";
import { useState } from "react";
import { uid } from "uid";
export const getStaticPaths = async () => {
const res = await fetch('http://localhost:3004/rencanaBelanja');
const data = await res.json();
const paths = data.map(x => {
return {
params : { id : x.id.toString() }
}
})
return {
paths,
fallback : false
}
}
export const getStaticProps = async (context) => {
const id = context.params.id;
const res = await fetch('http://localhost:3004/rencanaBelanja/' + id);
const data = await res.json();
return {
props : { barang : data }
}
}
const Details = ({ barang }) => {
const [hide,sethide] = useState("");
const [show,setShow] = useState("none");
const [items,setItems] = useState(barang.detail);
const [isUpdate,setIsUpdate] = useState({id:null, status:false});
const [formData, setFormData] = useState({
namaProduk:"",
tanggal:"",
lokasi:"",
stokBTUD:"",
satuanStok:"Kg",
rencanaPembelian:"",
satuanRencana: "Kg",
kategori: ""
})
const IdAbalAbal = (barang) => {
const Id = barang.map(x => (
x.id
))
const lengthId = Id.length ;
const lastId = Id[lengthId -1]
return lastId;
}
function generateId (barang){
let id = ''
if(barang.length > 0) {
id = IdAbalAbal(barang);
} else {
id = '0tr'
}
const numberId = id.split("");
const newNumber = parseInt(numberId[0]) + 1;
const idType = numberId[1] + numberId[2];
const newId = newNumber + idType;
return newId ;
}
const idBaru = generateId(barang);
function handleHide(){
if(hide === ""){
sethide("none")
}
setShow("")
}
function handleChange(e){
let data = {...formData};
data[e.target.name] = e.target.value;
setFormData(data)
}
function handleDelete(id){
let data = [...items];
let filteredData = data.filter((contact)=> contact.id !== id);
setItems(filteredData);
}
function handleEdit(id){
let data = [...items];
let foundData = data.find((contact)=> contact.id === id);
setFormData({
namaProduk:foundData.namaProduk,
tanggal:foundData.tanggal,
lokasi:foundData.lokasi,
stokBTUD:foundData.stokBTUD,
satuanStok:foundData.satuanStok,
rencanaPembelian:foundData.rencanaPembelian,
satuanRencana:foundData.satuanRencana,
kategori:foundData.kategori
})
setIsUpdate({id:id,status:true})
}
function handleSubmit(e){
e.preventDefault();
// menambahkan data
let data = [...items]
let idBaru = uid();
if(isUpdate.status) {
data.forEach((item)=>{
if(item.id === isUpdate.id){
item.namaProduk = formData.namaProduk;
item.tanggal = formData.tanggal;
item.lokasi = formData.lokasi;
item.stokBTUD = formData.stokBTUD;
item.satuanStok = formData.satuanStok;
item.rencanaPembelian = formData.rencanaPembelian;
item.satuanRencana = formData.satuanRencana;
item.kategori = formData.kategori
}
})
} else {
data.push({
id: idBaru,
namaProduk : formData.namaProduk,
tanggal : formData.tanggal,
lokasi : formData.lokasi,
stokBTUD : formData.stokBTUD,
satuanStok : formData.satuanStok,
rencanaPembelian : formData.rencanaPembelian,
satuanRencana : formData.satuanRencana,
kategori : formData.kategori
})
}
setItems(data);
setFormData({
namaProduk:"",
tanggal:"",
lokasi:"",
stokBTUD:"",
satuanStok:"Kg",
rencanaPembelian:"",
satuanRencana: "Kg",
kategori: ""
});
setIsUpdate({id:null, status:false})
}
const sendData = () => {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
id : idBaru,
detail : items
})
};
fetch('http://localhost:3004/rencanaBelanja', requestOptions)
.then(response => response.json())
}
return (
<div>
<Head>
<title>Detail Rencana Belanja</title>
</Head>
<div className={"mx-5 mt-5 " + hide}>
<button className="button green is-size-7-mobile"><i className="fas fa-arrow-left pr-2 green"></i>Kembali</button>
</div>
<div className="mx-5 mt-5 green title is-size-4-mobile">
{"Rencana Belanja " + barang.id}
</div>
<div className={show}>
<form onSubmit={handleSubmit} autoComplete="off" className="is-flex is-flex-direction-column form my-6 ">
<div className="masukan mb-2 px-5">
<label htmlFor="inputNama" className="label">Nama Barang</label>
<div className="control">
<input name="namaProduk" onChange={handleChange} value={formData.namaProduk} type="text" className="input"/>
</div>
</div>
<div className="masukan mb-2 px-5">
<label htmlFor="inputNama" className="label">Tanggal</label>
<div className="control">
<input name="tanggal" type="date" className="input" onChange={handleChange} value={formData.tanggal}/>
</div>
</div>
<div className="masukan mb-2 px-5">
<label htmlFor="inputNama" className="label">Lokasi</label>
<div className="control">
<input name="lokasi" type="text" className="input" onChange={handleChange} value={formData.lokasi}/>
</div>
</div>
<div className="masukan mb-2 px-5">
<label htmlFor="inputNama" className="label">Stok BTUD</label>
<div className="control">
<input name="stokBTUD" type="number" className="input" onChange={handleChange} value={formData.stokBTUD}/>
</div>
</div>
<div className="masukan mb-2 px-5">
<label htmlFor="inputNama" className="label">Satuan Stok BTUD</label>
<div className="select">
<select className="s" name="satuanStok" onChange={handleChange} value={formData.satuanStok}>
<option>Kg</option>
<option>Ons</option>
<option>Ikat</option>
<option>Liter</option>
<option>Buah</option>
</select>
</div>
</div>
<div className="masukan mb-2 px-5">
<label htmlFor="inputNama" className="label">Rencana Pembelian</label>
<div className="control">
<input name="rencanaPembelian" type="number" className="input" onChange={handleChange} value={formData.rencanaPembelian}/>
</div>
</div>
<div className="masukan mb-2 px-5">
<label htmlFor="inputNama" className="label">Satuan Rencana Pembelian</label>
<div className="select">
<select className="s" name="satuanRencana" onChange={handleChange} value={formData.satuanRencana}>
<option>Kg</option>
<option>Ons</option>
<option>Ikat</option>
<option>Liter</option>
<option>Buah</option>
</select>
</div>
</div>
<div className="masukan mb-2 px-5">
<label htmlFor="inputNama" className="label">Kategori</label>
<div className="select">
<select className="s" name="kategori" onChange={handleChange} value={formData.kategori}>
<option>Ikan</option>
<option>Buah</option>
<option>Daging</option>
<option>Bumbu Dapur</option>
<option>Sayur</option>
</select>
</div>
</div>
<div className="masukan my-2 px-5">
<div className="control">
<button className="button bg-green white is-rounded" type="submit">Tambah</button>
</div>
</div>
</form>
</div>
<div className="hscroll">
<table className="table">
<thead>
<tr>
<td className="white">No</td>
<td className="white">Nama Produk</td>
<td className="white">Stok BTUD</td>
<td className="white">Rencana Pembelian</td>
<td className="white">Aksi</td>
</tr>
</thead>
<tbody>
{
items.map((item, index) =>
<tr key={item.id}>
<td>{index +1}</td>
<td>{item.namaProduk}</td>
<td>{item.stokBTUD +" "+ item.satuanStok}</td>
<td>{item.rencanaPembelian +" "+ item.satuanRencana}</td>
<td className="is-flex">
<button onClick={()=>handleEdit(item.id)} className="button is-rounded bg-green white is-small">Ubah</button>
<button onClick={()=>handleDelete(item.id)} className="button is-rounded bg-red white is-small">Hapus</button>
</td>
</tr>
)
}
</tbody>
</table>
</div>
<button onClick={()=>handleHide()} className={"button bg-red white is-rounded my-5 mx-4 " + hide} type="submit">
Batalkan
</button>
<button onClick={()=>sendData(barang.id)} className={"button bg-green white is-rounded my-5 mx-4 " + show} type="submit">
Simpan
</button>
</div>
)
}
export default Details;
Komentar
Posting Komentar