Hello Elm Discuss.
I'm a java/javascript developer. I'm excited about learning elm. I finally switched from reading and watching to writing something myself and now I am looking for feedback and guidance on what I've done here below:
My types in Log.elm (I don't think the definitions matter for my problem, tell me if you need them too)
type alias Log =
{ author : String
, content : String
}
type LogMsg = ...
viewLog : Log -> Html LogMsg
My model in Task.elm
type alias Task =
{ title : String
, description : String
, logs : List Log
}
type TaskMsg
= Change String
Now I thought I was going to be able to write my view like this
viewTask task =
div []
[ h1 [] [ text task.title ]
, Markdown.toHtml [] task.description
, List.map viewLog task.logs
]
However this has 2 problems:
- the third element in the second parameter of div is a list, so I have a List inside a List
- The inner list is not a List Html TaskMsg but a List Html LogMsg (which was extra confusing when they where both simply called Msg at first)
After some searching and puzzling I was finally able to get the result I wanted with this code :
type TaskMsg
= Change String
| TaskLog LogMsg
viewTask : Task -> Html TaskMsg
viewTask task =
div []
(List.concat
[ [ h1 [] [ text task.title ]
, Markdown.toHtml [] task.description
]
, (List.map transformLogList
(List.map viewLog task.logs)
)
]
)
transformLogList : Html LogMsg -> Html TaskMsg
transformLogList log =
App.map transformLogMsg log
transformLogMsg : LogMsg -> TaskMsg
transformLogMsg logMsg =
TaskLog logMsg
Even though I'm a total beginner to elm, this seems more complicated than it should be.
The problems that I'm thinking of:
- Now I need to write an update method in Task.elm that deals with LogMsg. But I wanted to contain everything that touches LogMsg in Log.elm
- Especially the double usage of List.map combined with App.map will be something I'll have trouble reading again when I come back to it in the future
So what would I need to restructure or rewrite to be able to share this with a fellow elm first-timer.
I have searched the guide, the docs, a number of blog posts and other elm repositories on github and somehow I still miss the answer...
But I don't think I understand yet what it would mean to apply that suggestion to my code.
(Also I'm not familiar with the <| operator yet)