Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy up with purs-tidy #91

Merged
merged 2 commits into from
Aug 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Api/Endpoint.purs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type PaginationRep =
)

-- | This record type is useful for endpoints that only need pagination information.
type Pagination = { | PaginationRep }
type Pagination = { | PaginationRep }

-- | When we request articles we can include a few parameters to filter the results. Two of those
-- | parameters control pagination, so we'll re-use our pagination row defined above.
Expand Down Expand Up @@ -100,7 +100,7 @@ endpointCodec = root $ prefix "api" $ sum
{ "Login": "users" / "login" / noArgs
, "User": "user" / noArgs
, "Users": "users" / noArgs
, "Follow": "profiles" / uname segment/ "follow"
, "Follow": "profiles" / uname segment / "follow"
, "Article": "articles" / slug segment
, "Comments": "articles" / slug segment / "comments"
, "Comment": "articles" / slug segment / "comments" / commentId
Expand Down
12 changes: 8 additions & 4 deletions src/Api/Request.purs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,19 @@ loginCodec =
-- | minimal profile.
login :: forall m. MonadAff m => BaseURL -> LoginFields -> m (Either String (Tuple Token Profile))
login baseUrl fields =
let method = Post $ Just $ Codec.encode (CAR.object "User" { user: loginCodec }) { user: fields }
in requestUser baseUrl { endpoint: Login, method }
let
method = Post $ Just $ Codec.encode (CAR.object "User" { user: loginCodec }) { user: fields }
in
requestUser baseUrl { endpoint: Login, method }

-- | This function registers a user (if they don't already exist), returning an auth token and the
-- | user's minimal profile.
register :: forall m. MonadAff m => BaseURL -> RegisterFields -> m (Either String (Tuple Token Profile))
register baseUrl fields =
let method = Post $ Just $ Codec.encode (CAR.object "User" { user: registerCodec }) { user: fields }
in requestUser baseUrl { endpoint: Users, method }
let
method = Post $ Just $ Codec.encode (CAR.object "User" { user: registerCodec }) { user: fields }
in
requestUser baseUrl { endpoint: Users, method }

-- | The login and registration requests share the same underlying implementation, just a different
-- | endpoint. This function can be re-used by both requests.
Expand Down
6 changes: 4 additions & 2 deletions src/AppM.purs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,10 @@ instance manageCommentAppM :: ManageComment AppM where
$ decodeWithUser (\u -> CAR.object "Comments" { comments: CA.array (Comment.codec u) }) mbJson

createComment slug body =
let method = Post $ Just $ Codec.encode (CAR.object "CommentBody" { body: CA.string }) { body }
in void $ mkAuthRequest { endpoint: Comments slug, method }
let
method = Post $ Just $ Codec.encode (CAR.object "CommentBody" { body: CA.string }) { body }
in
void $ mkAuthRequest { endpoint: Comments slug, method }

deleteComment slug id =
void $ mkAuthRequest { endpoint: Comment slug id, method: Delete }
Expand Down
4 changes: 2 additions & 2 deletions src/Capability/LogMessages.purs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ logDebug :: forall m. LogMessages m => Now m => String -> m Unit
logDebug = log Debug

-- | Log a message to convey non-error information
logInfo :: forall m. LogMessages m => Now m =>String -> m Unit
logInfo :: forall m. LogMessages m => Now m => String -> m Unit
logInfo = log Info

-- | Log a message as a warning
logWarn :: forall m. LogMessages m => Now m =>String -> m Unit
logWarn :: forall m. LogMessages m => Now m => String -> m Unit
logWarn = log Warn

-- | Log a message as an error
Expand Down
36 changes: 18 additions & 18 deletions src/Component/HTML/Footer.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ footer :: forall i p. HH.HTML i p
footer =
HH.footer_
[ HH.div
[ css "container" ]
[ HH.a
[ css "logo-font"
, HP.href "/"
[ css "container" ]
[ HH.a
[ css "logo-font"
, HP.href "/"
]
[ HH.text "conduit" ]
, HH.span
[ css "attribution" ]
[ HH.text "An interactive learning project from "
, HH.a
[ HP.href "https://thinkster.io" ]
[ HH.text "Thinkster" ]
, HH.text ". Code & design licensed under MIT. Implemented by "
, HH.a
[ HP.href "https://thomashoneyman.com" ]
[ HH.text "Thomas Honeyman" ]
, HH.text "."
]
]
[ HH.text "conduit" ]
, HH.span
[ css "attribution" ]
[ HH.text "An interactive learning project from "
, HH.a
[ HP.href "https://thinkster.io" ]
[ HH.text "Thinkster" ]
, HH.text ". Code & design licensed under MIT. Implemented by "
, HH.a
[ HP.href "https://thomashoneyman.com" ]
[ HH.text "Thomas Honeyman" ]
, HH.text "."
]
]
]
82 changes: 41 additions & 41 deletions src/Component/HTML/Header.purs
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,44 @@ header currentUser route =
HH.nav
[ css "navbar navbar-light" ]
[ HH.div
[ css "container" ]
[ HH.a
[ css "navbar-brand"
, safeHref Home
]
[ HH.text "conduit" ]
, HH.ul
[ css "nav navbar-nav pull-xs-right" ]
[ navItem Home
[ HH.text "Home" ]
, whenElem (isJust currentUser) \_ ->
navItem Editor
[ HH.i
[ css "ion-compose" ]
[ HH.text " New Post" ]
]
, whenElem (isJust currentUser) \_ ->
navItem Settings
[ HH.i
[ css "ion-gear-a" ]
[ HH.text " Settings" ]
]
, maybeElem currentUser \profile ->
navItem (Profile profile.username)
[ HH.img
[ css "user-pic"
, HP.src $ Avatar.toStringWithDefault profile.image
]
, HH.text $ Username.toString profile.username
]
, whenElem (isNothing currentUser) \_ ->
navItem Login
[ HH.text "Log in" ]
, whenElem (isNothing currentUser) \_ ->
navItem Register
[ HH.text "Sign up" ]
[ css "container" ]
[ HH.a
[ css "navbar-brand"
, safeHref Home
]
[ HH.text "conduit" ]
, HH.ul
[ css "nav navbar-nav pull-xs-right" ]
[ navItem Home
[ HH.text "Home" ]
, whenElem (isJust currentUser) \_ ->
navItem Editor
[ HH.i
[ css "ion-compose" ]
[ HH.text " New Post" ]
]
, whenElem (isJust currentUser) \_ ->
navItem Settings
[ HH.i
[ css "ion-gear-a" ]
[ HH.text " Settings" ]
]
, maybeElem currentUser \profile ->
navItem (Profile profile.username)
[ HH.img
[ css "user-pic"
, HP.src $ Avatar.toStringWithDefault profile.image
]
, HH.text $ Username.toString profile.username
]
, whenElem (isNothing currentUser) \_ ->
navItem Login
[ HH.text "Log in" ]
, whenElem (isNothing currentUser) \_ ->
navItem Register
[ HH.text "Sign up" ]
]
]
]
]

where
Expand All @@ -68,8 +68,8 @@ header currentUser route =
HH.li
[ css "nav-item" ]
[ HH.a
[ css $ "nav-link" <> guard (route == r) " active"
, safeHref r
]
html
[ css $ "nav-link" <> guard (route == r) " active"
, safeHref r
]
html
]
4 changes: 2 additions & 2 deletions src/Component/HTML/Utils.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import Routing.Duplex (print)

-- | I get annoyed writing `class_ $ ClassName "..."` over and over again. This small utility saves
-- | a few characters all over our HTML.
css :: forall r i. String -> HH.IProp ( class :: String | r ) i
css :: forall r i. String -> HH.IProp (class :: String | r) i
css = HP.class_ <<< HH.ClassName

-- | We must provide a `String` to the "href" attribute, but we represent routes with the much
-- | better `Route` type. This utility is a drop-in replacement for `href` that uses `Route`.
safeHref :: forall r i. Route -> HH.IProp ( href :: String | r) i
safeHref :: forall r i. Route -> HH.IProp (href :: String | r) i
safeHref = HP.href <<< append "#" <<< print routeCodec

-- | Sometimes we need to deal with elements which may or may not exist. This function lets us
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Part/FollowButton.purs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ followButton followAct unfollowAct author = case author.relation of
, HE.onClick \_ -> followAct
]
[ HH.i
[ css "ion-plus-round"]
[ css "ion-plus-round" ]
[]
, HH.text $ " Follow " <> Username.toString author.username
]
Expand Down
8 changes: 4 additions & 4 deletions src/Component/TagInput.purs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ component = H.mkComponent
HH.span
[ css "tag-default tag-pill" ]
[ HH.i
[ css "ion-close-round"
, HE.onClick \_ -> RemoveTag tag
]
[ ]
[ css "ion-close-round"
, HE.onClick \_ -> RemoveTag tag
]
[]
, HH.text (unwrap tag)
]
2 changes: 1 addition & 1 deletion src/Data/Log.purs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ reason (Log { reason: r }) = r
-- | relies on our `Now` capability to grab the current time and write it as an additional piece
-- | of metadata. Our application monad will retrieve the current time effectfully, but we'll
-- | write our tests using a hard-coded time so they can be deterministic.
mkLog :: m. Now m => LogReason -> String -> m Log
mkLog :: forall m. Now m => LogReason -> String -> m Log
mkLog logReason inputMessage = do
now <- nowDateTime

Expand Down
14 changes: 7 additions & 7 deletions src/Form/Field.purs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ input sym form props =
HH.fieldset
[ css "form-group" ]
[ HH.input
( append
[ css "form-control form-control-lg"
, HP.value $ F.getInput sym form
, HE.onValueInput $ F.setValidate sym
]
props
)
( append
[ css "form-control form-control-lg"
, HP.value $ F.getInput sym form
, HE.onValueInput $ F.setValidate sym
]
props
)
, maybeElem (F.getError sym form) \err ->
HH.div
[ css "error-messages" ]
Expand Down
15 changes: 8 additions & 7 deletions src/Form/Validation.purs
Original file line number Diff line number Diff line change
Expand Up @@ -88,34 +88,34 @@ errorToString = case _ of
-- | The first validator we'll write verifies that the input is not empty. The same validator can
-- | apply to any input value that is a monoid, as the `Monoid` type class represents 'emptiness'
-- | with the `mempty` value. We'll just check whether the input is the empty value.
required :: form m a. Eq a => Monoid a => Monad m => F.Validation form m FormError a a
required :: forall form m a. Eq a => Monoid a => Monad m => F.Validation form m FormError a a
required = F.hoistFnE_ $ cond (_ /= mempty) Required

-- | This validator ensures that an input string is longer than the provided lower limit.
minLength :: form m. Monad m => Int -> F.Validation form m FormError String String
minLength :: forall form m. Monad m => Int -> F.Validation form m FormError String String
minLength n = F.hoistFnE_ $ cond (\str -> String.length str > n) TooShort

-- | This validator ensures that an input string is shorter than the provided upper limit.
maxLength :: form m. Monad m => Int -> F.Validation form m FormError String String
maxLength :: forall form m. Monad m => Int -> F.Validation form m FormError String String
maxLength n = F.hoistFnE_ $ cond (\str -> String.length str <= n) TooLong

-- | This validator ensures that an input string is a valid email address, using a fairly naive
-- | requirement that it at least includes the `@` symbol. This is our first example of a validator
-- | that returns a different output value than its input value.
emailFormat :: form m. Monad m => F.Validation form m FormError String Email
emailFormat :: forall form m. Monad m => F.Validation form m FormError String Email
emailFormat = F.hoistFnE_ $ map Email <<< cond (String.contains (String.Pattern "@")) InvalidEmail

-- | This validator ensures that an input string is a valid username. Usernames in Conduit use the
-- | smart constructor pattern, so we can't construct a username directly -- we'll need to defer
-- | to the `parse` helper function exported by `Conduit.Data.Username`. Since that function returns
-- | a `Maybe` value, we'll use the `note` helper from `Data.Either` to turn the `Nothing` case into
-- | an error.
usernameFormat :: form m. Monad m => F.Validation form m FormError String Username
usernameFormat :: forall form m. Monad m => F.Validation form m FormError String Username
usernameFormat = F.hoistFnE_ $ note InvalidUsername <<< Username.parse

-- | Our avatar validator follows the same pattern, validating and transforming an input string into
-- | an `Avatar`.
avatarFormat :: form m. Monad m => F.Validation form m FormError String Avatar
avatarFormat :: forall form m. Monad m => F.Validation form m FormError String Avatar
avatarFormat = F.hoistFnE_ $ note InvalidAvatar <<< Avatar.parse

-- Utilities
Expand All @@ -133,7 +133,8 @@ cond f err a = if f a then pure a else Left err
-- |
-- | This helper function lets us transform a set of validation rules so that they only apply when
-- | the input is not empty. It isn't used in this module, but is used in the various forms.
toOptional :: ∀ form m a b
toOptional
:: forall form m a b
. Monoid a
=> Eq a
=> Monad m
Expand Down
Loading