Skip to content

Commit

Permalink
Merge pull request #76 from ChannelFinder/october-refactor
Browse files Browse the repository at this point in the history
October refactor
  • Loading branch information
tynanford authored Oct 10, 2023
2 parents 0d20c5f + c9a16da commit 3e21a01
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 184 deletions.
184 changes: 60 additions & 124 deletions src/components/home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,81 +41,51 @@ function Home(props) {
const [searchTags, setSearchTags] = useState([]);
const [isLoading, setIsLoading] = useState(false);

const cfProps = {
"pvName": setPVName,
"recordDesc": setRecordDesc,
"recordType": setRecordType,
"iocName": setIOCName,
"hostName": setHostName,
"pvStatus": setPVStatus,
"alias": setAliasOf,
[extraPropAName]: setExtraPropAValue,
[extraPropBName]: setExtraPropBValue,
}

const handleSearchType = (e, newSearchType) => {
if (newSearchType !== null) {
setStandardSearch(newSearchType);
}
};
const handlePVNameChange = (e) => {
setPVName(e.target.value);
};
const handleHostNameChange = (e) => {
setHostName(e.target.value);
};
const handleIOCNameChange = (e) => {
setIOCName(e.target.value);
};
const handlePVStatusChange = (e) => {
setPVStatus(e.target.value);
};
const handleAliasOfChange = (e) => {
setAliasOf(e.target.value);
};
const handleRecordTypeChange = (e) => {
setRecordType(e.target.value);
};
const handleRecordDescChange = (e) => {
setRecordDesc(e.target.value);
};
const handleExtraPropAChange = (e) => {
setExtraPropAValue(e.target.value);
};
const handleExtraPropBChange = (e) => {
setExtraPropBValue(e.target.value);
};
const handleInputChange = (e) => {
cfProps[e.target.name](e.target.value);
}
const handleFreeformChange = (e) => {
setFreeformQuery(e.target.value);
}
const queryPVs = (parameters) => {
api.CF_QUERY(parameters)
.then((data) => {
if (data === null) {
console.log("Null PV data from Channel Finder");
setCFData(null);
setIsLoading(false);
}
else {
setCFData(data);
setIsLoading(false);
}
})
.catch((err) => {
console.log("Error in fetch of channel finder data");
console.log(err);
props.handleErrorMessage("Error in EPICS Channel Finder query");
props.handleSeverity("error");
props.handleOpenErrorAlert(true);
setIsLoading(false);
setCFData(null);
})
};

const queryCount = (parameters) => {
api.COUNT_QUERY(parameters)
const fetchData = (apiCall, parameters, setData, dataName, displayError = false, setLoading = false) => {
apiCall(parameters)
.then((data) => {
if (data === null) {
console.log("Null count data from Channel Finder");
setPVCount(null)
}
else {
setPVCount(data);
console.log(`Null ${dataName} data from Channel Finder`);
}
setData(data);
if (setLoading) setIsLoading(false);
})
.catch((err) => {
console.log("Error in PV count fetch");
console.log(`Error in ${dataName} fetch from Channel Finder`);
console.log(err);
setPVCount(null)
if (displayError) {
props.handleErrorMessage("Error in EPICS Channel Finder Query");
props.handleSeverity("error");
props.handleOpenErrorAlert(true);
}
setData(null);
if (setLoading) setIsLoading(false);
})

}

useEffect(() => {
Expand Down Expand Up @@ -155,65 +125,33 @@ function Home(props) {
freeformQuery = params["pvName"];
delete params["pvName"];
}
if ("recordDesc" in params) {
setRecordDesc(params["recordDesc"]);
freeformQuery = freeformQuery.concat(` recordDesc=${params["recordDesc"]}`);
delete params["recordDesc"];
}
if ("recordType" in params) {
setRecordType(params["recordType"]);
freeformQuery = freeformQuery.concat(` recordType=${params["recordType"]}`)
delete params["recordType"];
}
if ("iocName" in params) {
setIOCName(params["iocName"]);
freeformQuery = freeformQuery.concat(` iocName=${params["iocName"]}`);
delete params["iocName"];
}
if ("hostName" in params) {
setHostName(params["hostName"]);
freeformQuery = freeformQuery.concat(` hostName=${params["hostName"]}`);
delete params["hostName"];
}
if ("pvStatus" in params) {
setPVStatus(params["pvStatus"]);
freeformQuery = freeformQuery.concat(` pvStatus=${params["pvStatus"]}`);
delete params["pvStatus"]
} else {
setPVStatus("*");
}
if ("alias" in params) {
setAliasOf(params["alias"]);
freeformQuery = freeformQuery.concat(` alias=${params["alias"]}`);
delete params["alias"];
}
if (extraPropAName in params) {
setExtraPropAValue(params[extraPropAName]);
freeformQuery = freeformQuery.concat(` ${extraPropAName}=${params[extraPropAName]}`);
delete params[extraPropAName];
}
if (extraPropBName in params) {
setExtraPropBValue(params[extraPropBName]);
freeformQuery = freeformQuery.concat(` ${extraPropBName}=${params[extraPropBName]}`);
delete params[extraPropBName];
// Populate the stock & extra prop CF property values
for (const prop in cfProps) {
if (prop in params) {
if (prop === "pvName") continue;
cfProps[prop](params[prop]);
freeformQuery = freeformQuery.concat(` ${prop}=${params[prop]}`);
delete params[prop];
} else if (prop === "pvStatus") {
cfProps[prop]("*");
}
}
if (!standardSearch) {
for (let key in params) {
const value = params[key];
if (key.includes('tag')) {
freeformQuery = freeformQuery.concat(` ${value}`);
} else {
freeformQuery = freeformQuery.concat(` ${key}=${value}`);
}
// Fill out the freeform query with any extra search props
for (let key in params) {
const value = params[key];
if (key.includes('tag')) {
freeformQuery = freeformQuery.concat(` ${value}`);
} else {
freeformQuery = freeformQuery.concat(` ${key}=${value}`);
}
}
setFreeformQuery(freeformQuery);
let resetParams = {}
searchParams.forEach((val, key) => { if (val !== "") { resetParams[key] = val } });
resetParams["standardSearch"] = standardSearch;
setIsLoading(true);
queryPVs(resetParams);
queryCount(resetParams);
fetchData(api.CF_QUERY, resetParams, setCFData, "PV", true, true);
fetchData(api.COUNT_QUERY, resetParams, setPVCount, "count");
}
else {
setPVName("");
Expand Down Expand Up @@ -249,10 +187,10 @@ function Home(props) {
if (e.target.recordDesc.value) { params[e.target.recordDesc.name] = e.target.recordDesc.value; }
}
if (extraPropAName !== null) {
if (e.target.extraPropA.value) { params[process.env.REACT_APP_EXTRA_PROP] = e.target.extraPropA.value; }
if (e.target[extraPropAName].value) { params[process.env.REACT_APP_EXTRA_PROP] = e.target[extraPropAName].value; }
}
if (extraPropBName !== null) {
if (e.target.extraPropB.value) { params[process.env.REACT_APP_SECOND_EXTRA_PROP] = e.target.extraPropB.value; }
if (e.target[extraPropBName].value) { params[process.env.REACT_APP_SECOND_EXTRA_PROP] = e.target[extraPropBName].value; }
}
return params;
}
Expand Down Expand Up @@ -286,8 +224,8 @@ function Home(props) {
params = parseFreeformSearch(e);
}
params['standardSearch'] = standardSearch;
queryCount(params);
queryPVs(params);
fetchData(api.CF_QUERY, params, setCFData, "PV", true, true);
fetchData(api.COUNT_QUERY, params, setPVCount, "count");
setSearchParams(params);
}

Expand Down Expand Up @@ -352,19 +290,17 @@ function Home(props) {
{
standardSearch ? (
<ParamSearch
pvName={pvName} handlePVNameChange={handlePVNameChange}
hostName={hostName} handleHostNameChange={handleHostNameChange}
iocName={iocName} handleIOCNameChange={handleIOCNameChange}
pvStatus={pvStatus} handlePVStatusChange={handlePVStatusChange}
aliasOf={aliasOf} handleAliasOfChange={handleAliasOfChange}
recordType={recordType} handleRecordTypeChange={handleRecordTypeChange}
pvName={pvName}
hostName={hostName} iocName={iocName}
pvStatus={pvStatus} aliasOf={aliasOf}
recordType={recordType} recordDesc={recordDesc}
recordTypeAutocompleteKey={recordTypeAutocompleteKey}
recordDesc={recordDesc} handleRecordDescChange={handleRecordDescChange}
extraPropAName={extraPropAName} extraPropBName={extraPropBName}
extraPropAValue={extraPropAValue} handleExtraPropAChange={handleExtraPropAChange}
extraPropBValue={extraPropBValue} handleExtraPropBChange={handleExtraPropBChange}
extraPropAValue={extraPropAValue} extraPropBValue={extraPropBValue}
handleInputChange={handleInputChange}
handleClear={handleClear}
setIsLoading={setIsLoading}></ParamSearch>
setIsLoading={setIsLoading}
></ParamSearch>
) : (
<FreeSearch freeformQuery={freeformQuery} setFreeformQuery={setFreeformQuery} handleFreeformChange={handleFreeformChange}
handleClear={handleClear} searchProperties={searchProperties} searchTags={searchTags} />
Expand Down
Loading

0 comments on commit 3e21a01

Please sign in to comment.