CE-1180 Updated to support multi-value keys. also, support tables (e.g., api tables) w/o a primary key, by redirecting a successful UK lookup to the (new) /key?queryString record-view screen

This commit is contained in:
2024-05-10 15:45:44 -05:00
parent 8c9224aceb
commit 5693661d20

View File

@ -35,6 +35,7 @@ import DialogTitle from "@mui/material/DialogTitle";
import Grid from "@mui/material/Grid"; import Grid from "@mui/material/Grid";
import Icon from "@mui/material/Icon"; import Icon from "@mui/material/Icon";
import TextField from "@mui/material/TextField"; import TextField from "@mui/material/TextField";
import {any} from "prop-types";
import React, {useState} from "react"; import React, {useState} from "react";
import {useNavigate} from "react-router-dom"; import {useNavigate} from "react-router-dom";
import {QCancelButton} from "qqq/components/buttons/DefaultButtons"; import {QCancelButton} from "qqq/components/buttons/DefaultButtons";
@ -71,7 +72,12 @@ function hasGotoFieldNames(tableMetaData: QTableMetaData): boolean
function GotoRecordDialog(props: Props): JSX.Element function GotoRecordDialog(props: Props): JSX.Element
{ {
const fields: QFieldMetaData[] = []; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// this is an array of array of fields. //
// that is - each entry in the top-level array is a set of fields that can be used together to goto a record //
// such as (pkey), (ukey-field1,ukey-field2). //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
const options: QFieldMetaData[][] = [];
let pkey = props?.tableMetaData?.fields.get(props?.tableMetaData?.primaryKeyField); let pkey = props?.tableMetaData?.fields.get(props?.tableMetaData?.primaryKeyField);
let addedPkey = false; let addedPkey = false;
@ -82,31 +88,38 @@ function GotoRecordDialog(props: Props): JSX.Element
{ {
for (let i = 0; i < mdbMetaData.gotoFieldNames.length; i++) for (let i = 0; i < mdbMetaData.gotoFieldNames.length; i++)
{ {
// todo - multi-field keys!! const option: QFieldMetaData[] = [];
let fieldName = mdbMetaData.gotoFieldNames[i][0]; options.push(option);
let field = props.tableMetaData.fields.get(fieldName); for (let j = 0; j < mdbMetaData.gotoFieldNames[i].length; j++)
if (field)
{ {
fields.push(field); let fieldName = mdbMetaData.gotoFieldNames[i][j];
let field = props.tableMetaData.fields.get(fieldName);
if (field.name == pkey.name) if (field)
{ {
addedPkey = true; option.push(field);
if (pkey != null && field.name == pkey.name)
{
addedPkey = true;
}
} }
} }
} }
} }
} }
//////////////////////////////////////////////////////////////////////////////////////////
// if pkey wasn't in the gotoField options meta-data, go ahead add it as an option here //
//////////////////////////////////////////////////////////////////////////////////////////
if (pkey && !addedPkey) if (pkey && !addedPkey)
{ {
fields.unshift(pkey); options.unshift([pkey]);
} }
const makeInitialValues = () => const makeInitialValues = () =>
{ {
const rs = {} as { [field: string]: string }; const rs = {} as { [field: string]: string };
fields.forEach((field) => rs[field.name] = ""); options.forEach((option) => option.forEach((field) => rs[field.name] = ""));
return (rs); return (rs);
}; };
@ -141,11 +154,16 @@ function GotoRecordDialog(props: Props): JSX.Element
} }
else if (e.key == "Enter" && targetId?.startsWith("gotoInput-")) else if (e.key == "Enter" && targetId?.startsWith("gotoInput-"))
{ {
const index = targetId?.replaceAll("gotoInput-", ""); const parts = targetId?.split(/-/);
const index = parts[1];
document.getElementById("gotoButton-" + index).click(); document.getElementById("gotoButton-" + index).click();
} }
}; };
/***************************************************************************
** event handler for close button
***************************************************************************/
const closeRequested = () => const closeRequested = () =>
{ {
if (props.mayClose) if (props.mayClose)
@ -154,10 +172,47 @@ function GotoRecordDialog(props: Props): JSX.Element
} }
}; };
const goClicked = async (fieldName: string) =>
/*******************************************************************************
** function to say if an option's submit button should be disabled
*******************************************************************************/
const isOptionSubmitButtonDisabled = (optionIndex: number) =>
{
let anyFieldsInThisOptionHaveAValue = false;
options[optionIndex].forEach((field) =>
{
if(values[field.name])
{
anyFieldsInThisOptionHaveAValue = true;
}
})
if(!anyFieldsInThisOptionHaveAValue)
{
return (true);
}
return (false);
}
/***************************************************************************
** event handler for clicking an 'option's go/submit button
***************************************************************************/
const optionGoClicked = async (optionIndex: number) =>
{ {
setError(""); setError("");
const filter = new QQueryFilter([new QFilterCriteria(fieldName, QCriteriaOperator.EQUALS, [values[fieldName]])], null, null, "AND", null, 10);
const criteria: QFilterCriteria[] = [];
const queryStringParts: string[] = [];
options[optionIndex].forEach((field) =>
{
criteria.push(new QFilterCriteria(field.name, QCriteriaOperator.EQUALS, [values[field.name]]))
queryStringParts.push(`${field.name}=${encodeURIComponent(values[field.name])}`)
})
const filter = new QQueryFilter(criteria, null, null, "AND", null, 10);
try try
{ {
const queryResult = await qController.query(props.tableMetaData.name, filter, null, props.tableVariant); const queryResult = await qController.query(props.tableMetaData.name, filter, null, props.tableVariant);
@ -168,12 +223,26 @@ function GotoRecordDialog(props: Props): JSX.Element
} }
else if (queryResult.length == 1) else if (queryResult.length == 1)
{ {
navigate(`${props.metaData.getTablePathByName(props.tableMetaData.name)}/${encodeURIComponent(queryResult[0].values.get(props.tableMetaData.primaryKeyField))}`); if(options[optionIndex].length == 1 && options[optionIndex][0].name == pkey?.name)
{
/////////////////////////////////////////////////
// navigate by pkey, if that's how we searched //
/////////////////////////////////////////////////
navigate(`${props.metaData.getTablePathByName(props.tableMetaData.name)}/${encodeURIComponent(queryResult[0].values.get(props.tableMetaData.primaryKeyField))}`);
}
else
{
/////////////////////////////////
// else navigate by unique-key //
/////////////////////////////////
navigate(`${props.metaData.getTablePathByName(props.tableMetaData.name)}/key/?${queryStringParts.join("&")}`);
}
close(); close();
} }
else else
{ {
setError("More than 1 record found..."); setError("More than 1 record was found...");
setTimeout(() => setError(""), 3000); setTimeout(() => setError(""), 3000);
} }
} }
@ -187,7 +256,7 @@ function GotoRecordDialog(props: Props): JSX.Element
if (props.tableMetaData) if (props.tableMetaData)
{ {
if (fields.length == 0 && !error) if (options.length == 0 && !error)
{ {
setError("This table is not configured for this feature."); setError("This table is not configured for this feature.");
} }
@ -200,31 +269,38 @@ function GotoRecordDialog(props: Props): JSX.Element
<DialogContent> <DialogContent>
{props.subHeader} {props.subHeader}
{ {
fields.map((field, index) => options.map((option, optionIndex) =>
( <Box key={optionIndex}>
<Grid key={field.name} container alignItems="center" py={1}> {
<Grid item xs={3} textAlign="right" pr={2}> option.map((field, index) =>
{field.label} (
</Grid> <Grid key={field.name} container alignItems="center" py={1}>
<Grid item xs={6}> <Grid item xs={3} textAlign="right" pr={2}>
<TextField {field.label}
id={`gotoInput-${index}`} </Grid>
autoFocus={index == 0} <Grid item xs={6}>
autoComplete="off" <TextField
inputProps={{width: "100%"}} id={`gotoInput-${optionIndex}-${index}`}
onChange={(e) => handleChange(field.name, e.target.value)} autoFocus={optionIndex == 0 && index == 0}
value={values[field.name]} autoComplete="off"
sx={{width: "100%"}} inputProps={{width: "100%"}}
onFocus={event => event.target.select()} onChange={(e) => handleChange(field.name, e.target.value)}
/> value={values[field.name]}
</Grid> sx={{width: "100%"}}
<Grid item xs={1} pl={2}> onFocus={event => event.target.select()}
<MDButton id={`gotoButton-${index}`} type="submit" variant="gradient" color="info" size="small" onClick={() => goClicked(field.name)} fullWidth startIcon={<Icon>double_arrow</Icon>} disabled={`${values[field.name]}`.length == 0}> />
Go </Grid>
</MDButton> <Grid item xs={1} pl={2}>
</Grid> {
</Grid> (index == option.length - 1) &&
)) <MDButton id={`gotoButton-${optionIndex}`} type="submit" variant="gradient" color="info" size="small" onClick={() => optionGoClicked(optionIndex)} fullWidth startIcon={<Icon>double_arrow</Icon>} disabled={isOptionSubmitButtonDisabled(optionIndex)}>Go</MDButton>
}
</Grid>
</Grid>
))
}
</Box>
)
} }
{ {
error && error &&