2017-07-17 09:25:57 +00:00
|
|
|
module Update exposing (update)
|
|
|
|
|
|
|
|
-- Local modules
|
|
|
|
|
2017-08-16 16:08:34 +00:00
|
|
|
import Models exposing (Model, Route(..), Filter, FilterType(..))
|
2017-07-17 09:25:57 +00:00
|
|
|
import Messages exposing (Msg(..))
|
|
|
|
import Decoders exposing (webSocketActionDecoder, initDataDecoder, eventDecoder)
|
|
|
|
import Routing exposing (parseLocation)
|
2017-07-23 00:51:39 +00:00
|
|
|
import Views.FilterView exposing (parseFilterFromQuery, filterToQuery)
|
2017-07-17 09:25:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- Core modules
|
|
|
|
|
|
|
|
import Json.Decode
|
|
|
|
|
|
|
|
|
2017-07-19 16:30:50 +00:00
|
|
|
-- External modules
|
|
|
|
|
|
|
|
import Navigation
|
|
|
|
|
|
|
|
|
2017-07-17 09:25:57 +00:00
|
|
|
update : Msg -> Model -> ( Model, Cmd Msg )
|
|
|
|
update msg model =
|
|
|
|
case msg of
|
|
|
|
NoOp ->
|
|
|
|
( model, Cmd.none )
|
|
|
|
|
|
|
|
WebSocketPayload str ->
|
|
|
|
let
|
|
|
|
newModel =
|
|
|
|
case Json.Decode.decodeString webSocketActionDecoder str of
|
|
|
|
Ok webSocketAction ->
|
|
|
|
case webSocketAction.action of
|
|
|
|
"init" ->
|
|
|
|
case Json.Decode.decodeString initDataDecoder str of
|
|
|
|
Ok m ->
|
2017-07-23 16:30:27 +00:00
|
|
|
m model.flags model.filter model.location model.route True
|
2017-07-17 09:25:57 +00:00
|
|
|
|
|
|
|
Err error ->
|
|
|
|
model
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
model
|
|
|
|
|
|
|
|
Err error ->
|
|
|
|
model
|
2017-07-23 00:51:39 +00:00
|
|
|
|
|
|
|
( newModel_, _ ) =
|
|
|
|
update (OnLocationChange model.location) newModel
|
2017-07-17 09:25:57 +00:00
|
|
|
in
|
2017-07-23 00:51:39 +00:00
|
|
|
newModel_ ! []
|
2017-07-17 09:25:57 +00:00
|
|
|
|
2017-08-16 16:08:34 +00:00
|
|
|
ToggleFilter filter ->
|
2017-07-17 09:25:57 +00:00
|
|
|
let
|
2017-07-18 11:35:17 +00:00
|
|
|
currentFilter =
|
|
|
|
model.filter
|
|
|
|
|
|
|
|
newFilter =
|
2017-08-16 16:08:34 +00:00
|
|
|
case filter of
|
|
|
|
TypeFilter name slug color lightText ->
|
|
|
|
let
|
|
|
|
eventType =
|
|
|
|
TypeFilter name slug color lightText
|
|
|
|
in
|
|
|
|
{ currentFilter
|
|
|
|
| eventTypes =
|
|
|
|
if List.member eventType model.filter.eventTypes then
|
|
|
|
List.filter (\x -> x /= eventType) model.filter.eventTypes
|
|
|
|
else
|
|
|
|
eventType :: model.filter.eventTypes
|
|
|
|
}
|
|
|
|
|
|
|
|
LocationFilter name slug icon ->
|
|
|
|
let
|
|
|
|
eventLocation =
|
|
|
|
LocationFilter name slug icon
|
|
|
|
in
|
|
|
|
{ currentFilter
|
|
|
|
| eventLocations =
|
|
|
|
if List.member eventLocation model.filter.eventLocations then
|
|
|
|
List.filter (\x -> x /= eventLocation) model.filter.eventLocations
|
|
|
|
else
|
|
|
|
eventLocation :: model.filter.eventLocations
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoFilter name slug ->
|
|
|
|
let
|
|
|
|
videoRecording =
|
|
|
|
VideoFilter name slug
|
|
|
|
in
|
|
|
|
{ currentFilter
|
|
|
|
| videoRecording =
|
|
|
|
if List.member videoRecording model.filter.videoRecording then
|
|
|
|
List.filter (\x -> x /= videoRecording) model.filter.videoRecording
|
|
|
|
else
|
|
|
|
videoRecording :: model.filter.videoRecording
|
|
|
|
}
|
2017-07-23 00:51:39 +00:00
|
|
|
|
2018-05-20 18:08:25 +00:00
|
|
|
TrackFilter name slug ->
|
|
|
|
let
|
|
|
|
eventTrack =
|
|
|
|
TrackFilter name slug
|
|
|
|
in
|
|
|
|
{ currentFilter
|
|
|
|
| eventTracks =
|
|
|
|
if List.member eventTrack model.filter.eventTracks then
|
|
|
|
List.filter (\x -> x /= eventTrack) model.filter.videoRecording
|
|
|
|
else
|
|
|
|
eventTrack :: model.filter.eventTracks
|
|
|
|
}
|
|
|
|
|
2017-07-23 00:51:39 +00:00
|
|
|
query =
|
|
|
|
filterToQuery newFilter
|
|
|
|
|
|
|
|
cmd =
|
|
|
|
Navigation.newUrl query
|
2017-07-18 11:35:17 +00:00
|
|
|
in
|
2017-07-23 00:51:39 +00:00
|
|
|
{ model | filter = newFilter } ! [ cmd ]
|
2017-07-18 11:35:17 +00:00
|
|
|
|
2017-07-17 09:25:57 +00:00
|
|
|
OnLocationChange location ->
|
|
|
|
let
|
|
|
|
newRoute =
|
2017-07-17 14:05:38 +00:00
|
|
|
parseLocation location
|
2017-07-23 00:51:39 +00:00
|
|
|
|
|
|
|
newFilter =
|
|
|
|
case newRoute of
|
|
|
|
OverviewFilteredRoute query ->
|
|
|
|
parseFilterFromQuery query model
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
model.filter
|
2017-07-17 09:25:57 +00:00
|
|
|
in
|
2017-07-23 00:51:39 +00:00
|
|
|
{ model | filter = newFilter, route = newRoute, location = location } ! []
|
2017-07-19 16:30:50 +00:00
|
|
|
|
|
|
|
BackInHistory ->
|
|
|
|
model ! [ Navigation.back 1 ]
|