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

KOGITO-3339 Integrate sorting in task inbox #462

Merged
merged 1 commit into from
Sep 25, 2020
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
2 changes: 2 additions & 0 deletions ui-packages/packages/common/src/graphql/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ const GET_TASKS_FOR_USER = gql`
$groups: [String!]
$offset: Int
$limit: Int
$orderBy: UserTaskInstanceOrderBy
) {
UserTaskInstances(
where: {
Expand All @@ -298,6 +299,7 @@ const GET_TASKS_FOR_USER = gql`
]
}
pagination: { offset: $offset, limit: $limit }
orderBy: $orderBy
) {
id
name
Expand Down
7 changes: 7 additions & 0 deletions ui-packages/packages/common/src/graphql/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import gql from 'graphql-tag';
import * as ApolloReactCommon from '@apollo/react-common';
import * as ApolloReactHooks from '@apollo/react-hooks';

export namespace GraphQL {
export type Maybe<T> = T | null;
export type Exact<T extends { [key: string]: any }> = {
[K in keyof T]: T[K];
};

/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
Expand Down Expand Up @@ -1048,6 +1050,7 @@ export namespace GraphQL {
groups?: Maybe<Array<Scalars['String']>>;
offset?: Maybe<Scalars['Int']>;
limit?: Maybe<Scalars['Int']>;
orderBy?: Maybe<UserTaskInstanceOrderBy>;
}>;

export type GetTasksForUserQuery = { __typename?: 'Query' } & {
Expand Down Expand Up @@ -1728,6 +1731,7 @@ export namespace GraphQL {
name
referenceName
description
name
priority
processInstanceId
processId
Expand Down Expand Up @@ -1881,6 +1885,7 @@ export namespace GraphQL {
$groups: [String!]
$offset: Int
$limit: Int
$orderBy: UserTaskInstanceOrderBy
) {
UserTaskInstances(
where: {
Expand All @@ -1891,6 +1896,7 @@ export namespace GraphQL {
]
}
pagination: { offset: $offset, limit: $limit }
orderBy: $orderBy
) {
id
name
Expand Down Expand Up @@ -1934,6 +1940,7 @@ export namespace GraphQL {
* groups: // value for 'groups'
* offset: // value for 'offset'
* limit: // value for 'limit'
* orderBy: // value for 'orderBy'
* },
* });
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import TaskConsoleContext, {
} from '../../../context/TaskConsoleContext/TaskConsoleContext';
import Columns from '../../../util/Columns';
import UserTaskInstance = GraphQL.UserTaskInstance;
import _ from 'lodash';

const UserTaskLoadingComponent = (
<Bullseye>
Expand All @@ -39,31 +40,37 @@ const UserTaskLoadingComponent = (
);

const TaskInbox: React.FC = props => {
const context: IContext<UserTaskInstance> = useContext(TaskConsoleContext);
const [defaultPageSize] = useState<number>(10);
const [isLoaded, setIsLoaded] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [queryOffset, setOffset] = useState<number>(0);
const [pageSize, setPageSize] = useState<number>(defaultPageSize);
const [isLoadingMore, setIsLoadingMore] = useState<boolean>(false);
const [tableData, setTableData] = useState<any[]>([]);

const context: IContext<UserTaskInstance> = useContext(TaskConsoleContext);
const [sorting, setSorting] = useState<boolean>(false);

const [
getUserTasks,
{ loading, error, data, refetch, networkStatus }
] = GraphQL.useGetTasksForUserLazyQuery({
fetchPolicy: 'network-only',
notifyOnNetworkStatusChange: true,
variables: {
user: context.getUser().id,
groups: context.getUser().groups,
offset: queryOffset,
limit: pageSize
}
notifyOnNetworkStatusChange: true
});

const columns: DataTableColumn[] = [
Columns.getTaskDescriptionColumn(true),
Columns.getDefaultColumn('processId', 'Process', false),
Columns.getDefaultColumn('priority', 'Priority', true),
Columns.getTaskStateColumn(true),
Columns.getDateColumn('started', 'Started', true),
Columns.getDateColumn('lastUpdate', 'Last update', true)
];

const onGetMoreInstances = (_queryOffset, _pageSize, _loadMore) => {
let newQueryLimit = _pageSize;
let newQueryOffset = _queryOffset;

setIsLoadingMore(_loadMore);

if (_queryOffset !== queryOffset) {
Expand All @@ -74,41 +81,93 @@ const TaskInbox: React.FC = props => {
setPageSize(_pageSize);
}

if (!_.isEmpty(context.getActiveQueryInfo().sortBy)) {
newQueryOffset = 0;
newQueryLimit = tableData.length + newQueryLimit;
}

context.getActiveQueryInfo().offset = newQueryOffset;
context.getActiveQueryInfo().maxElements += _pageSize;

fetchUserTasks(newQueryOffset, newQueryLimit);
};
const fetchUserTasks = (_queryOffset, _queryLimit) => {
getUserTasks({
variables: {
user: context.getUser().id,
groups: context.getUser().groups,
offset: _queryOffset,
limit: _pageSize
limit: _queryLimit,
orderBy: getSortByObject()
}
});
};

useEffect(() => {
onGetMoreInstances(queryOffset, pageSize, false);
if (!context.getActiveQueryInfo().maxElements) {
context.getActiveQueryInfo().maxElements = pageSize;
}
if (context.getActiveQueryInfo().offset) {
setOffset(context.getActiveQueryInfo().offset);
}
fetchUserTasks(0, context.getActiveQueryInfo().maxElements);
}, []);

useEffect(() => {
if (isLoadingMore === undefined || !isLoadingMore) {
setIsLoading(loading);
}

if (!loading && data !== undefined) {
const newData = tableData.concat(data.UserTaskInstances);
setTableData(newData);
setSorting(false);

if (_.isEmpty(context.getActiveQueryInfo().sortBy)) {
const newData = tableData.concat(data.UserTaskInstances);
setTableData(newData);
} else {
setTableData(data.UserTaskInstances);
}

if (queryOffset > 0 && tableData.length > 0) {
setIsLoadingMore(false);
}

if (!isLoaded) {
setIsLoaded(true);
}
}
}, [data]);

const onSorting = (index, direction) => {
setSorting(true);
if (direction) {
context.getActiveQueryInfo().sortBy = { index, direction };
} else {
context.getActiveQueryInfo().sortBy = null;
}
};

const getSortByObject = () => {
if (!_.isEmpty(context.getActiveQueryInfo().sortBy)) {
return _.set(
{},
columns[context.getActiveQueryInfo().sortBy.index].path,
context.getActiveQueryInfo().sortBy.direction.toUpperCase()
);
}
return null;
};

useEffect(() => {
if (!_.isEmpty(context.getActiveQueryInfo().sortBy)) {
fetchUserTasks(0, context.getActiveQueryInfo().maxElements);
}
}, [context.getActiveQueryInfo().sortBy]);

if (error) {
return <ServerErrors error={error} variant={'large'} />;
}

if (!isLoaded) {
if (!isLoaded || sorting) {
return UserTaskLoadingComponent;
}

Expand All @@ -121,19 +180,9 @@ const TaskInbox: React.FC = props => {
/>
);
}

const columns: DataTableColumn[] = [
Columns.getTaskDescriptionColumn(),
Columns.getDefaultColumn('processId', 'Process'),
Columns.getDefaultColumn('priority', 'Priority'),
Columns.getTaskStateColumn(),
Columns.getDateColumn('started', 'Started'),
Columns.getDateColumn('lastUpdate', 'Last update')
];

const mustShowMore =
isLoadingMore || (data && data.UserTaskInstances.length === pageSize);

isLoadingMore ||
context.getActiveQueryInfo().maxElements === tableData.length;
return (
<React.Fragment>
<DataTable
Expand All @@ -144,6 +193,8 @@ const TaskInbox: React.FC = props => {
error={error}
refetch={refetch}
LoadingComponent={UserTaskLoadingComponent}
onSorting={onSorting}
sortBy={context.getActiveQueryInfo().sortBy}
/>
{mustShowMore && (
<LoadMore
Expand Down
Loading