Fix pagination in UI 97/58397/1
authorMalek <malek.zoabi@amdocs.com>
Wed, 1 Aug 2018 10:45:39 +0000 (13:45 +0300)
committerMalek <malek.zoabi@amdocs.com>
Wed, 1 Aug 2018 10:46:43 +0000 (13:46 +0300)
Issue-ID: SDC-1585
Change-Id: I95e69beabc1a7d8845c96763543ae3bea3395a63
Signed-off-by: Malek <malek.zoabi@amdocs.com>
workflow-designer-ui/src/main/frontend/src/features/catalog/Catalog.js
workflow-designer-ui/src/main/frontend/src/features/catalog/CatalogView.jsx
workflow-designer-ui/src/main/frontend/src/features/catalog/__tests__/catalogActions-test.js
workflow-designer-ui/src/main/frontend/src/features/catalog/__tests__/catalogReducer-test.js
workflow-designer-ui/src/main/frontend/src/features/catalog/__tests__/catalogSagas-test.js
workflow-designer-ui/src/main/frontend/src/features/catalog/catalogActions.js
workflow-designer-ui/src/main/frontend/src/features/catalog/catalogApi.js
workflow-designer-ui/src/main/frontend/src/features/catalog/catalogConstants.js
workflow-designer-ui/src/main/frontend/src/features/catalog/catalogReducer.js
workflow-designer-ui/src/main/frontend/src/features/catalog/catalogSagas.js
workflow-designer-ui/src/main/frontend/src/features/catalog/views/Workflows.jsx

