{"version":3,"file":"paged.cjs","sources":["../../src/api/paged.ts"],"sourcesContent":["// @ts-nocheck\nimport { Component } from \"react\";\nimport h from \"@macrostrat/hyper\";\nimport { Button, ButtonGroup, NonIdealState } from \"@blueprintjs/core\";\nimport { APIResultView } from \"./frontend\";\n\nconst Pagination = (props) => {\n  const { currentPage, nextDisabled, setPage } = props;\n\n  return h(ButtonGroup, [\n    h(\n      Button,\n      {\n        onClick: () => setPage(currentPage - 1),\n        icon: \"arrow-left\",\n        disabled: currentPage <= 0,\n      },\n      \"Previous\",\n    ),\n    h(\n      Button,\n      {\n        onClick: () => setPage(currentPage + 1),\n        rightIcon: \"arrow-right\",\n        disabled: nextDisabled,\n      },\n      \"Next\",\n    ),\n  ]);\n};\n\nclass PagedAPIView extends Component<any, any> {\n  static defaultProps = {\n    count: null,\n    perPage: 20,\n    topPagination: false,\n    bottomPagination: true,\n    extraPagination: null,\n    opts: {}, // Options passed to GET\n    params: {},\n    getTotalCount(response) {\n      const { headers } = response;\n      return parseInt(headers[\"x-total-count\"]);\n    },\n  };\n  constructor(props) {\n    super(props);\n    this.setPage = this.setPage.bind(this);\n    this.params = this.params.bind(this);\n    this.state = { currentPage: 0, count: null };\n  }\n\n  setPage(i) {\n    return () => {\n      return this.setState({ currentPage: i });\n    };\n  }\n\n  renderPagination() {\n    const { perPage } = this.props;\n    const { count } = this.state;\n    let nextDisabled = false;\n    let paginationInfo = null;\n    let currentPage = this.currentPage();\n    const lastPage = this.lastPage();\n\n    if (lastPage != null) {\n      if (currentPage >= lastPage) {\n        currentPage = lastPage;\n        nextDisabled = true;\n      }\n      paginationInfo = h(\"div\", { disabled: true }, [\n        `${currentPage + 1} of ${lastPage + 1} (${count} records)`,\n      ]);\n    }\n\n    return h(\"div.pagination-controls\", [\n      h(Pagination, { currentPage, nextDisabled, setPage: this.setPage }),\n      this.props.extraPagination,\n      paginationInfo,\n    ]);\n  }\n\n  lastPage() {\n    const { count } = this.state;\n    const { perPage } = this.props;\n    if (count == null) {\n      return null;\n    }\n    let pages = Math.floor(count / perPage);\n    if (count % perPage === 0) {\n      pages -= 1;\n    }\n    return pages;\n  }\n\n  currentPage() {\n    let { currentPage } = this.state;\n    const lastPage = this.lastPage();\n    if (lastPage != null && currentPage >= lastPage) {\n      return lastPage;\n    }\n    if (currentPage < 0) {\n      currentPage = 0;\n    }\n    return currentPage;\n  }\n\n  params() {\n    const { params, perPage } = this.props;\n    let { offset, limit, ...otherParams } = params;\n    const currentPage = this.currentPage();\n    if (offset == null) {\n      offset = 0;\n    }\n    offset += currentPage * perPage;\n\n    // This shouldn't happen but it does\n    if (offset < 0) {\n      offset = 0;\n    }\n\n    if (limit == null || limit > perPage) {\n      limit = perPage;\n    }\n\n    return { offset, limit, ...otherParams };\n  }\n\n  render() {\n    let {\n      route,\n      perPage,\n      children,\n      getTotalCount,\n      primaryKey,\n      count,\n      topPagination,\n      bottomPagination,\n      extraPagination,\n      params,\n      opts,\n      ...rest\n    } = this.props;\n\n    params = this.params();\n\n    // Create new onResponse function\n    const { onResponse: __onResponse } = opts;\n    const onResponse = (response) => {\n      count = getTotalCount(response);\n      this.setState({ count });\n      // Run inherited onResponse if it exists\n      if (__onResponse != null) {\n        return __onResponse(response);\n      }\n    };\n\n    // Options for get\n    opts = { ...opts, onResponse };\n\n    const _children = (data) => {\n      if (this.state.count === 0) {\n        return h(NonIdealState, { icon: \"search\", title: \"No results\" });\n      }\n      // @ts-ignore\n      return children(data);\n    };\n\n    return h(\"div.pagination-container\", rest, [\n      topPagination ? this.renderPagination() : undefined,\n      h(APIResultView, { route, params, opts, primaryKey }, _children),\n      bottomPagination ? this.renderPagination() : undefined,\n    ]);\n  }\n}\n\nexport { PagedAPIView, Pagination };\n"],"names":["h","ButtonGroup","Button","Component","NonIdealState","APIResultView"],"mappings":";;;;;;;;AAMA,MAAM,aAAa,CAAC,UAAU;AAC5B,QAAM,EAAE,aAAa,cAAc,QAAA,IAAY;AAE/C,SAAOA,WAAAA,QAAEC,KAAAA,aAA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