about summary refs log tree commit diff
path: root/scratch
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-10-11T14·39+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-10-11T14·39+0100
commitabf1875934924c4a5146c5b36cfaf9429b974cbe (patch)
tree48c96eb0e6b30064c7a65ad261b025c33054e7aa /scratch
parent79cf42abd529e2b4db5577f835a206e400e20e24 (diff)
Support Msg to clear all completed tasks
Add a simple button to clear all completed tasks.
Diffstat (limited to 'scratch')
-rw-r--r--scratch/habit-screens/client/src/Habits.elm46
-rw-r--r--scratch/habit-screens/client/src/State.elm4
2 files changed, 45 insertions, 5 deletions
diff --git a/scratch/habit-screens/client/src/Habits.elm b/scratch/habit-screens/client/src/Habits.elm
index 92f1455dac38..7ec6c051b3c1 100644
--- a/scratch/habit-screens/client/src/Habits.elm
+++ b/scratch/habit-screens/client/src/Habits.elm
@@ -226,6 +226,10 @@ render { today, visibleDayOfWeek, completed } =
             p [] [ text "Unable to display habits because we do not know what day of the week it is." ]
 
         Just weekday ->
+            let
+                habits =
+                    habitsFor weekday
+            in
             div
                 [ Utils.class
                     [ Always "container mx-auto py-6 px-6"
@@ -265,15 +269,14 @@ render { today, visibleDayOfWeek, completed } =
                     p [ class "text-center" ]
                         [ let
                             t =
-                                timeRemaining completed (habitsFor weekday)
+                                timeRemaining completed habits
                           in
                           if t == 0 then
                             text "Nothing to do!"
 
                           else
                             text
-                                ((weekday
-                                    |> habitsFor
+                                ((habits
                                     |> timeRemaining completed
                                     |> String.fromInt
                                  )
@@ -283,9 +286,42 @@ render { today, visibleDayOfWeek, completed } =
 
                   else
                     text ""
+                , if today == visibleDayOfWeek then
+                    div []
+                        [ UI.button
+                            [ onClick
+                                (if Set.size completed == 0 then
+                                    State.DoNothing
+
+                                 else
+                                    State.ClearAll
+                                )
+                            , Utils.class
+                                [ Always "ml-10"
+                                , If (Set.size completed == 0)
+                                    "text-gray-500 cursor-not-allowed"
+                                    "text-red-500 underline cursor-pointer"
+                                ]
+                            ]
+                            [ let
+                                numCompleted =
+                                    habits
+                                        |> List.indexedMap (\i _ -> i)
+                                        |> List.filter (\i -> Set.member i completed)
+                                        |> List.length
+                              in
+                              if numCompleted == 0 then
+                                text "Clear"
+
+                              else
+                                text ("Clear (" ++ String.fromInt numCompleted ++ ")")
+                            ]
+                        ]
+
+                  else
+                    text ""
                 , ul []
-                    (weekday
-                        |> habitsFor
+                    (habits
                         |> List.indexedMap
                             (\i { label, minutesDuration } ->
                                 let
diff --git a/scratch/habit-screens/client/src/State.elm b/scratch/habit-screens/client/src/State.elm
index c5cdadf3afa0..5fde06c8000c 100644
--- a/scratch/habit-screens/client/src/State.elm
+++ b/scratch/habit-screens/client/src/State.elm
@@ -15,6 +15,7 @@ type Msg
     | ViewToday
     | ViewPrevious
     | ViewNext
+    | ClearAll
 
 
 type View
@@ -165,3 +166,6 @@ update msg ({ today, visibleDayOfWeek, completed } as model) =
               }
             , Cmd.none
             )
+
+        ClearAll ->
+            ( { model | completed = Set.empty }, Cmd.none )