index 5eeaaa2..8ff33bd 100644 (file)
@@ -28,7 +28,8 @@ const mapStateToProps = state => ({
 });
 
 const mapDispatchToProps = dispatch => ({
-    handleFetchWorkflow: (sort, page) => dispatch(fetchWorkflow(sort, page)),
+    handleFetchWorkflow: (sort, offset) =>
+        dispatch(fetchWorkflow(sort, offset)),
     handleResetWorkflow: () => dispatch(resetWorkflow()),
     clearWorkflow: () => dispatch(clearWorkflowAction),
     showNewWorkflowModal: () =>
index 99cee75..7882434 100644 (file)
@@ -53,11 +53,14 @@ class CatalogView extends React.Component {
 
     handleScroll = () => {
         const {
-            catalog: { page, sort },
+            catalog: {
+                paging: { offset },
+                sort
+            },
             handleFetchWorkflow
         } = this.props;
 
-        handleFetchWorkflow(sort, page);
+        handleFetchWorkflow(sort, offset);
     };
 
     goToOverviewPage = id => {
@@ -67,7 +70,11 @@ class CatalogView extends React.Component {
 
     render() {
         const { catalog, showNewWorkflowModal } = this.props;
-        const { sort, hasMore, total, results } = catalog;
+        const {
+            sort,
+            paging: { hasMore, total },
+            items
+        } = catalog;
         const alphabeticalOrder = sort[NAME];
 
         return (
@@ -85,7 +92,7 @@ class CatalogView extends React.Component {
                         <div className="main__content">
                             <AddWorkflow onClick={showNewWorkflowModal} />
                             <Workflows
-                                results={results}
+                                items={items}
                                 onWorkflowClick={this.goToOverviewPage}
                             />
                         </div>
index f49c2fd..43c38e0 100644 (file)
@@ -20,7 +20,7 @@ import {
     FETCH_WORKFLOW,
     UPDATE_WORKFLOW,
     RESET_WORKFLOW,
-    PAGE_SIZE,
+    LIMIT,
     NAME,
     ASC
 } from 'features/catalog/catalogConstants';
@@ -33,24 +33,28 @@ import {
 describe('Catalog Actions', () => {
     it('should have `fetchWorkflow` action', () => {
         const sort = { [NAME]: ASC };
-        const page = 0;
+        const offset = 0;
 
-        expect(fetchWorkflow(sort, page)).toEqual({
+        expect(fetchWorkflow(sort, offset)).toEqual({
             type: FETCH_WORKFLOW,
             payload: {
                 sort,
-                size: PAGE_SIZE,
-                page
+                limit: LIMIT,
+                offset
             }
         });
     });
 
     it('should have `updateWorkflow` action', () => {
         const payload = {
-            results: [],
-            total: 0,
-            page: 0,
-            size: 0,
+            paging: {
+                offset: 1,
+                limit: 1,
+                count: 1,
+                hasMore: false,
+                total: 2
+            },
+            items: [],
             sort: {
                 name: 'asc'
             }
index c4f34e7..5444bf9 100644 (file)
@@ -22,43 +22,62 @@ import { updateWorkflow, resetWorkflow } from 'features/catalog/catalogActions';
 
 describe('Catalog Reducer', () => {
     const state = {
-        hasMore: true,
-        results: [
+        paging: {
+            offset: 1,
+            limit: 1,
+            count: 1,
+            hasMore: false,
+            total: 2
+        },
+        sort: {
+            [NAME]: ASC
+        },
+        items: [
             {
-                id: '755eab7752374a2380544065b59b082d',
-                name: 'Workflow 1',
-                description: 'description description 1'
+                id: 'c5b7ca1a0f7944bfa948b85b32c5f314',
+                name: 'Workflow_2',
+                description: null,
+                versionStates: ['DRAFT'],
+                versions: null
             },
             {
-                id: 'ef8159204dac4c10a85b29ec30b4bd56',
-                name: 'Workflow 2',
-                description: 'description description 2'
+                id: '221336ef3f1645c686bc81899368ac27',
+                name: 'Workflow_1',
+                description: null,
+                versionStates: ['DRAFT'],
+                versions: null
             }
-        ],
-        total: 0,
-        sort: {
-            [NAME]: ASC
-        }
+        ]
     };
+
     const sort = {
         [NAME]: DESC
     };
-    const page = 0;
-    const data = {
-        total: 20,
-        size: 100,
-        page,
-        sort,
-        results: [
+
+    const offset = 0;
+
+    const dataPayload = {
+        paging: {
+            offset,
+            limit: 10,
+            count: 2,
+            hasMore: false,
+            total: 2
+        },
+        items: [
             {
-                id: '755eab7752374a2380544065b59b082d',
-                name: 'Workflow 11',
-                description: 'description description 11'
+                id: 'c5b7ca1a0f7944bfa948b85b32c5f314',
+                name: 'Workflow_2',
+                description: null,
+                versionStates: ['DRAFT'],
+                versions: null
             },
             {
-                id: 'ef8159204dac4c10a85b29ec30b4bd56',
-                name: 'Workflow 22',
-                description: 'description description 22'
+                id: '221336ef3f1645c686bc81899368ac27',
+                name: 'Workflow_1',
+                description: null,
+                versionStates: ['DRAFT'],
+                versions: null
             }
         ]
     };
@@ -68,19 +87,24 @@ describe('Catalog Reducer', () => {
     });
 
     it('should replace results when page is first', () => {
-        expect(catalogReducer(state, updateWorkflow({ ...data }))).toEqual({
+        expect(
+            catalogReducer(state, updateWorkflow({ sort, ...dataPayload }))
+        ).toEqual({
             ...initialState,
-            ...data,
-            hasMore: data.results.length < data.total,
-            page,
-            sort
+            sort,
+            ...dataPayload
         });
     });
 
     it('should add results when page is not first', () => {
         expect(
-            catalogReducer(state, updateWorkflow({ ...data, page: 1 })).results
-        ).toEqual(expect.arrayContaining([...data.results, ...state.results]));
+            catalogReducer(
+                state,
+                updateWorkflow({ sort, ...{ ...dataPayload, offset: 2 } })
+            ).items
+        ).toEqual(
+            expect.arrayContaining([...dataPayload.items, ...state.items])
+        );
     });
 
     it('should reset state', () => {
index cdea344..dd89c18 100644 (file)
@@ -19,7 +19,7 @@
 import { runSaga } from 'redux-saga';
 import { takeLatest } from 'redux-saga/effects';
 
-import { NAME, DESC, PAGE_SIZE } from 'features/catalog/catalogConstants';
+import { NAME, DESC, LIMIT } from 'features/catalog/catalogConstants';
 import catalogApi from '../catalogApi';
 import { fetchWorkflow, updateWorkflow } from 'features/catalog/catalogActions';
 import catalogSaga, { fetchWorkflowSaga } from 'features/catalog/catalogSagas';
@@ -41,21 +41,29 @@ describe('Catalog Sagas', () => {
         const sort = {
             [NAME]: DESC
         };
-        const page = 0;
+        const offset = 0;
         const data = {
-            total: 2,
-            size: 100,
-            page,
-            results: [
+            paging: {
+                offset,
+                limit: 10,
+                count: 2,
+                hasMore: false,
+                total: 2
+            },
+            items: [
                 {
-                    id: '755eab7752374a2380544065b59b082d',
-                    name: 'Workflow 11',
-                    description: 'description description 11'
+                    id: 'c5b7ca1a0f7944bfa948b85b32c5f314',
+                    name: 'Workflow_2',
+                    description: null,
+                    versionStates: ['DRAFT'],
+                    versions: null
                 },
                 {
-                    id: 'ef8159204dac4c10a85b29ec30b4bd56',
-                    name: 'Workflow 22',
-                    description: 'description description 22'
+                    id: '221336ef3f1645c686bc81899368ac27',
+                    name: 'Workflow_1',
+                    description: null,
+                    versionStates: ['DRAFT'],
+                    versions: null
                 }
             ]
         };
@@ -68,16 +76,17 @@ describe('Catalog Sagas', () => {
                 dispatch: action => dispatched.push(action)
             },
             fetchWorkflowSaga,
-            fetchWorkflow(sort, page)
+            fetchWorkflow(sort, offset)
         ).done;
 
         expect(dispatched).toEqual(
-            expect.arrayContaining([updateWorkflow({ ...data, sort })])
+            expect.arrayContaining([updateWorkflow({ sort, ...data })])
         );
+
         expect(catalogApi.getWorkflows).toBeCalledWith(
             sort,
-            PAGE_SIZE,
-            page + 1
+            LIMIT,
+            offset + LIMIT
         );
     });
 });
index 89f53e4..13e43a0 100644 (file)
 
 import { createActions } from 'redux-actions';
 
-import { NAMESPACE, PAGE_SIZE } from 'features/catalog/catalogConstants';
+import { NAMESPACE, LIMIT } from 'features/catalog/catalogConstants';
 
 export const {
     [NAMESPACE]: { fetchWorkflow, updateWorkflow, resetWorkflow }
 } = createActions({
     [NAMESPACE]: {
-        FETCH_WORKFLOW: (sort, page) => ({
+        FETCH_WORKFLOW: (sort, offset) => ({
             sort,
-            size: PAGE_SIZE,
-            page
+            limit: LIMIT,
+            offset
         }),
         UPDATE_WORKFLOW: undefined,
         RESET_WORKFLOW: undefined
index 79b271c..622c523 100644 (file)
@@ -25,12 +25,12 @@ function baseUrl() {
 }
 
 const Api = {
-    getWorkflows: (sort, size, page) => {
+    getWorkflows: (sort, limit, offset) => {
         const queryString = qs.stringify(
             {
-                sort: Object.keys(sort).map(key => `${key},${sort[key]}`),
-                size,
-                page
+                sort: Object.keys(sort).map(key => `${key}:${sort[key]}`),
+                limit,
+                offset
             },
             {
                 indices: false,
index 3120ad7..8c2fe2a 100644 (file)
@@ -20,7 +20,7 @@ export const NAME = 'name';
 export const ASC = 'asc';
 export const DESC = 'desc';
 
-export const PAGE_SIZE = 1000;
+export const LIMIT = 100;
 
 export const FETCH_WORKFLOW = `${NAMESPACE}/FETCH_WORKFLOW`;
 export const UPDATE_WORKFLOW = `${NAMESPACE}/UPDATE_WORKFLOW`;
index cbd7ab7..78d8e52 100644 (file)
@@ -22,9 +22,10 @@ import {
 } from 'features/catalog/catalogConstants';
 
 export const initialState = {
-    hasMore: true,
-    results: [],
-    total: 0,
+    paging: {
+        hasMore: true,
+        total: 0
+    },
     sort: {
         [NAME]: ASC
     }
@@ -33,23 +34,18 @@ export const initialState = {
 const catalogReducer = (state = initialState, action) => {
     const { type, payload } = action;
 
-    let results;
-
     switch (type) {
         case RESET_WORKFLOW:
             return { ...initialState, sort: state.sort };
 
         case UPDATE_WORKFLOW:
-            results =
-                payload.page === 0
-                    ? [...payload.results]
-                    : [...state.results, ...payload.results];
-
             return {
                 ...state,
                 ...payload,
-                results,
-                hasMore: results.length < payload.total
+                items:
+                    payload.paging.offset === 0
+                        ? [...payload.items]
+                        : [...state.items, ...payload.items]
             };
 
         default:
index 8dcfc61..bf56c61 100644 (file)
@@ -22,17 +22,17 @@ import { fetchWorkflow, updateWorkflow } from 'features/catalog/catalogActions';
 const noOp = () => {};
 
 export function* fetchWorkflowSaga({ payload }) {
-    const { sort, size, page } = payload;
+    const { sort, limit, offset } = payload;
 
     try {
         const data = yield call(
             catalogApi.getWorkflows,
             sort,
-            size,
-            page === undefined ? 0 : page + 1
+            limit,
+            offset === undefined ? 0 : offset + limit
         );
 
-        yield put(updateWorkflow({ ...data, sort }));
+        yield put(updateWorkflow({ sort, ...data }));
     } catch (e) {
         noOp();
     }
index 83c226f..a94e454 100644 (file)
@@ -19,8 +19,8 @@ import PropTypes from 'prop-types';
 
 import { Tile, TileInfo, TileInfoLine } from 'sdc-ui/lib/react';
 
-const Workflows = ({ results, onWorkflowClick }) =>
-    results.map((workflow, index) => (
+const Workflows = ({ items, onWorkflowClick }) =>
+    items.map((workflow, index) => (
         <Tile
             key={`workflow.${index}`}
             dataTestId="wf-catalog-workflow-item"
@@ -36,12 +36,12 @@ const Workflows = ({ results, onWorkflowClick }) =>
     ));
 
 Workflows.propTypes = {
-    results: PropTypes.array,
+    items: PropTypes.array,
     onWorkflowClick: PropTypes.func
 };
 
 Workflows.defaultProps = {
-    results: []
+    items: []
 };
 
 export default Workflows;