From 9b0adc976101bc4f375b05cc475478187c595714 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Wed, 18 Dec 2024 19:11:03 +0100 Subject: add pagination to rest api --- frontend/app/Schema.hs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'frontend/app/Schema.hs') diff --git a/frontend/app/Schema.hs b/frontend/app/Schema.hs index c10cffe..be04906 100644 --- a/frontend/app/Schema.hs +++ b/frontend/app/Schema.hs @@ -1,3 +1,5 @@ +{-# OPTIONS_GHC -fno-warn-type-defaults #-} + module Schema ( Schema, viewSchema, @@ -6,6 +8,7 @@ module Schema ) where +import ACMS.API.REST.Collection.Paginated (Paginated (..)) import Control.Applicative ((<|>)) import Data.Aeson qualified as A import Data.Aeson.Key qualified as AK @@ -20,6 +23,7 @@ import Miso import Miso.String (MisoString, fromMisoString, intercalate, toMisoString) import Route import Safe +import Text.Printf data Schema = Schema { id :: MisoString, @@ -76,9 +80,9 @@ viewSchema schema = ) <$> (M.toList schema.properties) -schemaTable :: MisoString -> Schema -> [A.Object] -> View action -schemaTable collection schema values = - table_ [] [thead, tbody] +schemaTable :: MisoString -> Schema -> Paginated A.Object -> View action +schemaTable collection schema paginated = + table_ [] [thead, tbody, tfoot] where thead = thead_ [] $ @@ -108,8 +112,20 @@ schemaTable collection schema values = ] | k <- M.keys schema.properties ] - | value <- values + | value <- paginated.data_ ] + tfoot = + let page, lastPage, perPage :: Int + page = 1 + lastPage = ceiling (fromIntegral paginated.count / fromIntegral perPage) + perPage = 15 + in tfoot_ [] $ + [ tr_ [] $ + [ td_ [colspan_ "999"] $ + [ text (toMisoString (printf "Page %d of %d (%d total results)" page lastPage paginated.count :: String)) + ] + ] + ] schemaForm :: Schema -> F.Form A.Object A.Object schemaForm schema = -- cgit v1.2.3