Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
15c0fdce82 | ||
|
|
467fbb89c9 | ||
|
|
ceeef16de9 | ||
|
|
74250261f2 | ||
|
|
1d7370e557 | ||
|
|
77110e1e9a |
Vendored
+4425
-4205
File diff suppressed because it is too large
Load Diff
+21
-8
@@ -5,7 +5,7 @@ import { useState } from "react";
|
||||
|
||||
const Testing = () => {
|
||||
const token = API_TOKEN;
|
||||
const [selectedVehicle, setSelectedVehicle] = useState(null);
|
||||
const [selectedVehicle, setSelectedVehicle] = useState([]);
|
||||
|
||||
return (
|
||||
<div style={{ padding: 8, marginTop: 12 }}>
|
||||
@@ -14,29 +14,42 @@ const Testing = () => {
|
||||
Authorization: `Bearer ${token}`,
|
||||
}}
|
||||
>
|
||||
{/* <ListVirtual
|
||||
<ListVirtual
|
||||
id="company-select"
|
||||
label="Company"
|
||||
name="name"
|
||||
url="/company"
|
||||
defaultValue={"442"}
|
||||
defaultValue={[442,722]}
|
||||
size="small"
|
||||
multiple
|
||||
/> */}
|
||||
/>
|
||||
<ListVirtual
|
||||
label="License Plate"
|
||||
name="license_plate"
|
||||
url="/vehicle"
|
||||
defaultValue={selectedVehicle}
|
||||
getData={(data) => {
|
||||
if (data) {
|
||||
setSelectedVehicle(data.id);
|
||||
}
|
||||
const ids = data.map(item => item.id);
|
||||
setSelectedVehicle(ids);
|
||||
}}
|
||||
size="small"
|
||||
filter={[
|
||||
{name:"company_id",value: "740","logic_operator":"=","operator":"AND"}
|
||||
{name:"company_id",value: "442,722","logic_operator":"IN","operator":"AND"}
|
||||
]}
|
||||
joins={[
|
||||
{
|
||||
table: "vehicle_type",
|
||||
columns: ["min_speed", "max_speed"],
|
||||
},
|
||||
]}
|
||||
warningCheck={(row) =>
|
||||
row.vehicle_type_height_dimension <= 0 ||
|
||||
row.vehicle_type_length_dimension <= 0 ||
|
||||
row.vehicle_type_width_dimension <= 0
|
||||
? "No compatible capacity"
|
||||
: null
|
||||
}
|
||||
multiple
|
||||
/>
|
||||
</ApiProvider>
|
||||
</div>
|
||||
|
||||
+208
-43
@@ -8,11 +8,13 @@ import {
|
||||
Button,
|
||||
List,
|
||||
ListItemButton,
|
||||
ListItemIcon,
|
||||
ListItemText,
|
||||
Typography,
|
||||
InputAdornment,
|
||||
IconButton,
|
||||
Checkbox
|
||||
Checkbox,
|
||||
CircularProgress,
|
||||
} from "@mui/material";
|
||||
import ClearIcon from "@mui/icons-material/Clear";
|
||||
import FilterTableBuilder from "../utils/filterTableBuilder";
|
||||
@@ -36,7 +38,9 @@ export default function ListVirtual({
|
||||
filter,
|
||||
isApiGo = false,
|
||||
multiple,
|
||||
customSxTextField = {}
|
||||
customSxTextField = {},
|
||||
joins = [],
|
||||
warningCheck,
|
||||
}) {
|
||||
const headersRequest = useApiHeaders();
|
||||
const [open, setOpen] = useState(false);
|
||||
@@ -44,6 +48,7 @@ export default function ListVirtual({
|
||||
const [rows, setRows] = useState([]);
|
||||
const [selected, setSelected] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [selectAllLoading, setSelectAllLoading] = useState(false);
|
||||
const [offset, setOffset] = useState(0);
|
||||
const [totalRecord, setTotalRecord] = useState(0);
|
||||
|
||||
@@ -72,6 +77,14 @@ export default function ListVirtual({
|
||||
filter.forEach(f => payload.where(f.logic_operator || "=", f.name, f.value, f.operator || "AND", f.value1 || null, f.table_name || null));
|
||||
}
|
||||
|
||||
if (Array.isArray(filter) && filter.some(f => f.name === "company_id")) {
|
||||
payload.addJoin("company", ["name"]);
|
||||
}
|
||||
|
||||
joins.forEach(join => {
|
||||
payload.addJoin(join.table, join.columns);
|
||||
});
|
||||
|
||||
try {
|
||||
const res = await apiRequest.search(
|
||||
url,
|
||||
@@ -121,51 +134,63 @@ export default function ListVirtual({
|
||||
}
|
||||
}, [open]);
|
||||
|
||||
const fetchById = async (id) => {
|
||||
const fetchDefaultValue = async (value) => {
|
||||
setLoading(true);
|
||||
|
||||
const payload = new FilterTableBuilder()
|
||||
.setPaging(0, 1)
|
||||
.equal("id", id);
|
||||
const payload = new FilterTableBuilder().setPaging(0, lengthData);
|
||||
|
||||
if (multiple) {
|
||||
payload.in("id", value); // value = [1,2,3]
|
||||
} else {
|
||||
payload.equal("id", value); // value = 1
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await apiRequest.search(
|
||||
url,
|
||||
payload.build(),
|
||||
url,
|
||||
payload.build(),
|
||||
{
|
||||
headers: headersRequest
|
||||
headers: headersRequest,
|
||||
},
|
||||
isApiGo,
|
||||
);
|
||||
);
|
||||
|
||||
const data = res.data?.[0];
|
||||
const data = res.data || [];
|
||||
|
||||
if (data) {
|
||||
setSelected(data);
|
||||
}
|
||||
setSelected(multiple ? data : data[0] || null);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
setLoading(false);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!defaultValue) return;
|
||||
if (defaultValue == null) return;
|
||||
|
||||
if (multiple) {
|
||||
if (Array.isArray(defaultValue)) {
|
||||
// kosong, jangan request
|
||||
if (Array.isArray(defaultValue) && defaultValue.length === 0) {
|
||||
setSelected([]);
|
||||
return;
|
||||
}
|
||||
|
||||
// object
|
||||
if (
|
||||
Array.isArray(defaultValue) &&
|
||||
typeof defaultValue[0] === "object"
|
||||
) {
|
||||
setSelected(defaultValue);
|
||||
} else {
|
||||
setSelected([defaultValue]);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (Array.isArray(defaultValue)) {
|
||||
setSelected(defaultValue[0] || null);
|
||||
} else {
|
||||
if (typeof defaultValue === "object" && !Array.isArray(defaultValue)) {
|
||||
setSelected(defaultValue);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fetchDefaultValue(defaultValue);
|
||||
}, [defaultValue, multiple]);
|
||||
|
||||
const handleSelect = (row) => {
|
||||
@@ -200,11 +225,21 @@ export default function ListVirtual({
|
||||
getData?.(multiple ? [] : null);
|
||||
};
|
||||
|
||||
// NEW: helper untuk memanggil warningCheck dengan aman
|
||||
const getRowWarning = useCallback(
|
||||
(row) => {
|
||||
if (typeof warningCheck !== "function") return null;
|
||||
return warningCheck(row) || null;
|
||||
},
|
||||
[warningCheck]
|
||||
);
|
||||
|
||||
const displayValue = multiple
|
||||
? (selected || [])
|
||||
.map((x) => x[name]?.trim())
|
||||
.filter(Boolean)
|
||||
.join(", ")
|
||||
? (selected?.length > 1
|
||||
? `${selected.length} ${label} selected`
|
||||
: selected?.length === 1
|
||||
? selected[0][name]
|
||||
: "")
|
||||
: selected?.[name] || "";
|
||||
|
||||
const isSelected = (row) => {
|
||||
@@ -215,6 +250,78 @@ export default function ListVirtual({
|
||||
return (selected || []).some((x) => x.id === row.id);
|
||||
};
|
||||
|
||||
// Whether every record matching the current search/filter is currently selected
|
||||
const isAllSelected =
|
||||
multiple && totalRecord > 0 && (selected?.length || 0) === totalRecord;
|
||||
|
||||
const isPartiallySelected =
|
||||
multiple && (selected?.length || 0) > 0 && !isAllSelected;
|
||||
|
||||
const handleSelectAll = async () => {
|
||||
// Toggle off: if everything is already selected, clicking again clears selection
|
||||
if (isAllSelected) {
|
||||
setSelected([]);
|
||||
getData?.([]);
|
||||
return;
|
||||
}
|
||||
|
||||
setSelectAllLoading(true);
|
||||
|
||||
try {
|
||||
const pageSize = 1000;
|
||||
const requests = [];
|
||||
|
||||
for (let pageOffset = 0; pageOffset < totalRecord; pageOffset += pageSize) {
|
||||
const payload = new FilterTableBuilder()
|
||||
.setPaging(pageOffset, pageSize)
|
||||
.setOrder([name], true);
|
||||
|
||||
if (search) {
|
||||
payload.like(name, search);
|
||||
}
|
||||
|
||||
if (filter?.length) {
|
||||
filter.forEach(f =>
|
||||
payload.where(
|
||||
f.logic_operator || "=",
|
||||
f.name,
|
||||
f.value,
|
||||
f.operator || "AND",
|
||||
f.value1 || null,
|
||||
f.table_name || null
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
joins.forEach(join => {
|
||||
payload.addJoin(join.table, join.columns);
|
||||
});
|
||||
|
||||
requests.push(
|
||||
apiRequest.search(
|
||||
url,
|
||||
payload.build(),
|
||||
{
|
||||
headers: headersRequest,
|
||||
},
|
||||
isApiGo
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const responses = await Promise.all(requests);
|
||||
|
||||
const allRows = responses.flatMap(res => res.data || []);
|
||||
|
||||
setSelected(allRows);
|
||||
getData?.(allRows);
|
||||
} catch (err) {
|
||||
console.error("Select All Error:", err);
|
||||
} finally {
|
||||
setSelectAllLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<TextField
|
||||
@@ -291,27 +398,75 @@ export default function ListVirtual({
|
||||
|
||||
<List disablePadding>
|
||||
<>
|
||||
{multiple && (
|
||||
{multiple && rows.length > 0 && (
|
||||
<ListItemButton
|
||||
onClick={() => console.log("")}
|
||||
onClick={handleSelectAll}
|
||||
disabled={selectAllLoading || totalRecord === 0}
|
||||
sx={{ fontSize: 14 }}
|
||||
>
|
||||
Select All
|
||||
<ListItemIcon sx={{ minWidth: 36 }}>
|
||||
{selectAllLoading ? (
|
||||
<CircularProgress size={18} />
|
||||
) : (
|
||||
<Checkbox
|
||||
edge="start"
|
||||
checked={isAllSelected}
|
||||
indeterminate={isPartiallySelected}
|
||||
tabIndex={-1}
|
||||
disableRipple
|
||||
/>
|
||||
)}
|
||||
</ListItemIcon>
|
||||
<ListItemText
|
||||
primary={
|
||||
selectAllLoading
|
||||
? "Selecting all..."
|
||||
: isAllSelected
|
||||
? "Unselect All"
|
||||
: "Select All"
|
||||
}
|
||||
/>
|
||||
</ListItemButton>
|
||||
)}
|
||||
|
||||
{rows.map((row, i) => (
|
||||
<ListItemButton
|
||||
key={`${row.id}-${i}`}
|
||||
onClick={() => handleSelect(row)}
|
||||
>
|
||||
{multiple && (
|
||||
<Checkbox checked={isSelected(row)} />
|
||||
)}
|
||||
{rows.map((row, i) => {
|
||||
const warning = getRowWarning(row);
|
||||
|
||||
<ListItemText primary={row[name]} />
|
||||
</ListItemButton>
|
||||
))}
|
||||
return (
|
||||
<ListItemButton
|
||||
key={`${row.id}-${i}`}
|
||||
onClick={() => handleSelect(row)}
|
||||
disabled={selectAllLoading}
|
||||
>
|
||||
{multiple && (
|
||||
<Checkbox checked={isSelected(row)} />
|
||||
)}
|
||||
|
||||
<ListItemText
|
||||
primary={
|
||||
<Typography
|
||||
component="span"
|
||||
sx={{
|
||||
fontSize: 14,
|
||||
color: warning ? "error.main" : "text.primary",
|
||||
}}
|
||||
>
|
||||
{row[name]}
|
||||
{warning && (
|
||||
<Typography
|
||||
component="span"
|
||||
sx={{ fontSize: 12, color: "error.main", ml: 0.5 }}
|
||||
>
|
||||
{" "}| {warning}
|
||||
</Typography>
|
||||
)}
|
||||
</Typography>
|
||||
}
|
||||
secondary={row.join?.company_name}
|
||||
/>
|
||||
</ListItemButton>
|
||||
);
|
||||
})}
|
||||
|
||||
{loading && (
|
||||
<Typography variant="caption" sx={{ display: 'block', textAlign: 'center', p: 1 }}>
|
||||
@@ -329,8 +484,18 @@ export default function ListVirtual({
|
||||
</DialogContent>
|
||||
|
||||
<DialogActions>
|
||||
<Button onClick={handleClear} size="small">Clear</Button>
|
||||
<Button onClick={() => setOpen(false)} size="small">Cancel</Button>
|
||||
<Button onClick={handleClear} disabled={selectAllLoading}>Clear</Button>
|
||||
<Button onClick={() => setOpen(false)}>Cancel</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
getData?.(selected ?? (multiple ? [] : null));
|
||||
setOpen(false);
|
||||
}}
|
||||
variant="contained"
|
||||
disabled={selectAllLoading}
|
||||
>
|
||||
Save
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
</>
|
||||
|
||||
+1
-1
@@ -60,7 +60,7 @@ const theme = createTheme({
|
||||
MuiOutlinedInput: {
|
||||
styleOverrides: {
|
||||
root: {
|
||||
borderRadius: 12
|
||||
borderRadius: 4
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user