feat: add onGetData callback to TableList and enhance DetailSparepart with improved image handling

This commit is contained in:
2025-11-27 13:30:45 +07:00
parent 572042ab53
commit ed4570e8dd
4 changed files with 334 additions and 70 deletions

View File

@@ -29,7 +29,11 @@ const SparepartCardList = ({
const handleUpdateStock = async (item) => {
const quantityToAdd = updateQuantities[item.sparepart_id] || 0;
if (quantityToAdd === 0) {
NotifAlert({ icon: 'info', title: 'Info', message: 'Please change the quantity first.' });
NotifAlert({
icon: 'info',
title: 'Info',
message: 'Please change the quantity first.',
});
return;
}
@@ -39,32 +43,55 @@ const SparepartCardList = ({
return;
}
setLoadingQuantities(prev => ({ ...prev, [item.sparepart_id]: true }));
setLoadingQuantities((prev) => ({ ...prev, [item.sparepart_id]: true }));
const payload = {
...item,
sparepart_stok: newStock,
sparepart_stok: newStock.toString(), // Convert number to string as required by API
};
// Remove unnecessary keys if the API doesn't like them
delete payload.key;
delete payload.id;
// Hanya tambahkan field jika nilainya tidak kosong untuk menghindari validasi error
if (item.sparepart_unit && item.sparepart_unit.trim() !== '') {
payload.sparepart_unit = item.sparepart_unit;
}
if (item.sparepart_merk && item.sparepart_merk.trim() !== '') {
payload.sparepart_merk = item.sparepart_merk;
}
if (item.sparepart_model && item.sparepart_model.trim() !== '') {
payload.sparepart_model = item.sparepart_model;
}
if (item.sparepart_description && item.sparepart_description.trim() !== '') {
payload.sparepart_description = item.sparepart_description;
}
try {
const response = await updateSparepart(item.sparepart_id, payload);
if (response.statusCode === 200) {
NotifOk({ icon: 'success', title: 'Success', message: 'Stock updated successfully.' });
// Periksa apakah response valid sebelum mengakses propertinya
if (response && response.statusCode === 200) {
NotifOk({
icon: 'success',
title: 'Success',
message: 'Stock updated successfully.',
});
if (onStockUpdate) {
onStockUpdate();
}
handleQuantityChange(item.sparepart_id, 0); // Reset quantity
} else {
NotifAlert({ icon: 'error', title: 'Failed', message: response.message || 'Failed to update stock.' });
NotifAlert({
icon: 'error',
title: 'Failed',
message: response?.message || 'Failed to update stock.',
});
}
} catch (error) {
NotifAlert({ icon: 'error', title: 'Error', message: error.message || 'An error occurred.' });
NotifAlert({
icon: 'error',
title: 'Error',
message: error.message || 'An error occurred.',
});
} finally {
setLoadingQuantities(prev => ({ ...prev, [item.sparepart_id]: false }));
setLoadingQuantities((prev) => ({ ...prev, [item.sparepart_id]: false }));
}
};
@@ -80,7 +107,9 @@ const SparepartCardList = ({
style={{
borderRadius: '8px',
overflow: 'hidden',
border: `1px solid ${fieldColor ? item[fieldColor] : cardColor || '#E0E0E0'}`,
border: `1px solid ${
fieldColor ? item[fieldColor] : cardColor || '#E0E0E0'
}`,
}}
bodyStyle={{ padding: 0 }}
>
@@ -106,42 +135,79 @@ const SparepartCardList = ({
{item.sparepart_item_type}
</Tag>
)}
<div style={{
backgroundColor: '#f0f0f0',
width: '100%',
padding: '8px',
borderRadius: '4px',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
aspectRatio: '1 / 1',
}}>
<img
src={item.image_url || "https://via.placeholder.com/150"}
alt={item[header]}
style={{
maxWidth: '100%',
maxHeight: '100%',
objectFit: 'contain',
}}
/>
<div
style={{
backgroundColor: '#f0f0f0',
width: '100%',
padding: '8px',
borderRadius: '4px',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
aspectRatio: '1 / 1',
}}
>
{(() => {
// Debug: log the image path construction
let imgSrc;
if (item.sparepart_foto) {
if (item.sparepart_foto.startsWith('http')) {
imgSrc = item.sparepart_foto;
} else {
// Gunakan format file URL seperti di brandDevice
const fileName = item.sparepart_foto.split('/').pop();
// Gunakan API getFileUrl untuk mendapatkan URL yang benar
const token = localStorage.getItem('token');
const baseURL = import.meta.env.VITE_API_SERVER || '';
imgSrc = `${baseURL}/file-uploads/images/${encodeURIComponent(fileName)}${token ? `?token=${encodeURIComponent(token)}` : ''}`;
}
console.log('Image path being constructed:', imgSrc);
} else {
imgSrc = 'https://via.placeholder.com/150';
}
return (
<img
src={imgSrc}
alt={item[header]}
style={{
maxWidth: '100%',
maxHeight: '100%',
objectFit: 'contain',
}}
onError={(e) => {
console.error('Image failed to load:', imgSrc);
e.target.src = 'https://via.placeholder.com/150';
}}
onLoad={() => console.log('Image loaded successfully:', imgSrc)}
/>
);
})()}
</div>
</div>
</Col>
<Col span={16}>
<div style={{ padding: '16px', position: 'relative', height: '100%' }}>
<div
style={{
padding: '16px',
position: 'relative',
height: '100%',
}}
>
<div
style={{
position: 'absolute',
top: 8,
right: 8,
display: 'flex',
gap: '8px'
gap: '8px',
}}
>
{showEditModal && (
<Button
style={{ color: '#faad14', borderColor: '#faad14' }}
style={{
color: '#faad14',
borderColor: '#faad14',
}}
icon={<EditOutlined />}
key="edit"
onClick={() => showEditModal(item)}
@@ -158,7 +224,17 @@ const SparepartCardList = ({
/>
)}
</div>
<Title level={5} style={{ margin: 0, marginBottom: '8px', paddingRight: '60px', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>
<Title
level={5}
style={{
margin: 0,
marginBottom: '8px',
paddingRight: '60px',
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
}}
>
{item[header]}
</Title>
<Text type="secondary">
@@ -166,23 +242,49 @@ const SparepartCardList = ({
</Text>
<Divider style={{ margin: '8px 0' }} />
<Space align="center" style={{ marginBottom: '8px', display: 'flex', justifyContent: 'center' }}>
<Space
align="center"
style={{
marginBottom: '8px',
display: 'flex',
justifyContent: 'center',
}}
>
<Button
icon={<MinusOutlined />}
onClick={() => handleQuantityChange(item.sparepart_id, quantity - 1)}
onClick={() =>
handleQuantityChange(
item.sparepart_id,
quantity - 1
)
}
disabled={isLoading}
style={{ width: 28, height: 28 }}
/>
<Text strong style={{ padding: '0 8px', fontSize: '16px' }}>{quantity}</Text>
<Text
strong
style={{ padding: '0 8px', fontSize: '16px' }}
>
{quantity}
</Text>
<Button
icon={<PlusOutlined />}
onClick={() => handleQuantityChange(item.sparepart_id, quantity + 1)}
onClick={() =>
handleQuantityChange(
item.sparepart_id,
quantity + 1
)
}
disabled={isLoading}
style={{ width: 28, height: 28 }}
/>
<Text type="secondary">{item.sparepart_unit ? ` / ${item.sparepart_unit}` : ' / pcs'}</Text>
<Text type="secondary">
{item.sparepart_unit
? ` / ${item.sparepart_unit}`
: ' / pcs'}
</Text>
</Space>
<Button
type={quantity === 0 ? 'default' : 'primary'}
size="small"
@@ -192,10 +294,20 @@ const SparepartCardList = ({
>
Update Stock
</Button>
<br />
<Text type="secondary" style={{ fontSize: '12px', marginTop: '8px', display: 'inline-block' }}>
Last updated: {item.updated_at ? dayjs(item.updated_at).format('DD MMM YYYY') : 'N/A'}
<Text
type="secondary"
style={{
fontSize: '12px',
marginTop: '8px',
display: 'inline-block',
}}
>
Last updated:{' '}
{item.updated_at
? dayjs(item.updated_at).format('DD MMM YYYY')
: 'N/A'}
</Text>
</div>
</Col>
@@ -208,4 +320,4 @@ const SparepartCardList = ({
);
};
export default SparepartCardList;
export default SparepartCardList;