Skip to content

Commit

Permalink
use Set and not List to model tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Perone committed Apr 6, 2022
1 parent 2747c70 commit 5d43fbf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
22 changes: 12 additions & 10 deletions elm/src/Logged.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import LoggedModel exposing (..)
import Style exposing (..)
import Tags exposing (..)

-- elm/core
import Set exposing (..)

-- elm/http
import Http exposing (..)

Expand Down Expand Up @@ -65,7 +68,7 @@ viewTag tag = Element.el
[ normalPadding
, normalSpacing
]
( Element.text tag.name )
( Element.text tag )

view : Model -> Element Msg
view model = Component.mainRow
Expand All @@ -87,7 +90,7 @@ view model = Component.mainRow
, width = fill
, view = \content -> Element.el
tableRowStyle
( row [] ( List.map viewTag content.tags ) ) }
( row [] ( List.map viewTag ( toList content.tags ) ) ) }
]
}
]
Expand All @@ -106,17 +109,17 @@ view model = Component.mainRow

-- HTTP

retrieveUrl : List Tag -> String
retrieveUrl : Set Tag -> String
retrieveUrl tags = Url.Builder.custom
( CrossOrigin "http://localhost:8080" )
[ "get-contents" ]
( List.map ( \tag -> Url.Builder.string "tag" tag.name ) tags )
( List.map ( \tag -> Url.Builder.string "tag" tag ) ( toList tags ) )
Nothing

authorization : Token -> Header
authorization token = Http.header "Authorization" ( String.append "Bearer " token )

retrieveContents : Token -> List Tag -> Cmd Msg
retrieveContents : Token -> Set Tag -> Cmd Msg
retrieveContents token tags = Http.request
{ method = "GET"
, headers = [ authorization token ]
Expand All @@ -133,13 +136,12 @@ handleContentsResponse result = case result of
Err error -> FetchFailed error

tagDecoder : Decoder Tag
tagDecoder = Json.Decode.map Tag
( field "name" Json.Decode.string )
tagDecoder = field "name" Json.Decode.string

contentDecoder : Decoder Content
contentDecoder = map2 Content
( field "message" Json.Decode.string )
( field "tags" ( Json.Decode.list ( Json.Decode.map Tag Json.Decode.string )))
( field "tags" ( Json.Decode.map fromList ( Json.Decode.list ( Json.Decode.string ) ) ) )

wrappedContentDecoder : Decoder Content
wrappedContentDecoder = Json.Decode.map identity
Expand All @@ -162,10 +164,10 @@ handleNewContentResponse content result = case result of
Ok () -> SubmitSuccessful content

tagEncoder : Tag -> Json.Encode.Value
tagEncoder tag = Json.Encode.string tag.name
tagEncoder tag = Json.Encode.string tag

contentEncoder : Content -> Json.Encode.Value
contentEncoder content = Json.Encode.object
[ ( "message", Json.Encode.string content.message )
, ( "tags", Json.Encode.list tagEncoder content.tags)
, ( "tags", Json.Encode.list tagEncoder ( toList content.tags ) )
]
9 changes: 5 additions & 4 deletions elm/src/LoggedModel.elm
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module LoggedModel exposing (..)

type alias Tag =
{ name : String
}
-- elm/core
import Set exposing (..)

type alias Tag = String

type alias Content =
{ message : String
, tags : List Tag
, tags : Set Tag
}
3 changes: 2 additions & 1 deletion elm/src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Style exposing (..)
import Browser exposing (..)

-- elm/core
import Set exposing (..)
import Tuple exposing (mapBoth)

-- mdgriffith/elm-ui
Expand Down Expand Up @@ -42,7 +43,7 @@ type Msg

updateAnonymous : Anonymous.Msg -> Anonymous.Model -> ( Model, Cmd Msg )
updateAnonymous msg anonymousModel = case msg of
Login ( Succeeded token ) -> ( LoggedIn ( Logged.init token ), Cmd.map LoggedInMsg ( retrieveContents token [] ) )
Login ( Succeeded token ) -> ( LoggedIn ( Logged.init token ), Cmd.map LoggedInMsg ( retrieveContents token empty ) )
_ -> Tuple.mapBoth Anonymous ( Cmd.map AnonymousMsg ) (Anonymous.update msg anonymousModel)

update : Msg -> Model -> ( Model, Cmd Msg )
Expand Down
28 changes: 10 additions & 18 deletions elm/src/Tags.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import Component exposing (..)
import LoggedModel exposing (..)
import Style exposing (..)

-- elm/core
import Set exposing (..)

-- mdgriffith/elm-ui
import Element exposing (..)
import Element.Background exposing (..)
Expand All @@ -16,11 +19,11 @@ import Element.Input exposing (labelAbove)

type alias Model =
{ newTag : String
, tags : List Tag
, tags : Set Tag
}

init : Model
init = Model "" []
init = Model "" empty

-- UPDATE

Expand All @@ -29,31 +32,20 @@ type Msg
| Submit
| Remove String

update : ( List Tag -> Cmd msg ) -> Msg -> Model -> ( Model, Cmd msg )
update : ( Set Tag -> Cmd msg ) -> Msg -> Model -> ( Model, Cmd msg )
update onSubmit msg model = case msg of
NewTag newTag -> ( { model | newTag = newTag }, Cmd.none )
Submit ->
let
tags = ( Tag model.newTag ) :: model.tags
tags = insert model.newTag model.tags
in
( { model | newTag = "", tags = tags }, onSubmit tags )
Remove id ->
let
tags = removeTag id model.tags
tags = remove id model.tags
in
( { model | tags = tags }, onSubmit tags )

removeTag : String -> List Tag -> List Tag
removeTag id tags = remove ( Tag id ) tags

remove : a -> List a -> List a
remove value list = case list of
[] -> []
( head :: tail ) ->
if head == value
then remove value tail
else head :: remove value tail

-- VIEW

removable : String -> Element Msg -> Element Msg
Expand All @@ -66,7 +58,7 @@ removable id element = row
]

viewRemovableTag : ( Tag -> Element Msg ) -> Tag -> Element Msg
viewRemovableTag viewTag tag = removable tag.name ( viewTag tag )
viewRemovableTag viewTag tag = removable tag ( viewTag tag )

view : ( Tag -> Element Msg ) -> String -> String -> Model -> Element Msg
view viewTag label submitText model = column
Expand All @@ -80,5 +72,5 @@ view viewTag label submitText model = column
, label = labelAbove [] ( Element.text label )
} )
, Component.button Submit submitText
, Element.row [ normalSpacing ] ( List.map (viewRemovableTag viewTag) model.tags )
, Element.row [ normalSpacing ] ( List.map (viewRemovableTag viewTag) ( toList model.tags ) )
]

0 comments on commit 5d43fbf

Please sign in to comment.