Skip to content

Commit

Permalink
Merge tag 'elm-0.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmjones committed Feb 9, 2017
2 parents 0f24336 + 2bf1833 commit 1716f96
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
1 change: 1 addition & 0 deletions elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
],
"exposed-modules": [],
"dependencies": {
"elm-community/result-extra": "2.2.0 <= v < 3.0.0",
"elm-lang/core": "5.1.1 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"elm-lang/http": "1.0.0 <= v < 2.0.0",
Expand Down
64 changes: 41 additions & 23 deletions src/elm/CronPixie.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module CronPixie exposing (..)
import Html exposing (Html, div, text, h3, ul, li, span)
import Html.Attributes exposing (class, title)
import Html.Events exposing (..)
import Html.App
import Date
import Date.Format
import Time exposing (Time, second)
import String
import List exposing (head, tail, reverse)
import Maybe exposing (withDefault)
import Task
import Http exposing (stringData, multipart)
import Http exposing (stringPart, multipartBody, encodeUri)
import Json.Decode exposing (..)
import Json.Encode as Json
import Result.Extra exposing (unpack)


-- MODEL
Expand Down Expand Up @@ -84,11 +84,9 @@ init flags =

type Msg
= Tick Time
| FetchSucceed (List Schedule)
| FetchFail Http.Error
| Fetch (Result Http.Error (List Schedule))
| RunNow Event
| PostSucceed String
| PostFail Http.Error
| UpdateEvent (Result Http.Error String)



Expand Down Expand Up @@ -172,7 +170,7 @@ displayInterval model seconds =
if 0 > (seconds + 60) then
-- Cron runs max every 60 seconds.
model.strings.passed
else if 0 > (seconds - model.timer_period) then
else if 0 > (toFloat seconds - model.timer_period) then
-- If due now or in next refresh period, show "now".
model.strings.now
else
Expand Down Expand Up @@ -216,10 +214,10 @@ update msg model =
Tick newTime ->
( model, getSchedules model.nonce )

FetchSucceed schedules ->
Fetch (Ok schedules) ->
( { model | schedules = schedules }, Cmd.none )

FetchFail err ->
Fetch (Err _) ->
( model, Cmd.none )

RunNow event ->
Expand All @@ -229,20 +227,20 @@ update msg model =
in
( { model | schedules = List.map (updateScheduledEvent event dueEvent) model.schedules }, postEvent model.nonce dueEvent )

PostSucceed schedules ->
UpdateEvent (Ok schedules) ->
( model, Cmd.none )

PostFail err ->
UpdateEvent (Err _) ->
( model, Cmd.none )


getSchedules : String -> Cmd Msg
getSchedules nonce =
let
url =
Http.url "/wp-admin/admin-ajax.php" [ ( "action", "cron_pixie_schedules" ), ( "nonce", nonce ) ]
encodedUrl =
url "/wp-admin/admin-ajax.php" [ ( "action", "cron_pixie_schedules" ), ( "nonce", nonce ) ]
in
Task.perform FetchFail FetchSucceed (Http.get schedulesDecoder url)
Http.send Fetch (Http.get encodedUrl schedulesDecoder)


schedulesDecoder : Decoder (List Schedule)
Expand All @@ -252,12 +250,12 @@ schedulesDecoder =

scheduleDecoder : Decoder Schedule
scheduleDecoder =
object4 Schedule ("name" := string) ("display" := string) (maybe ("interval" := int)) (maybe ("events" := (list eventDecoder)))
map4 Schedule (field "name" string) (field "display" string) (maybe (field "interval" int)) (maybe (field "events" (list eventDecoder)))


eventDecoder : Decoder Event
eventDecoder =
object6 Event (oneOf [ "schedule" := string, succeed "false" ]) (maybe ("interval" := int)) ("hook" := string) ("args" := eventArgsDecoder) ("timestamp" := int) ("seconds_due" := int)
map6 Event (oneOf [ field "schedule" string, succeed "false" ]) (maybe (field "interval" int)) (field "hook" string) (field "args" eventArgsDecoder) (field "timestamp" int) (field "seconds_due" int)


eventArgsDecoder : Decoder (List ( String, String ))
Expand Down Expand Up @@ -329,14 +327,34 @@ postEvent nonce event =
]

body =
multipart
[ stringData "action" "cron_pixie_events"
, stringData "nonce" nonce
, stringData "model" (Json.encode 0 eventValue)
multipartBody
[ stringPart "action" "cron_pixie_events"
, stringPart "nonce" nonce
, stringPart "model" (Json.encode 0 eventValue)
-- , stringData "model" (Json.encode 0 eventValue)
]
in
Task.perform PostFail PostSucceed (Http.post string url body)
Http.send UpdateEvent (Http.post url body string)


url : String -> List ( String, String ) -> String
url baseUrl args =
case args of
[] ->
baseUrl

_ ->
baseUrl ++ "?" ++ String.join "&" (List.map queryPair args)


queryPair : ( String, String ) -> String
queryPair ( key, value ) =
queryEscape key ++ "=" ++ queryEscape value


queryEscape : String -> String
queryEscape string =
String.join "+" (String.split "%20" (encodeUri string))



Expand All @@ -352,9 +370,9 @@ subscriptions model =
-- MAIN


main : Program Flags
main : Program Flags Model Msg
main =
Html.App.programWithFlags
Html.programWithFlags
{ init = init
, view = view
, update = update
Expand Down

0 comments on commit 1716f96

Please sign in to comment.