Refactor site-fetching into a reusable module
This commit is contained in:
parent
ddf86d901b
commit
716769d24e
3 changed files with 32 additions and 13 deletions
|
|
@ -4,20 +4,14 @@
|
|||
module Widget.Card ( hCard ) where
|
||||
|
||||
import Import
|
||||
import Util ( compileMustache )
|
||||
|
||||
import qualified Site
|
||||
|
||||
import Data.Maybe (fromJust)
|
||||
import Text.Mustache ( Template, substitute )
|
||||
import qualified Data.Map as M
|
||||
import qualified Data.Text as T
|
||||
|
||||
entityToTuple :: Entity t -> (Key t, t)
|
||||
entityToTuple (Entity key value) = (key, value)
|
||||
|
||||
compileTemplates :: M.Map (Key Site) Site -> M.Map (Key Site) (Site, Template)
|
||||
compileTemplates = fmap $ \site -> (site, compile site)
|
||||
where compile site = T.unpack (siteIcon site) `compileMustache` siteTemplate site
|
||||
|
||||
arrangeProfiles :: [Profile] -> M.Map (Key Site) (Site, Template) -> [((Site, Template), Profile)]
|
||||
arrangeProfiles profiles sites = sortBy icon $ zip profileSites profiles
|
||||
where findSite = fromJust . flip M.lookup sites . profileSiteId
|
||||
|
|
@ -33,9 +27,9 @@ routeFromPgp PgpKey { pgpKeyFingerprint = f } = staticR ["pgp", T.takeEnd 8 f ++
|
|||
hCard :: Entity User -> Widget
|
||||
hCard (Entity userId user) = do
|
||||
mcurrentRoute <- getCurrentRoute
|
||||
userProfiles <- handlerToWidget . runDB $ do
|
||||
profiles <- map entityVal <$> selectList [ProfileUserId ==. userId] []
|
||||
sites <- M.fromList . map entityToTuple <$> selectList [SiteId <-. (profileSiteId <$> profiles)] []
|
||||
return . arrangeProfiles profiles . compileTemplates $ sites
|
||||
userProfiles <- handlerToWidget $ do
|
||||
profiles <- runDB $ map entityVal <$> selectList [ProfileUserId ==. userId] []
|
||||
sites <- Site.fetch $ profileSiteId <$> profiles
|
||||
return . arrangeProfiles profiles $ sites
|
||||
pgpKeys <- handlerToWidget . runDB $ map entityVal <$> selectList [PgpKeyUserId ==. userId] []
|
||||
$(widgetFile "mf2/h-card")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue