Search code examples
haskellyesodpersist

How can I get a String from a PersistObjectId in yesod?


From a higher level what I am trying to do is make a post via javascript that includes the ID to an object.

I am passing the 'PlayerId' to a widget that will create the jquery that will post.

  tableClickHandlerWidget :: String -> TableId -> PlayerId -> Maybe Int ->  Widget
  tableClickHandlerWidget elemId tid playerId seatId = do
    let seatNumber = if seatId == Nothing
                      then "null"
                      else show  $ fromJust seatId
    let pid = fromPersistToJS $ unKey playerId
    toWidget[julius|
        $(function() {
          $('#{show elemId}').click.post(
            '@{GamingSessionsR}',
            { player: '#{pid}', table: '#{show tid}', seat:'#{seatNumber}' },
            );
        });
        |]
    toWidget[hamlet|something<br/>|]


  fromPersistToJS :: PersistValue -> String
  fromPersistToJS p = do
   let (a) = fromPersistValue p
   case a of
      Left  l -> T.unpack l
      Right  r -> r

What i am getting back from fromPersistToJS is always an exception that object id cannot be made into a string.

But the URLs are essentially strings and have IDs inside them; so I am sure this must bepossible.

https://github.com/gdoteof/exodus/ is the whole repo and I am making this post at

https://github.com/gdoteof/exodus/commit/4daa0a25a9f44c69cbdc5c0bb4e8aa4f6433de45


Solution

  • the answer was very easy.

    let playerIdAsText = toPathPiece playerId