Homework-4/src/Main.elm

70 lines
2.1 KiB
Elm
Raw Normal View History

module Main exposing (..)
import ClassSchedule.Model exposing (..)
import ClassSchedule.View exposing (..)
2021-05-30 20:40:10 -07:00
import ClassSchedule.Decode exposing (..)
import Browser exposing (Document, document)
import Dict exposing (..)
import Tuple exposing (..)
2021-05-30 20:40:10 -07:00
import Http exposing (..)
oneHour : Time -> (Time, Time)
oneHour t = (t, addMinutes 50 t)
2021-05-16 21:00:04 -07:00
twoHours : Time -> (Time, Time)
twoHours t = (t, addMinutes 110 t)
nAm : Int -> Time
nAm i = (i, 0, AM)
nPm : Int -> Time
nPm i = (i, 0, PM)
onDays : List DayOfWeek -> (Time, Time) -> List (DayOfWeek, Time, Time)
onDays dds (t1, t2) = List.map (\d -> (d, t1, t2)) dds
init : Flags -> (Model, Cmd Msg)
2021-05-30 20:40:10 -07:00
init () =
( { terms = Dict.empty, term = "Spring 2021", searchInput = "", selected = Nothing }
, Http.get
{ url = "cs565_hw4c_schedule_data.json"
, expect = expectJson ReceiveData response
}
)
view : Model -> Document Msg
view m =
{ title = "Course Scheduler"
, body = [ viewModel m ]
}
2021-05-16 17:02:57 -07:00
modifyCurrent : (List (CourseStatus, Course) -> List (CourseStatus, Course)) -> Model -> Model
modifyCurrent f m = { m | terms = Dict.update (m.term) (Maybe.map f) (m.terms) }
changeCourse : Int -> CourseStatus -> List (CourseStatus, Course) -> List (CourseStatus, Course)
changeCourse i ncs =
let updateCourses j (cs, c) = if i == j then (ncs, c) else (cs, c)
in List.indexedMap updateCourses
update : Msg -> Model -> (Model, Cmd Msg)
2021-05-16 17:02:57 -07:00
update msg m =
case msg of
SelectCourse i -> ({ m | selected = Just i}, Cmd.none)
AddCourse i -> (modifyCurrent (changeCourse i Added) m, Cmd.none)
RemoveCourse i -> (modifyCurrent (changeCourse i NotAdded) m, Cmd.none)
SearchInput s -> ({ m | searchInput = s }, Cmd.none)
2021-05-30 20:40:10 -07:00
ReceiveData e ->
case e of
Err er -> Debug.log (Debug.toString er) (m, Cmd.none)
Ok d -> ({ m | terms = Dict.singleton "Spring 2021" <| List.map (pair NotAdded) d }, Cmd.none)
2021-05-16 17:02:57 -07:00
_ -> (m, Cmd.none)
subscriptions : Model -> Sub Msg
subscriptions m = Sub.none
main = document
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}