Skip to content

Commit

Permalink
Implement rule deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Simmerl committed Mar 9, 2017
1 parent 06ca95a commit ad100e3
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 68 deletions.
5 changes: 3 additions & 2 deletions cmd/console/Action.elm
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ type Msg
| FetchApps (WebData (List App))
| FetchRule (WebData Rule)
| FetchRules (WebData (List Rule))
| ListApps
| LocationChange Location
| Navigate Route
| NewApp (WebData App)
| SelectApp String
| RuleDeleteAsk String
| RuleDeleteConfirm String
| RuleDelete (WebData Bool)
| Tick Time
8 changes: 5 additions & 3 deletions cmd/console/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Main exposing (main)
import AnimationFrame
import Navigation
import Action exposing (Msg(..))
import Ask exposing (confirm)
import Model exposing (Flags, Model, init)
import Update exposing (update)
import View exposing (view)
Expand All @@ -18,10 +19,11 @@ main =
}



-- SUBSCRIPTION


subscriptions : Model -> Sub Msg
subscriptions model =
AnimationFrame.times Tick
Sub.batch
[ AnimationFrame.times Tick
, confirm RuleDeleteConfirm
]
28 changes: 26 additions & 2 deletions cmd/console/Rule/Api.elm
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
module Rule.Api exposing (getRule, listRules)
module Rule.Api exposing (deleteRule, getRule, listRules)

import Http
import RemoteData exposing (WebData, sendRequest)
import Rule.Model exposing (Rule, decode, decodeList)


deleteRule : String -> String -> Cmd (WebData Bool)
deleteRule appId ruleId =
Http.request
{ body = Http.emptyBody
, expect = Http.expectStringResponse readDelete
, headers = []
, method = "DELETE"
, timeout = Nothing
, url = ruleUrl appId ruleId
, withCredentials = False
}
|> sendRequest

getRule : String -> String -> Cmd (WebData Rule)
getRule appId ruleId =
Http.get ("/api/apps/" ++ appId ++ "/rules/" ++ ruleId) decode
Http.get (ruleUrl appId ruleId) decode
|> sendRequest


listRules : String -> Cmd (WebData (List Rule))
listRules appId =
Http.get ("/api/apps/" ++ appId ++ "/rules") decodeList
|> sendRequest

readDelete : Http.Response String -> Result String Bool
readDelete response =
if response.status.code == 204 then
Ok True
else
Err response.status.message

ruleUrl : String -> String -> String
ruleUrl appId ruleId =
"/api/apps/" ++ appId ++ "/rules/" ++ ruleId
45 changes: 38 additions & 7 deletions cmd/console/Rule/Model.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
module Rule.Model exposing (Rule, decode, decodeList, targetString)
module Rule.Model exposing (Entity(..), Rule, decode, decodeList, targetString)

import Dict exposing (Dict)
import Json.Decode as Decode


-- MODEL

type Criteria
= EventCriteria
| ObjectCriteria

type Entity
= Connection | Event | Object | Reaction | UnknownEntity

