Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: FEAT: Array column should be filterable #1389

Merged
merged 7 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/components/fields/Array/Filter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { IFilterOperator } from "@src/components/fields/types";

export const operators: IFilterOperator[] = [
{
label: "equals",
value: "==",
},
{
label: "not equals",
value: "!=",
},
{
label: "contains the following",
value: "array-contains",
},
{
label: "contains atleast one of the following",
value: "array-contains-any",
},
];
24 changes: 18 additions & 6 deletions src/components/fields/Array/SideDrawerField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default function ArraySideDrawerField({
}: ISideDrawerFieldProps) {
const handleAddNew = (fieldType: ArraySupportedFiledTypes) => {
onChange([...(value || []), SupportedTypes[fieldType].initialValue]);
onDirty(true);
if (onDirty) onDirty(true);
};
const handleChange = (newValue_: any, indexUpdated: number) => {
onChange(
Expand All @@ -137,13 +137,13 @@ export default function ArraySideDrawerField({
const handleRemove = (index: number) => {
value.splice(index, 1);
onChange([...value]);
onDirty(true);
if (onDirty) onDirty(true);
onSubmit();
};

const handleClearField = () => {
onChange([]);
onSubmit();
if (onSubmit) onSubmit();
};

function handleOnDragEnd(result: DropResult) {
Expand All @@ -157,7 +157,7 @@ export default function ArraySideDrawerField({
const [removed] = list.splice(result.source.index, 1);
list.splice(result.destination.index, 0, removed);
onChange(list);
onSubmit();
if (onSubmit) onSubmit();
}

if (value === undefined || Array.isArray(value)) {
Expand All @@ -166,7 +166,11 @@ export default function ArraySideDrawerField({
<DragDropContext onDragEnd={handleOnDragEnd}>
<Droppable droppableId="columns_manager" direction="vertical">
{(provided) => (
<List {...provided.droppableProps} ref={provided.innerRef}>
<List
sx={{ padding: 0 }}
{...provided.droppableProps}
ref={provided.innerRef}
>
{(value || []).map((v: any, index: number) => (
<ArrayFieldInput
key={`index-${index}-value`}
Expand All @@ -185,7 +189,15 @@ export default function ArraySideDrawerField({
)}
</Droppable>
</DragDropContext>
<AddButton handleAddNew={handleAddNew} />
{props.operator === "array-contains" ? (
value?.length < 1 ? (
<AddButton handleAddNew={handleAddNew} />
) : (
<></>
)
) : (
<AddButton handleAddNew={handleAddNew} />
)}
</>
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/fields/Array/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IFieldConfig, FieldType } from "@src/components/fields/types";
import withRenderTableCell from "@src/components/Table/TableCell/withRenderTableCell";

import DisplayCell from "./DisplayCell";
import { operators } from "./Filter";

import BasicContextMenuActions from "@src/components/Table/ContextMenu/BasicCellContextMenuActions";

Expand All @@ -27,6 +28,7 @@ export const config: IFieldConfig = {
popoverProps: { PaperProps: { sx: { p: 1, minWidth: "200px" } } },
}),
SideDrawerField,
filter: { operators, defaultValue: [] },
requireConfiguration: false,
contextMenuActions: BasicContextMenuActions,
};
Expand Down
2 changes: 2 additions & 0 deletions src/components/fields/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ export interface ISideDrawerFieldProps<T = any> {
disabled: boolean;

row: TableRow;

operator?: TableFilter["operator"];
}

export interface ISettingsProps {
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/useFirestoreCollectionWithAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,13 @@ export const tableFiltersToFirestoreFilters = (filters: TableFilter[]) => {
where(filter.key.concat(".hex"), "!=", filter.value.hex.toString())
);
continue;
} else if (filter.operator === "array-contains") {
if (!filter.value || !filter.value.length) continue;
// make the value as a singular string
firestoreFilters.push(
where(filter.key, filter.operator as WhereFilterOp, filter.value[0])
);
continue;
rishi-raj-jain marked this conversation as resolved.
Show resolved Hide resolved
}
firestoreFilters.push(
where(filter.key, filter.operator as WhereFilterOp, filter.value)
Expand Down