How to structure an Elm project?

The answer to this question can be found in the Beginning Elm bookarrow-up-right. This question will therefore be removed at some time in the future.

circle-info

Do not overuse: Files are allowed to get quite big without needing to split them.

Todo.elm
type Task =
    Task
        { name : String
        , completed : Bool
        }

createTask : String -> Task

completeTask : Task -> Task

type Form a =
    Valid a
    | Invalid a
    | Partial a

validate : (Form a -> Form ()) -> Form a -> Form a

type alias TaskForm =
    { name : String
    , validated : Bool
    }

validateTaskForm : Form TaskForm -> Form ()

type User =
    User
        { name : String
        , lastOnline : Posix
        }

createUser : String -> Posix -> User

type alias UserForm =
    { name : String
    , password : String
    , validated : Bool
    }

validateUserForm : Form UserForm -> Form ()

type alias TodoPageModel =
    { form :
        Form TaskForm
    , todo : Array Task
    , User : User
    }

type alias LoginPageModel =
    Form UserForm

type Model =
    LoginPage LoginPageModel
    | TodoPage TodoPageModel

type LoginPageMsg =
    NameEntered String
    | PasswordEntered String
    | Submited 
    | GotTime Posix

type TodoPageMsg =
    LoggedOut
    | TaskAdded String
    | TaskCompleted Int
    | TaskDeleted

type Msg =
    LoginSpecific LoginPageMsg
    | TodoSpecific TodoPageMsg

init : () -> Model

updateTodoPage : TodoPageModel -> (TodoPageModel, Cmd TodoPageMsg)

updateLoginPage : LoginPageModel -> (LoginPageModel, Cmd LoginPageMsg)

update : Msg -> (Model, Cmd Msg) -> (Model, Cmd Msg)

viewUserForm : Form UserForm -> Html Msg

viewLoginPage : LoginPageModel -> Html Msg

viewTask : Task -> Html Msg

viewTaskForm : Form TaskForm -> Html Msg

viewTodoPage : TodoPageModel -> Html Msg

view : Model -> Html Msg

subscription : Model -> Sub Msgexpos

Question

How should I structure my Elm project?

Answer

Use the following file structure:

  • Main.elm - Contains the main code

  • Data.elm - Contains utility functions for types (like constants)

    • Data/.. - Contains types. Group them logically, for example User and UserForm into Data/User.elm.

  • View.elm - Contains utility functions for views (like view specific constants and very general function.)

    • View/.. - Contains different Views. Sometimes a type has different views. A login page might have a special view for a wrong login.

  • Page.elm - Contains utility functions for pages. For page-transitions its handy to store the different models in here.

    • Page/.. - Contains a Model/View/Update for every page.

  • .. - in the same style you can add your project specific folders like a separate folder for validation.

Further reading

Last updated

Was this helpful?