You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by GitBox <gi...@apache.org> on 2022/08/25 02:09:23 UTC

[GitHub] [incubator-devlake] likyh commented on a diff in pull request #2780: feat: create paginator hook and paginate blueprints grid

likyh commented on code in PR #2780:
URL: https://github.com/apache/incubator-devlake/pull/2780#discussion_r954437327


##########
config-ui/src/hooks/usePaginator.jsx:
##########
@@ -0,0 +1,217 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+import React, { useState, useEffect, useRef, useCallback, useMemo } from 'react'
+import {
+  Popover,
+  Menu,
+  MenuItem,
+  Button
+} from '@blueprintjs/core'
+
+import { integrationsData } from '@/data/integrations'
+
+const PagingOptionsMenu = (props) => {
+  const { pageOptions = [10, 25, 50, 75, 100], perPage = 10, setPerPage = (page) => undefined } = props
+  return (
+    <Menu>
+      {pageOptions && pageOptions.map(pageOption => (
+        <MenuItem
+          key={pageOption}
+          active={perPage === pageOption}
+          icon='key-option'
+          text={`${pageOption} Records`}
+          onClick={() => setPerPage(pageOption)}
+        />))}
+    </Menu>
+  )
+}
+
+const Controls = (props) => {
+  const {
+    enabled = true,
+    pagingOptionsMenu,
+    currentPage,
+    perPage = 10,
+    maxPage,
+    onPrevPage = () => {},
+    onNextPage = () => {},
+    isLoading = false,
+  } = props
+
+  return (
+    <div
+      className='pagination-controls'
+      style={{ display: 'flex', whiteSpace: 'nowrap' }}
+    >
+      <Popover placement='bottom'>
+        <Button
+          className='btn-select-page-size'
+          style={{ whiteSpace: 'nowrap' }}
+          icon='numbered-list'
+          text={`Rows: ${perPage}`}
+          disabled={isLoading}
+          outlined
+          minimal
+        />
+        <>{pagingOptionsMenu}</>
+      </Popover>
+      <Button
+        onClick={onPrevPage}
+        className='pagination-btn btn-prev-page'
+        icon='step-backward'
+        small
+        text='PREV'
+        style={{ marginLeft: '5px', marginRight: '5px', whiteSpace: 'nowrap' }}
+        disabled={currentPage === 1 || isLoading}
+      />
+      <Button
+        style={{ whiteSpace: 'nowrap' }}
+        disabled={currentPage === maxPage || isLoading}
+        onClick={onNextPage}
+        className='pagination-btn btn-next-page'
+        rightIcon='step-forward'
+        text='NEXT'
+        small
+      />
+    </div>
+  )
+}
+
+function usePaginator (initialLoadingState = false) {
+  // const [integrations, setIntegrations] = useState(integrationsData)
+
+  const [_data, _setData] = useState([])
+
+  // filter related
+  const [filterParams, setFilterParams] = useState({})
+  const [filterFunc, setFilterFunc] = useState(() => (params, item) => true)
+  const filteredData = useMemo(() => {
+    console.log('>> SET FILTER DATA BY', filterParams, _data)
+    const filteredData = []
+    for (const item of _data) {
+      console.log(item)
+      if (filterFunc(filterParams, item)) {
+        filteredData.push(item)
+      }
+    }
+    return filteredData
+  }, [_data, filterParams, filterFunc])
+
+  // page related
+  const [pagedData, setPagedData] = useState([])
+  const [pageOptions, setPageOptions] = useState([5, 25, 50, 75, 100])
+  const [currentPage, setCurrentPage] = useState(1)
+  const [perPage, setPerPage] = useState(pageOptions[1])
+  const [maxPage, setMaxPage] = useState(0)

Review Comment:
   I kept it because maybe we need to set it by backend response. But we can delete it now.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@devlake.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org