type alias Recipient =
{ query : List Query
Expand All @@ -17,21 +23,40 @@ type alias Recipient =

type alias Rule =
{ active : Bool
, criteria : Criteria
, deleted : Bool
, ecosystem : Int
, entity : Int
, entity : Entity
, id : String
, name : String
, recipients : List Recipient
}

type Target
= Commenters | PostOwner | Unknown
= Commenters | PostOwner | UnknownTarget

type alias Query =
( String, String )


matchEntity : Int -> Entity
matchEntity enum =
case enum of
0 ->
Connection

1 ->
Event

2 ->
Object

3 ->
Reaction

_ ->
UnknownEntity

matchTarget : Query -> Target
matchTarget query =
case query of
Expand All @@ -42,7 +67,7 @@ matchTarget query =
PostOwner

( _, _ ) ->
Unknown
UnknownTarget

targetString : Target -> String
targetString target =
Expand All @@ -53,7 +78,7 @@ targetString target =
PostOwner ->
"PostOwner"

Unknown ->
UnknownTarget ->
"Unknown"


Expand All @@ -63,15 +88,21 @@ targetString target =

decode : Decode.Decoder Rule
decode =
Decode.map7 Rule
Decode.map8 Rule
(Decode.field "active" Decode.bool)
(Decode.field "criteria" decodeCriteria)
(Decode.field "deleted" Decode.bool)
(Decode.field "ecosystem" Decode.int)
(Decode.field "entity" Decode.int)
(Decode.andThen decodeEntity (Decode.field "entity" Decode.int))
(Decode.field "id" Decode.string)
(Decode.field "name" Decode.string)
(Decode.field "recipients" (Decode.list decodeRecipient))

decodeCriteria =
Decode.succeed EventCriteria

decodeEntity raw =
Decode.succeed (matchEntity raw)

decodeList : Decode.Decoder (List Rule)
decodeList =
Expand Down
67 changes: 54 additions & 13 deletions cmd/console/Rule/View.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Html.Attributes exposing (class, title)
import Html.Events exposing (onClick)
import Rule.Model exposing (Rule)

import Rule.Model exposing (Entity(..))

viewActivated : Bool -> Html msg
viewActivated active =
Expand All @@ -22,22 +23,22 @@ viewEcosystem ecosystem =
_ ->
span [ class "nc-icon-outline ui-2_alert", title "unknown" ] []

viewEntity : Int -> Html msg
viewEntity : Entity -> Html msg
viewEntity entity =
case entity of
0 ->
Connection ->
span [ class "nc-icon-outline arrows-2_conversion", title "Connection" ] []

1 ->
Event ->
span [ class "nc-icon-outline ui-1_bell-53", title "event" ] []

2 ->
Object ->
span [ class "nc-icon-outline ui-1_database", title "Object" ] []

3 ->
Reaction ->
span [ class "nc-icon-outline ui-2_like", title "Reaction" ] []

_ ->
UnknownEntity ->
span [ class "nc-icon-outline ui-2_alert", title "Unknown" ] []

viewRuleDescription : Rule -> Html msg
Expand All @@ -59,31 +60,31 @@ viewRuleDescription rule =

entity =
case rule.entity of
0 ->
Connection ->
div [ class "icon", title "Connections" ]
[ span [ class "nc-icon-outline arrows-2_conversion" ] []
, span [] [ text "Connections" ]
]

1 ->
Event ->
div [ class "icon", title "Events" ]
[ span [ class "nc-icon-outline ui-1_bell-53" ] []
, span [] [ text "Events" ]
]

2 ->
Object ->
div [ class "icon", title "Objects" ]
[ span [ class "nc-icon-outline ui-1_database" ] []
, span [] [ text "Objects" ]
]

3 ->
Reaction ->
div [ class "icon", title "Reactions" ]
[ span [ class "nc-icon-outline ui-2_like" ] []
, span [] [ text "Reactions" ]
]

_ ->
UnknownEntity ->
div [ class "icon", title "Unknown" ]
[ span [ class "nc-icon-outline ui-2_alert" ] []
, span [] [ text "Unknown" ]
Expand Down Expand Up @@ -115,9 +116,9 @@ viewRuleTable : (Rule -> Html msg) -> List Rule -> Html msg
viewRuleTable item rules =
let
list =
List.sortBy .entity rules
List.sortWith sortByEntity rules
in
table []
table [ class "navigation" ]
[ thead []
[ tr []
[ th [ class "icon" ] [ text "active" ]
Expand All @@ -129,3 +130,43 @@ viewRuleTable item rules =
]
, tbody [] (List.map item list)
]


sortByEntity : Rule -> Rule -> Order
sortByEntity a b =
case (a.entity, b.entity) of
(Connection, Connection) ->
EQ

(Connection, _) ->
LT

(Event, Connection) ->
GT

(Event, Event) ->
EQ

(Event, _) ->
LT

(Object, Connection) ->
GT

(Object, Event) ->
GT

(Object, Object) ->
EQ

(Object, _) ->
LT

(Reaction, Reaction) ->
EQ

(Reaction, _) ->
GT

(UnknownEntity, _) ->
GT
15 changes: 10 additions & 5 deletions cmd/console/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ module Update exposing (update)

import RemoteData exposing (RemoteData(Loading, NotAsked), WebData)
import Action exposing (Msg(..))
import Ask exposing (ask)
import Formo exposing (blurElement, elementValue, focusElement, updateElementValue, validateForm)
import Model exposing (Flags, Model, init)
import App.Api exposing (createApp)
import App.Model exposing (initAppForm)
import Route
import Rule.Api exposing (deleteRule)


update : Msg -> Model -> ( Model, Cmd Msg )
Expand Down Expand Up @@ -48,9 +50,6 @@ update msg model =
FetchRules response ->
( { model | rules = response }, Cmd.none )

ListApps ->
( model, Cmd.map LocationChange (Route.navigate Route.Apps) )

LocationChange location ->
init (Flags model.zone) location

Expand All @@ -60,8 +59,14 @@ update msg model =
NewApp response ->
( { model | appForm = initAppForm, apps = (appendWebData model.apps response), newApp = NotAsked }, Cmd.none )

SelectApp id ->
( model, Cmd.map LocationChange (Route.navigate (Route.App id)) )
RuleDeleteAsk id ->
( model, ask id )

RuleDeleteConfirm id ->
( model, Cmd.map RuleDelete (deleteRule model.appId id) )

RuleDelete _ ->
( model, Cmd.map LocationChange (Route.navigate (Route.Rules model.appId)) )

Tick time ->
let
Expand Down
Loading

0 comments on commit ad100e3

Please sign in to comment.