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

Adding custom hook for selection on list component #5757

Merged

Conversation

gnehapk
Copy link
Contributor

@gnehapk gnehapk commented Jun 17, 2020

Creating a custom hook for abstracting the selection logic for a list component

@openshift-ci-robot openshift-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. component/ceph Related to ceph-storage-plugin labels Jun 17, 2020
@openshift-ci-robot openshift-ci-robot added component/lso Related to local-storage-operator-plugin component/shared Related to console-shared labels Jun 17, 2020
@openshift-ci-robot openshift-ci-robot added the component/core Related to console core functionality label Jun 17, 2020
@gnehapk gnehapk changed the title [WIP] Adding custom hook for selection on list component Adding custom hook for selection on list component Jun 17, 2020
@openshift-ci-robot openshift-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jun 17, 2020
@gnehapk
Copy link
Contributor Author

gnehapk commented Jun 18, 2020

/assign @spadgett @bipuladh

@bipuladh
Copy link
Contributor

We can use the reducers as well if passing data is getting too complicated https://github.com/openshift/console/blob/master/frontend/packages/console-plugin-sdk/src/typings/reducers.ts.

@spadgett
Copy link
Member

cc @jcaianirh who was going to look bulk actions

@gnehapk
Copy link
Contributor Author

gnehapk commented Jun 19, 2020

/assign @rawagner

@gnehapk gnehapk force-pushed the list-selection-component branch 2 times, most recently from 32500da to 074ff13 Compare June 23, 2020 04:11
@gnehapk
Copy link
Contributor Author

gnehapk commented Jun 23, 2020

/retest

1 similar comment
@gnehapk
Copy link
Contributor Author

gnehapk commented Jun 23, 2020

/retest

onRowSelected(getAllSelectedRows(data, uniqueUIDs));
};

const updateSelectedRows = (rows: R[]) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd wrap this into React.useCallback

uniqueUIDs = _.xor(selectedRows, [rowData?.props?.id]);
}
setSelectedRows(uniqueUIDs);
onRowSelected(getAllSelectedRows(data, uniqueUIDs));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont see a lot of value in having getAllSelectedRows as separate function

Just inlining works fine (and is more transparent IMO)

Suggested change
onRowSelected(getAllSelectedRows(data, uniqueUIDs));
onRowSelected(data.filter((datum) => selectedRows.includes(datum.metadata.uid)));


const onSelect = (_event, isSelected, rowIndex, rowData) => {
let uniqueUIDs = [];
if (rowIndex === -1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when can rowIndex be -1 ?

I dont understand why this is not just

const selectedUIDs = isSelected ? selectedRows.push(rowData.props.id) : selectedRows.filter((uid) => rowData.props.id != uid);
setSelectedRows(selectedUIDs);

why do we need to do all the uniq and xors ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you click select all on top of the table you will get a rowIndex of -1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, when you select checkbox that corresponds to select all nodes, the rowIndex comes as -1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uniq is done to uniquely get the uid of the nodes as its possible that the visibleRows and selectedRows can have common uid in it.

} => {
const [selectedRows, setSelectedRows] = React.useState<string[]>([]);

const onSelect = (_event, isSelected, rowIndex, rowData) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably be wrapped in React.useCallback too

@@ -153,7 +153,11 @@ export const CreateOCSServiceForm = withHandlePromise<
kind={NodeModel.kind}
showTitle={false}
ListComponent={NodeTable}
customData={{ selectedNodes, setSelectedNodes, visibleRows, setVisibleRows }}
customData={{
onRowSelected: (selectedNodes: NodeKind[]) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onRowSelected: (selectedNodes: NodeKind[]) => {
onRowSelected: setNodes

@gnehapk
Copy link
Contributor Author

gnehapk commented Jun 25, 2020

@rawagner Addressed your comments. Please review.

if (isSelected) {
uniqueUIDs = _.uniq([...visibleRows, ...selectedRows]);
} else {
uniqueUIDs = _.uniq(selectedRows.filter((uid) => !visibleRows.includes(uid)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you dont have to do uniq here

@gnehapk gnehapk force-pushed the list-selection-component branch 2 times, most recently from 15c3932 to 9ba2b39 Compare June 26, 2020 01:49
@gnehapk gnehapk force-pushed the list-selection-component branch 3 times, most recently from 66ef398 to 24af09b Compare June 28, 2020 09:31
@gnehapk
Copy link
Contributor Author

gnehapk commented Jun 28, 2020

@rawagner @bipuladh Updated the PR.

@gnehapk
Copy link
Contributor Author

gnehapk commented Jun 29, 2020

@rawagner Updated. Please review.

@rawagner
Copy link
Contributor

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Jun 29, 2020
@openshift-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: gnehapk, rawagner

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 29, 2020
@openshift-merge-robot openshift-merge-robot merged commit ed7b51e into openshift:master Jun 29, 2020
@@ -50,25 +52,28 @@ const getColumns = () => {
];
};

const getSelected = (selected: NodeKind[], nodeUID: string) =>
selected.map((node) => node.metadata.uid).includes(nodeUID);
const isSelected = (selected: Set<string>, nodeUID: string): boolean => selected.has(nodeUID);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just inline use it. I don't think we need a helper function for this.

Comment on lines +58 to +62
{
componentProps,
}: {
componentProps: { data: NodeKind[] };
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you destructuring componentProps?

Suggested change
{
componentProps,
}: {
componentProps: { data: NodeKind[] };
},
componentProps: { data: NodeKind[] };

Can't it be ^^?

selected: _.isArray(selectedNodes)
? getSelected(selectedNodes, node.metadata.uid)
selected: selectedNodes
? isSelected(selectedNodes, node.metadata.uid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
? isSelected(selectedNodes, node.metadata.uid)
? (selectedNodes.has(node.metadata.uid))


const onSelect = React.useCallback(
(_event, isSelected, rowIndex, rowData) => {
const uniqueUIDs: Set<string> = selectedRows ? new Set([...selectedRows]) : new Set<string>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't selectedRows being initialized as a Set?

if (_.isEmpty(rows)) return true;
return Object.keys(rowUIDMap).some((uid) => rows?.[uid]?.selected !== rowUIDMap?.[uid]?.selected);
};
const isSelected = (selected: Set<string>, nodeUID: string): boolean => selected.has(nodeUID);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use has

@spadgett spadgett added this to the v4.6 milestone Jun 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. component/ceph Related to ceph-storage-plugin component/core Related to console core functionality component/lso Related to local-storage-operator-plugin component/shared Related to console-shared lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants