Homework-4/src/Main.elm

70 lines
2.1 KiB
Elm

module Main exposing (..)
import ClassSchedule.Model exposing (..)
import ClassSchedule.View exposing (..)
import ClassSchedule.Decode exposing (..)
import Browser exposing (Document, document)
import Dict exposing (..)
import Tuple exposing (..)
import Http exposing (..)
oneHour : Time -> (Time, Time)
oneHour t = (t, addMinutes 50 t)
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)
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 ]
}
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)
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)
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)
_ -> (m, Cmd.none)
subscriptions : Model -> Sub Msg
subscriptions m = Sub.none
main = document
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}