module Decisions.PostCellUpdate exposing (..) import Http import Json.Decode as Decode import Json.Encode as Encode import Decisions.Messages as Messages import Decisions.Model exposing (..) postCellUpdate : String -> CellUpdate -> Cmd Messages.Msg postCellUpdate url cellUpdate = postUpdate url (encodeCellUpdate cellUpdate) postBoolCellUpdate : String -> BoolCellUpdate -> Cmd Messages.Msg postBoolCellUpdate url boolCellUpdate = postBoolUpdate url (encodeBoolCellUpdate boolCellUpdate) postNestedUpdate url rowId columnId parent child = postUpdate url (encodeNestedCellUpdate rowId columnId parent child) postDecisionUpdate : String -> DecisionUpdate -> Cmd Messages.Msg postDecisionUpdate url decisionUpdate = postUpdate url (encodeDecisionUpdate decisionUpdate) postUpdate : String -> Encode.Value -> Cmd Messages.Msg postUpdate url value = let request = Http.post url (Http.jsonBody value) Decode.string in Http.send Messages.RecievePostNotification request postBoolUpdate : String -> Encode.Value -> Cmd Messages.Msg postBoolUpdate url value = let request = Http.post url (Http.jsonBody value) Decode.string in Http.send Messages.RecievePostNotification request encodeCellUpdate : CellUpdate -> Encode.Value encodeCellUpdate cellUpdate = Encode.object [ ( "rowId", Encode.string cellUpdate.rowId ) , ( "columnId", Encode.string cellUpdate.columnId ) , ( "value", Encode.string cellUpdate.value ) , ( "updateType", Encode.string "DECISION_QUESTION" ) ] encodeBoolCellUpdate : BoolCellUpdate -> Encode.Value encodeBoolCellUpdate boolCellUpdate = Encode.object [ ( "rowId", Encode.string boolCellUpdate.rowId ) , ( "columnId", Encode.string boolCellUpdate.columnId ) , ( "value", Encode.bool boolCellUpdate.value ) , ( "updateType", Encode.string "DECISION_QUESTION" ) ] encodeNestedCellUpdate : String -> String -> String -> String -> Encode.Value encodeNestedCellUpdate rowId columnId parent child = Encode.object [ ( "rowId", Encode.string rowId ) , ( "columnId", Encode.string columnId ) , ( "parent", Encode.string parent ) , ( "child", Encode.string child ) , ( "updateType", Encode.string "NESTED_DECISION_QUESTION" ) ] type alias DecisionUpdate = { submissionIds : List String , decision : String , subDecision : String } encodeDecisionUpdate : DecisionUpdate -> Encode.Value encodeDecisionUpdate decisionUpdate = Encode.object [ ( "submissionIds", Encode.list <| List.map Encode.string decisionUpdate.submissionIds ) , ( "decision", Encode.string decisionUpdate.decision ) , ( "subDecision", Encode.string decisionUpdate.subDecision ) , ( "updateType", Encode.string "OUTCOME" ) ]