Skip to content

Commit

Permalink
Fix AjaxUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
jhbertra committed Jan 21, 2022
1 parent 98aaf9d commit 840a925
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 28 deletions.
1 change: 1 addition & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[ "aff"
, "aff-promise"
, "affjax"
, "argonaut"
, "argonaut-codecs"
, "argonaut-core"
, "arrays"
Expand Down
36 changes: 8 additions & 28 deletions src/AjaxUtils.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,36 @@ module AjaxUtils
) where

import Prelude hiding (div)
import Affjax (printError)
import Affjax.StatusCode (StatusCode(..))

import Bootstrap (alertDanger_, btn, floatRight)
import Data.Argonaut (Json, JsonDecodeError)
import Data.Argonaut.Core (stringify)
import Data.Argonaut.Decode (printJsonDecodeError)
import Halogen (RefLabel(RefLabel))
import Halogen.HTML (ClassName(..), HTML, br_, button, div, p_, text)
import Halogen.HTML.Events (onClick)
import Halogen.HTML.Properties (class_, classes, ref)
import Icons (Icon(..), icon)
import Servant.PureScript (AjaxError, ErrorDescription(..))
import Servant.PureScript (AjaxError, printAjaxError)

data AjaxErrorPaneAction
= CloseErrorPane

ajaxErrorPane :: forall p i. AjaxError -> HTML p i
ajaxErrorPane :: forall p i. AjaxError JsonDecodeError Json -> HTML p i
ajaxErrorPane error =
div
[ class_ ajaxErrorClass
, ref ajaxErrorRefLabel
]
[ alertDanger_
[ showAjaxError error
[ text $ printAjaxError stringify printJsonDecodeError error
, br_
, text "Please try again or contact support for assistance."
]
]

closeableAjaxErrorPane :: forall p. AjaxError -> HTML p AjaxErrorPaneAction
closeableAjaxErrorPane
:: forall p. AjaxError JsonDecodeError Json -> HTML p AjaxErrorPaneAction
closeableAjaxErrorPane error =
div
[ class_ ajaxErrorClass ]
Expand All @@ -44,7 +45,7 @@ closeableAjaxErrorPane error =
, onClick $ const $ CloseErrorPane
]
[ icon Close ]
, p_ [ showAjaxError error ]
, p_ [ text $ printAjaxError stringify printJsonDecodeError error ]
]
]

Expand All @@ -53,24 +54,3 @@ ajaxErrorRefLabel = RefLabel "ajax-error"

ajaxErrorClass :: ClassName
ajaxErrorClass = ClassName "ajax-error"

showAjaxError :: forall p i. AjaxError -> HTML p i
showAjaxError = _.description >>> showErrorDescription

showErrorDescription :: forall p i. ErrorDescription -> HTML p i
showErrorDescription (UnexpectedHTTPStatus { status: StatusCode 404 }) = text
"Data not found."

showErrorDescription (UnexpectedHTTPStatus { body, status }) =
text
$ "Server error "
<> show status
<> ": "
<> stringify body

showErrorDescription (DecodingError err) =
text $ "DecodingError: "
<> printJsonDecodeError err

showErrorDescription (ConnectingError err) = text $ "ConnectionError: " <>
printError err

0 comments on commit 840a925

Please sign in to comment.