about summary refs log tree commit diff
path: root/scratch/habit-screens/client/src/State.elm
blob: 5bfa5c92802b5d67311a4e19c71291a0320d567d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module State exposing (..)

import Date
import Set exposing (Set)
import Task
import Time exposing (Weekday(..))


type Msg
    = DoNothing
    | SetView View
    | ReceiveDate Date.Date
    | ToggleHabit Int
    | MaybeAdjustWeekday


type View
    = Habits


type HabitType
    = Daily
    | Weekly
    | Yearly


type alias Habit =
    String


type alias Model =
    { isLoading : Bool
    , view : View
    , dayOfWeek : Maybe Weekday
    , completed : Set Int
    }


{-| The initial state for the application.
-}
init : ( Model, Cmd Msg )
init =
    ( { isLoading = False
      , view = Habits
      , dayOfWeek = Nothing
      , completed = Set.empty
      }
    , Date.today |> Task.perform ReceiveDate
    )


{-| Now that we have state, we need a function to change the state.
-}
update : Msg -> Model -> ( Model, Cmd Msg )
update msg ({ completed } as model) =
    case msg of
        DoNothing ->
            ( model, Cmd.none )

        SetView x ->
            ( { model
                | view = x
                , isLoading = True
              }
            , Cmd.none
            )

        ReceiveDate x ->
            ( { model | dayOfWeek = Just (Date.weekday x) }, Cmd.none )

        ToggleHabit i ->
            ( { model
                | completed =
                    if Set.member i completed then
                        Set.remove i completed

                    else
                        Set.insert i completed
              }
            , Cmd.none
            )

        MaybeAdjustWeekday ->
            ( model, Date.today |> Task.perform ReceiveDate )