Skip to content

Commit

Permalink
refactor: Update FilterFormStore logic to handle column type changes …
Browse files Browse the repository at this point in the history
…better.
  • Loading branch information
thiagodallacqua-hpe committed Oct 14, 2024
1 parent b0208ab commit 5b1bd68
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
20 changes: 11 additions & 9 deletions webui/react/src/components/FilterForm/components/FilterField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ const FilterField = ({
}: Props): JSX.Element => {
const users = Loadable.getOrElse([], useObservable(userStore.getUsers()));
const resourcePools = Loadable.getOrElse([], useObservable(clusterStore.resourcePools));
const currentColumn = columns.find((c) => c.column === field.columnName);
const currentColumn = useMemo(
() => columns.find((c) => c.column === field.columnName),
[columns, field.columnName],
);
const [metadataColumns, setMetadataColumns] = useState(() => new Map<string, number[]>()); // a map of metadata columns and found indexes

useEffect(() => {
Expand Down Expand Up @@ -112,19 +115,18 @@ const FilterField = ({
};

const onChangeColumnName = (value: SelectValue) => {
const prevType = currentColumn?.type;
const newColName = value?.toString() ?? '';
const newCol = columns.find((c) => c.column === newColName);
const prevType = field.type;
const [newColName, type] = (value?.toString() ?? '').split(' ');
const newCol = columns.find((c) => c.column === newColName && type === c.type);
if (newCol) {
Observable.batch(() => {
formStore.setFieldColumnName(field.id, newCol);

if ((SpecialColumnNames as ReadonlyArray<string>).includes(newColName)) {
formStore.setFieldOperator(field.id, Operator.Eq);
updateFieldValue(field.id, null);
} else if (prevType !== newCol?.type) {
} else if (prevType !== newCol.type) {
const defaultOperator: Operator =
AvailableOperators[newCol?.type ?? V1ColumnType.UNSPECIFIED][0];
AvailableOperators[newCol.type ?? V1ColumnType.UNSPECIFIED][0];
formStore.setFieldOperator(field.id, defaultOperator);
updateFieldValue(field.id, null);
}
Expand Down Expand Up @@ -264,9 +266,9 @@ const FilterField = ({
options={columns.map((col, idx) => ({
key: `${col.column} ${idx}`,
label: getColDisplayName(col),
value: col.column,
value: `${col.column} ${col.type}`,
}))}
value={field.columnName}
value={`${field.columnName} ${field.type}`}
width={'100%'}
onChange={onChangeColumnName}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ export class FilterFormStore {
col: Pick<V1ProjectColumn, 'location' | 'type' | 'column'>,
): void {
return this.#updateField(id, (form) => {
if (form.columnName === col.column && form.location === col.location) {
return form;
if (form.columnName === col.column) {
if (form.type === col.type && form.location === col.location) return form;
}
return {
...form,
Expand Down

0 comments on commit 5b1bd68

Please sign in to comment.