Commit 9512bd8f authored by Cédric F.'s avatar Cédric F.

Allow to edit events

parent ac1884c0
......@@ -16,8 +16,8 @@ type alias Model =
, lifeExpectancyString : String
, hideUnproductiveYears : Bool
, events : List Event
, newEventOpen : Bool
, newEvent : Event
, eventFormOpen : Int
, eventForm : EventForm
, modalOpen : Bool
, now : Time
}
......@@ -41,6 +41,14 @@ type alias Event =
}
type alias EventForm =
{ from : String
, to : String
, color : String
, label : String
}
initialModel : Model
initialModel =
{ birthDate = DateExtra.unsafeFromString "1988-07-24"
......@@ -51,8 +59,8 @@ initialModel =
, lifeExpectancyString = "80"
, hideUnproductiveYears = False
, events = []
, newEventOpen = False
, newEvent = initialEvent
, eventFormOpen = -1
, eventForm = EventForm "" "" "" ""
, modalOpen = False
, now = 0
}
......@@ -75,22 +83,13 @@ mergeJsonModel jsonModel =
mergeModel : Model -> Model -> Model
mergeModel newModel baseModel =
{ newModel
| newEventOpen = baseModel.newEventOpen
, newEvent = baseModel.newEvent
| eventFormOpen = baseModel.eventFormOpen
, eventForm = baseModel.eventForm
, modalOpen = baseModel.modalOpen
, now = baseModel.now
}
initialEvent : Event
initialEvent =
{ from = Date.fromTime 0
, to = Date.fromTime 0
, color = Color.black
, label = ""
}
fallbackColor : Color
fallbackColor =
Color.red
......@@ -109,9 +108,9 @@ type Msg
| NewDateOfBirth String
| NewLifeExpectancy String
| HideUnproductiveYears Bool
| ToggleNewEvent
| UpdateNewEvent NewEventField String
| SaveNewEvent
| ShowEventForm Int
| UpdateEvent NewEventField String
| SaveEvent
| NewConfig String
| ToggleModal
......
......@@ -84,39 +84,91 @@ update msg model =
in
( newModel, updateUrl newModel )
ToggleNewEvent ->
( { model | newEventOpen = not model.newEventOpen }, Cmd.none )
UpdateNewEvent field value ->
ShowEventForm id ->
let
newId =
if model.eventFormOpen == id then
-1
else
id
event =
model.newEvent
List.take newId model.events |> List.reverse |> List.head
eventForm =
case event of
Nothing ->
{ from = DateExtra.toISOString <| Date.fromTime model.now
, to = DateExtra.toISOString <| Date.fromTime model.now
, color = Color.Convert.colorToHex fallbackColor
, label = ""
}
Just event ->
{ from = DateExtra.toISOString event.from
, to = DateExtra.toISOString event.to
, color = Color.Convert.colorToHex event.color
, label = event.label
}
in
( { model | eventFormOpen = newId, eventForm = eventForm }, Cmd.none )
UpdateEvent field value ->
let
form =
model.eventForm
-- TODO: this is kinda gross, is there a better way?
newEvent =
newForm =
case field of
EventFrom ->
{ event | from = DateExtra.fromStringWithFallback value event.from }
{ form | from = value }
EventTo ->
{ event | to = DateExtra.fromStringWithFallback value event.to }
{ form | to = value }
EventColor ->
{ event | color = Color.Convert.hexToColor value |> Maybe.withDefault fallbackColor }
{ form | color = value }
EventLabel ->
{ event | label = value }
{ form | label = value }
in
( { model | newEvent = newEvent }, Cmd.none )
( { model | eventForm = newForm }, Cmd.none )
SaveNewEvent ->
SaveEvent ->
let
eventForm =
model.eventForm
newEvent =
{ from =
DateExtra.fromStringWithFallback eventForm.from (Date.fromTime 0)
, to =
DateExtra.fromStringWithFallback eventForm.to (Date.fromTime 0)
, color =
Color.Convert.hexToColor eventForm.color |> Maybe.withDefault fallbackColor
, label = eventForm.label
}
newEvents =
if model.eventFormOpen == 0 then
newEvent :: model.events
else
List.indexedMap
(\index ->
\event ->
if index + 1 == model.eventFormOpen then
newEvent
else
event
)
model.events
events =
(model.newEvent :: model.events)
|> List.sortBy (\event -> DateExtra.toISOString event.from)
List.sortBy (\event -> DateExtra.toISOString event.from) newEvents
newModel =
{ model | events = events, newEventOpen = False }
{ model | events = events, eventFormOpen = -1 }
in
( newModel, updateUrl newModel )
......
......@@ -156,102 +156,117 @@ config model =
newEvent : Model -> List (Html Msg)
newEvent model =
[ li [ class "list-group-item" ]
[ h5 [ class "mb-0" ]
[ span [] [ text "Events" ]
, a
[ href "javascript:void(0)"
, onClick (ShowEventForm 0)
, class "float-xs-right"
]
[ text "Add event" ]
]
]
, eventForm model 0
]
eventForm : Model -> Int -> Html Msg
eventForm model index =
let
event =
model.eventForm
visibility =
if model.newEventOpen then
if model.eventFormOpen == index then
""
else
" hidden-xs-up"
inputs =
[ div [ class "row form-group" ]
[ div [ class "col-xs-6" ]
[ div [ class "input-group" ]
[ span [ class "input-group-addon" ] [ text "From" ]
, input
(List.append dateInputAttributes
[ class "form-control"
, onInput (UpdateNewEvent EventFrom)
]
)
[]
in
li [ class <| "list-group-item" ++ visibility ]
[ Html.form [ onSubmit SaveEvent ]
[ div [ class "row form-group" ]
[ div [ class "col-xs-6" ]
[ div [ class "input-group" ]
[ span [ class "input-group-addon" ] [ text "From" ]
, input
(List.append dateInputAttributes
[ class "form-control"
, value event.from
, onInput (UpdateEvent EventFrom)
]
)
[]
]
]
, div [ class "col-xs-6" ]
[ div [ class "input-group" ]
[ span [ class "input-group-addon" ] [ text "To" ]
, input
(List.append dateInputAttributes
[ class "form-control"
, value event.to
, onInput (UpdateEvent EventTo)
]
)
[]
]
]
]
, div [ class "col-xs-6" ]
[ div [ class "input-group" ]
[ span [ class "input-group-addon" ] [ text "To" ]
, input
(List.append dateInputAttributes
, div [ class "row form-group" ]
[ div [ class "col-xs-6" ]
[ div [ class "input-group" ]
[ span [ class "input-group-addon" ] [ text "Label" ]
, input
[ class "form-control"
, onInput (UpdateNewEvent EventTo)
, type_ "text"
, required True
, value event.label
, onInput (UpdateEvent EventLabel)
]
)
[]
[]
]
]
]
]
, div [ class "row form-group" ]
[ div [ class "col-xs-6" ]
[ div [ class "input-group" ]
[ span [ class "input-group-addon" ] [ text "Label" ]
, input
[ class "form-control"
, type_ "text"
, required True
, onInput (UpdateNewEvent EventLabel)
, div [ class "col-xs-3" ]
[ div [ class "input-group" ]
[ span [ class "input-group-addon" ] [ text "Color" ]
, input
[ class "form-control"
, type_ "color"
, required True
, value event.color
, style [ ( "height", "2.5rem" ) ]
, onInput (UpdateEvent EventColor)
]
[]
]
[]
]
]
, div [ class "col-xs-3" ]
[ div [ class "input-group" ]
[ span [ class "input-group-addon" ] [ text "Color" ]
, input
[ class "form-control"
, type_ "color"
, required True
, style [ ( "height", "2.5rem" ) ]
, onInput (UpdateNewEvent EventColor)
, div [ class "col-xs-3 text-xs-right" ]
[ input
[ class "btn btn-primary"
, type_ "submit"
, value
(if index == 0 then
"Add"
else
"Save"
)
]
[]
]
]
, div [ class "col-xs-3 text-xs-right" ]
[ input
[ class "btn btn-primary"
, type_ "submit"
, value "Add"
]
[]
]
]
]
in
[ li [ class "list-group-item" ]
[ h5 [ class "mb-0" ]
[ span [] [ text "Events" ]
, a
[ href "javascript:void(0)"
, onClick ToggleNewEvent
, class "float-xs-right"
]
[ text "Add event" ]
]
]
, li [ class <| "list-group-item" ++ visibility ]
[ Html.form [ onSubmit SaveNewEvent ] inputs
]
]
events : Model -> List (Html Msg)
events model =
let
makeEvent event =
li [ class "list-group-item" ]
makeEvent event index =
[ li [ class "list-group-item" ]
[ div
[ class "row" ]
[ div [ class "col-xs-5" ]
[ div [ class "col-xs-3" ]
[ div
[ class "float-xs-left"
, style
......@@ -264,16 +279,26 @@ events model =
[]
, span [] [ text event.label ]
]
, div [ class "col-xs-5 text-muted" ]
, div [ class "col-xs-7 text-xs-right text-muted" ]
[ text <|
DateExtra.toISOString event.from
++ " to "
++ DateExtra.toISOString event.to
++ " ("
++ eventPercentage event model
++ "%)"
]
, div [ class "col-xs-2 text-xs-right" ]
[ a
[ href "javascript:void(0)"
, onClick (ShowEventForm index)
]
[ text "edit" ]
]
, div [ class "col-xs-2 text-xs-right text-muted" ]
[ text <| eventPercentage event model ++ "%" ]
]
]
, eventForm model index
]
eventPercentage event model =
100
......@@ -282,13 +307,19 @@ events model =
|> roundToPadded 1
list =
if List.isEmpty model.events then
if List.isEmpty events then
[ li [ class "list-group-item" ]
[ div [ class "text-muted text-xs-center" ] [ text "no events yet" ]
]
]
else
List.map makeEvent model.events
List.concat <|
List.indexedMap
(\index -> \event -> makeEvent event (index + 1))
events
events =
model.events
in
[ div [ class "card" ]
[ ul [ class "list-group list-group-flush" ] <|
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment