From 3ec84cb0512423375c246a1a1ecd04e9898b35d0 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Tue, 14 Aug 2018 03:46:22 -0400 Subject: [PATCH] Solution for advanced/part7 --- advanced/part7/src/Author.elm | 11 ++++------- advanced/part7/src/Timestamp.elm | 17 +++++++++-------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/advanced/part7/src/Author.elm b/advanced/part7/src/Author.elm index 5efcae8..68c1753 100644 --- a/advanced/part7/src/Author.elm +++ b/advanced/part7/src/Author.elm @@ -206,17 +206,14 @@ toggleFollowButton txt extraClasses msgWhenClicked uname = decoder : Maybe Cred -> Decoder Author decoder maybeCred = - {- 👉 TODO: Use this `Profile` and `Username` to decode an `Author`! - - 💡 HINT: `decoderHelp` will help here, but slightly altering its type may make things easier... - -} - Decode.succeed "..." + Decode.succeed Tuple.pair |> custom Profile.decoder |> required "username" Username.decoder + |> Decode.andThen (decoderHelp maybeCred) -decoderHelp : Maybe Cred -> Profile -> Username -> Decoder Author -decoderHelp maybeCred prof uname = +decoderHelp : Maybe Cred -> ( Profile, Username ) -> Decoder Author +decoderHelp maybeCred ( prof, uname ) = case maybeCred of Nothing -> -- If you're logged out, you can't be following anyone! diff --git a/advanced/part7/src/Timestamp.elm b/advanced/part7/src/Timestamp.elm index aa2eb7c..e5cff49 100644 --- a/advanced/part7/src/Timestamp.elm +++ b/advanced/part7/src/Timestamp.elm @@ -24,17 +24,18 @@ view timeZone timestamp = -} iso8601Decoder : Decoder Time.Posix iso8601Decoder = - {- 👉 TODO: Use the following function to decode this Time.Posix value: + Decode.string + |> Decode.andThen decoderHelp - Iso8601.toTime : String -> Result (List DeadEnd) Time.Posix +decoderHelp : String -> Decoder Time.Posix +decoderHelp str = + case Iso8601.toTime str of + Ok time -> + Decode.succeed time - - ❕ NOTE: You can disregard the (List DeadEnd) here. No need to use it to complete this exercise! - - 💡 HINT: Decode.andThen will be useful here. - -} - "..." + Err _ -> + Decode.fail ("Invalid ISO-8601 timestamp: " ++ str)