work on car code

This commit is contained in:
2024-12-18 11:34:21 +03:30
parent 9fccf770d5
commit aedeb62849
7 changed files with 168 additions and 21 deletions

View File

@@ -0,0 +1,86 @@
"use client";
import {Autocomplete, CircularProgress, TextField} from "@mui/material";
import {useEffect, useState} from "react";
import useRequest from "@/lib/hooks/useRequest";
import {debounce} from "@mui/material/utils";
const CarCode = ({carCode, setCarCode}) => {
const [inputValue, setInputValue] = useState('');
const [options, setOptions] = useState([]);
const [loading, setLoading] = useState(false);
const requestServer = useRequest();
const fetchCarCodes = (inputValue, controller) => {
const debouncer = debounce((query) => {
if (query.length < 2) {
setOptions([]);
return;
}
setLoading(true);
requestServer(`car-code/api?${query}`, "get", {
requestOptions: {signal: controller.signal}
})
.then((response) => {
setOptions(response || []);
})
.catch(() => {
setOptions([]);
})
.finally(() => {
setLoading(false);
});
}, 500)
debouncer(inputValue)
}
useEffect(() => {
const controller = new AbortController();
fetchCarCodes(inputValue, controller);
return () => controller.abort();
}, [inputValue]);
return (
<Autocomplete
value={carCode}
size="small"
onChange={(event, newValue) => {
setCarCode(newValue);
}}
inputValue={inputValue}
onInputChange={(event, newInputValue) => {
setInputValue(newInputValue);
}}
options={options}
getOptionLabel={(option) =>
typeof option === 'string' ? option : option.name || option.code
}
loading={loading}
loadingText="درحال جستجوی کد خودرو"
noOptionsText={
inputValue.length < 2
? "حداقل دو رقم از کد خودرو را وارد کنید"
: "کد خودرویی یافت نشد"
}
sx={{width: 300}}
renderInput={(params) => (
<TextField
{...params}
label="کد خودرو"
fullWidth
InputProps={{
...params.InputProps,
endAdornment: (
<>
{loading ? <CircularProgress size="25px" color="inherit"/> : null}
{params.InputProps.endAdornment}
</>
),
}}
/>
)}
/>
);
};
export default CarCode;

View File

@@ -0,0 +1,5 @@
export const formatCounter = (counter) => {
const minutes = Math.floor(counter / 60).toString().padStart(2, '0');
const seconds = (counter % 60).toString().padStart(2, '0');
return `${minutes}:${seconds}`;
};

View File

@@ -31,4 +31,7 @@ export const GET_OPERATOR_LIST = "/v3/api/fake-operator-list";
export const EXPORT_OPERATOR_LIST = "https://rms.rmto.ir/v2/road_patrols/operator/cartable/report";
export const GET_SUPERVISOR_LIST = "/v3/api/fake-supervisor-list";
export const EXPORT_SUPERVISOR_LIST = "https://rms.rmto.ir/v2/road_patrols/supervisor/cartable/report";
////**** mohammad check here ****////
export const GET_OTP_TOKEN = "https://rms.rmto.ir/v2/get_otp_token";
export const VERIFY_OTP = "https://rms.rmto.ir/v2/verify_otp";