about summary refs log tree commit diff
path: root/scratch/habit-screens/client/src/State.elm
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-10-10T17·10+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-10-10T17·20+0100
commit19fbdad1c0a93fbc177fdb1ae97354aa3bec4f9f (patch)
tree953599414cdd8511588e0a7dbea776b4e283064c /scratch/habit-screens/client/src/State.elm
parent487232d1aac6273ae911576a595b5c332c4ed0ca (diff)
Support viewing different days
Allow users to browse the habits of the other days of the week.
Diffstat (limited to 'scratch/habit-screens/client/src/State.elm')
-rw-r--r--scratch/habit-screens/client/src/State.elm85
1 files changed, 81 insertions, 4 deletions
diff --git a/scratch/habit-screens/client/src/State.elm b/scratch/habit-screens/client/src/State.elm
index 5bfa5c92802b..3369d843ffbe 100644
--- a/scratch/habit-screens/client/src/State.elm
+++ b/scratch/habit-screens/client/src/State.elm
@@ -12,6 +12,9 @@ type Msg
     | ReceiveDate Date.Date
     | ToggleHabit Int
     | MaybeAdjustWeekday
+    | ViewToday
+    | ViewPrevious
+    | ViewNext
 
 
 type View
@@ -31,19 +34,71 @@ type alias Habit =
 type alias Model =
     { isLoading : Bool
     , view : View
-    , dayOfWeek : Maybe Weekday
+    , today : Maybe Weekday
     , completed : Set Int
+    , visibleDayOfWeek : Maybe Weekday
     }
 
 
+previousDay : Weekday -> Weekday
+previousDay weekday =
+    case weekday of
+        Mon ->
+            Sun
+
+        Tue ->
+            Mon
+
+        Wed ->
+            Tue
+
+        Thu ->
+            Wed
+
+        Fri ->
+            Thu
+
+        Sat ->
+            Fri
+
+        Sun ->
+            Sat
+
+
+nextDay : Weekday -> Weekday
+nextDay weekday =
+    case weekday of
+        Mon ->
+            Tue
+
+        Tue ->
+            Wed
+
+        Wed ->
+            Thu
+
+        Thu ->
+            Fri
+
+        Fri ->
+            Sat
+
+        Sat ->
+            Sun
+
+        Sun ->
+            Mon
+
+
 {-| The initial state for the application.
 -}
 init : ( Model, Cmd Msg )
 init =
     ( { isLoading = False
       , view = Habits
-      , dayOfWeek = Nothing
+      , today = Nothing
       , completed = Set.empty
+      , visibleDayOfWeek = Nothing
       }
     , Date.today |> Task.perform ReceiveDate
     )
@@ -52,7 +107,7 @@ init =
 {-| Now that we have state, we need a function to change the state.
 -}
 update : Msg -> Model -> ( Model, Cmd Msg )
-update msg ({ completed } as model) =
+update msg ({ today, visibleDayOfWeek, completed } as model) =
     case msg of
         DoNothing ->
             ( model, Cmd.none )
@@ -66,7 +121,12 @@ update msg ({ completed } as model) =
             )
 
         ReceiveDate x ->
-            ( { model | dayOfWeek = Just (Date.weekday x) }, Cmd.none )
+            ( { model
+                | today = Just (Date.weekday x)
+                , visibleDayOfWeek = Just (Date.weekday x)
+              }
+            , Cmd.none
+            )
 
         ToggleHabit i ->
             ( { model
@@ -82,3 +142,20 @@ update msg ({ completed } as model) =
 
         MaybeAdjustWeekday ->
             ( model, Date.today |> Task.perform ReceiveDate )
+
+        ViewToday ->
+            ( { model | visibleDayOfWeek = today }, Cmd.none )
+
+        ViewPrevious ->
+            ( { model
+                | visibleDayOfWeek = visibleDayOfWeek |> Maybe.map previousDay
+              }
+            , Cmd.none
+            )
+
+        ViewNext ->
+            ( { model
+                | visibleDayOfWeek = visibleDayOfWeek |> Maybe.map nextDay
+              }
+            , Cmd.none
+            )