Differentiating between data is too cumbersome, so now we just load everything on start. Think about optimizing later!

This commit is contained in:
Vidir Valberg Gudmundsson 2017-07-18 11:26:56 +02:00
parent d2691e1802
commit 4279653fcb
6 changed files with 17 additions and 79 deletions

View file

@ -9,12 +9,12 @@ import Models exposing (Day, Speaker, Event, EventInstance, EventLocation, Event
import Json.Decode exposing (int, string, float, list, bool, dict, Decoder)
import Json.Decode.Pipeline exposing (decode, required, optional, hardcoded)
import Date exposing (Month(..))
import Date exposing (Date, Month(..))
-- External modules
import Date.Extra as Date
import Date.Extra as DateExtra
-- DECODERS
@ -54,15 +54,16 @@ eventDecoder =
|> required "speakers" (list speakerDecoder)
dateDecoder : Decoder Date
dateDecoder =
let
unpacked x =
case Date.fromIsoString x of
case DateExtra.fromIsoString x of
Just value ->
value
Nothing ->
Date.fromParts 1970 Jan 1 0 0 0 0
DateExtra.fromParts 1970 Jan 1 0 0 0 0
in
Json.Decode.map unpacked string
@ -108,7 +109,7 @@ initDataDecoder : Decoder (Flags -> Maybe Day -> Filter -> Route -> Model)
initDataDecoder =
decode Model
|> required "days" (list dayDecoder)
|> required "events" (list eventDecoder)
|> required "event_instances" (list eventInstanceDecoder)
|> required "event_locations" (list eventLocationDecoder)
|> required "event_types" (list eventTypeDecoder)
|> hardcoded []

View file

@ -39,13 +39,8 @@ init flags location =
initModel =
(Model [] [] [] [] [] flags Nothing emptyFilter currentRoute)
-- To ensure we load data on right momens we call update with the
-- OnLocationChange message which is in charge of all that
( model, cmd ) =
update (OnLocationChange location) initModel
in
model ! [ cmd ]
initModel ! [ sendInitMessage flags.camp_slug ]

View file

@ -12,10 +12,10 @@ type Route
type alias Model =
{ days : List Day
, events : List Event
, eventInstances : List EventInstance
, eventLocations : List EventLocation
, eventTypes : List EventType
, events : List Event
, flags : Flags
, activeDay : Maybe Day
, filter : Filter

View file

@ -6,7 +6,6 @@ import Models exposing (Model, Route(OverviewRoute, EventRoute), Filter)
import Messages exposing (Msg(..))
import Decoders exposing (webSocketActionDecoder, initDataDecoder, eventDecoder)
import Routing exposing (parseLocation)
import WebSocketCalls exposing (sendGetEventContent, sendInitMessage)
-- Core modules
@ -29,24 +28,7 @@ update msg model =
"init" ->
case Json.Decode.decodeString initDataDecoder str of
Ok m ->
let
newModel_ =
m model.flags Nothing (Filter [] []) model.route
in
{ model
| days = newModel_.days
, eventInstances = newModel_.eventInstances
, eventLocations = newModel_.eventLocations
, eventTypes = newModel_.eventTypes
}
Err error ->
model
"get_event_content" ->
case Json.Decode.decodeString eventDecoder str of
Ok event ->
{ model | events = event :: model.events }
Err error ->
model
@ -101,26 +83,5 @@ update msg model =
let
newRoute =
parseLocation location
onLoadCmd =
case newRoute of
EventRoute eventSlug ->
case List.head (List.filter (\x -> x.slug == eventSlug) model.events) of
Just event ->
Cmd.none
Nothing ->
sendGetEventContent model.flags.camp_slug (Debug.log "eventSlug" eventSlug)
OverviewRoute ->
case List.head model.days of
Just day ->
Cmd.none
Nothing ->
sendInitMessage model.flags.camp_slug
_ ->
Cmd.none
in
{ model | route = newRoute } ! [ onLoadCmd ]
{ model | route = newRoute } ! []

View file

@ -1,8 +1,7 @@
module WebSocketCalls exposing (scheduleServer, sendInitMessage, sendGetEventContent)
module WebSocketCalls exposing (scheduleServer, sendInitMessage)
-- Internal modules
import Models exposing (EventSlug)
import Messages exposing (Msg)
@ -27,16 +26,3 @@ sendInitMessage camp_slug =
]
)
)
sendGetEventContent : String -> EventSlug -> Cmd Msg
sendGetEventContent campSlug eventSlug =
WebSocket.send scheduleServer
(Json.Encode.encode 0
(Json.Encode.object
[ ( "action", Json.Encode.string "get_event_content" )
, ( "event_slug", Json.Encode.string eventSlug )
, ( "camp_slug", Json.Encode.string campSlug )
]
)
)

View file

@ -28,6 +28,10 @@ class ScheduleConsumer(JsonWebsocketConsumer):
},
camp.get_days('camp')
))
events_query_set = Event.objects.filter(camp=camp)
events = list([x.serialize() for x in events_query_set])
event_instances_query_set = EventInstance.objects.filter(event__camp=camp)
event_instances = list([x.serialize(user=message.user) for x in event_instances_query_set])
@ -39,24 +43,15 @@ class ScheduleConsumer(JsonWebsocketConsumer):
data = {
"action": "init",
"events": events,
"event_instances": event_instances,
"event_locations": event_locations,
"event_types": event_types,
"event_instances": event_instances,
"days": days,
}
except Camp.DoesNotExist:
pass
if action == 'get_event_content':
camp_slug = content.get('camp_slug')
event_slug = content.get('event_slug')
event = Event.objects.get(
slug=event_slug,
camp__slug=camp_slug
)
data = event.serialize()
data['action'] = "get_event_content"
if action == 'favorite':
event_instance_id = content.get('event_instance_id')
event_instance = EventInstance.objects.get(id=event_instance_id)