120 lines
3.2 KiB
JavaScript
120 lines
3.2 KiB
JavaScript
import { LoadingOutlined } from "@ant-design/icons";
|
|
import { useLazyQuery } from "@apollo/client";
|
|
import { Empty, Select } from "antd";
|
|
import _ from "lodash";
|
|
import React, { forwardRef, useEffect, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import {
|
|
SEARCH_JOBS_BY_ID_FOR_AUTOCOMPLETE,
|
|
SEARCH_JOBS_FOR_AUTOCOMPLETE,
|
|
} from "../../graphql/jobs.queries";
|
|
import AlertComponent from "../alert/alert.component";
|
|
const { Option } = Select;
|
|
|
|
const JobSearchSelect = (
|
|
{
|
|
value,
|
|
onChange,
|
|
onBlur,
|
|
disabled,
|
|
convertedOnly = false,
|
|
notExported = true,
|
|
clm_no = false,
|
|
},
|
|
ref
|
|
) => {
|
|
const { t } = useTranslation();
|
|
const [callSearch, { loading, error, data }] = useLazyQuery(
|
|
SEARCH_JOBS_FOR_AUTOCOMPLETE,
|
|
{
|
|
...(convertedOnly || notExported
|
|
? {
|
|
variables: {
|
|
...(convertedOnly ? { isConverted: true } : {}),
|
|
...(notExported ? { notExported: true } : {}),
|
|
},
|
|
}
|
|
: {}),
|
|
}
|
|
);
|
|
|
|
const [
|
|
callIdSearch,
|
|
{ loading: idLoading, error: idError, data: idData },
|
|
] = useLazyQuery(SEARCH_JOBS_BY_ID_FOR_AUTOCOMPLETE);
|
|
|
|
const executeSearch = (v) => {
|
|
callSearch(v);
|
|
};
|
|
const debouncedExecuteSearch = _.debounce(executeSearch, 500);
|
|
|
|
const handleSearch = (value) => {
|
|
debouncedExecuteSearch({ variables: { search: value } });
|
|
};
|
|
|
|
const [option, setOption] = useState(value);
|
|
|
|
useEffect(() => {
|
|
if (value === option && value) {
|
|
callIdSearch({ variables: { id: value } }); // Sometimes results in a no-op. Not sure how to fix.
|
|
}
|
|
}, [value, option, callIdSearch]);
|
|
|
|
const handleSelect = (value) => {
|
|
setOption(value);
|
|
if (value !== option && onChange) {
|
|
onChange(value);
|
|
}
|
|
};
|
|
|
|
const theOptions = _.uniqBy(
|
|
[
|
|
...(idData && idData.jobs_by_pk ? [idData.jobs_by_pk] : []),
|
|
...(data && data.search_jobs ? data.search_jobs : []),
|
|
],
|
|
"id"
|
|
);
|
|
|
|
return (
|
|
<div>
|
|
<Select
|
|
ref={ref}
|
|
disabled={disabled}
|
|
showSearch
|
|
autoFocus
|
|
value={option}
|
|
style={{
|
|
width: "100%",
|
|
}}
|
|
filterOption={false}
|
|
onSearch={handleSearch}
|
|
// onChange={setOption}
|
|
onChange={handleSelect}
|
|
onSelect={handleSelect}
|
|
notFoundContent={loading ? <LoadingOutlined /> : <Empty />}
|
|
onBlur={onBlur}
|
|
>
|
|
{theOptions
|
|
? theOptions.map((o) => (
|
|
<Option key={o.id} value={o.id}>
|
|
{`${clm_no ? `${o.clm_no} | ` : ""}${
|
|
o.ro_number || t("general.labels.na")
|
|
} | ${o.ownr_ln || ""} ${o.ownr_fn || ""} ${
|
|
o.ownr_co_nm ? ` ${o.ownr_co_num}` : ""
|
|
}| ${o.v_model_yr || ""} ${o.v_make_desc || ""} ${
|
|
o.v_model_desc || ""
|
|
}`}
|
|
</Option>
|
|
))
|
|
: null}
|
|
</Select>
|
|
{idLoading || loading ? <LoadingOutlined /> : null}
|
|
{error ? <AlertComponent message={error.message} type="error" /> : null}
|
|
{idError ? (
|
|
<AlertComponent message={idError.message} type="error" />
|
|
) : null}
|
|
</div>
|
|
);
|
|
};
|
|
export default forwardRef(JobSearchSelect);
|