-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from levelhq/digest-settings
Enable/disable digest
- Loading branch information
Showing
28 changed files
with
695 additions
and
113 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module DigestSettings exposing (DigestSettings, decoder, fragment, isEnabled, toggle) | ||
|
||
import GraphQL exposing (Fragment) | ||
import Json.Decode as Decode exposing (Decoder) | ||
|
||
|
||
type DigestSettings | ||
= DigestSettings Data | ||
|
||
|
||
type alias Data = | ||
{ isEnabled : Bool | ||
} | ||
|
||
|
||
|
||
-- PROPERTIES | ||
|
||
|
||
isEnabled : DigestSettings -> Bool | ||
isEnabled (DigestSettings data) = | ||
data.isEnabled | ||
|
||
|
||
toggle : DigestSettings -> DigestSettings | ||
toggle (DigestSettings data) = | ||
DigestSettings { data | isEnabled = not data.isEnabled } | ||
|
||
|
||
|
||
-- GRAPHQL | ||
|
||
|
||
fragment : Fragment | ||
fragment = | ||
let | ||
queryBody = | ||
""" | ||
fragment DigestSettingsFields on DigestSettings { | ||
isEnabled | ||
} | ||
""" | ||
in | ||
GraphQL.toFragment queryBody [] | ||
|
||
|
||
|
||
-- DECODERS | ||
|
||
|
||
decoder : Decoder DigestSettings | ||
decoder = | ||
Decode.map DigestSettings <| | ||
Decode.map Data (Decode.field "isEnabled" Decode.bool) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
module Mutation.UpdateDigestSettings exposing (Response(..), request, variables) | ||
|
||
import DigestSettings exposing (DigestSettings) | ||
import GraphQL exposing (Document) | ||
import Id exposing (Id) | ||
import Json.Decode as Decode exposing (Decoder) | ||
import Json.Encode as Encode | ||
import Session exposing (Session) | ||
import Task exposing (Task) | ||
import User exposing (User) | ||
import ValidationError exposing (ValidationError) | ||
import ValidationFields | ||
|
||
|
||
type Response | ||
= Success DigestSettings | ||
| Invalid (List ValidationError) | ||
|
||
|
||
document : Document | ||
document = | ||
GraphQL.toDocument | ||
""" | ||
mutation UpdateDigestSettings( | ||
$spaceId: ID!, | ||
$isEnabled: Boolean | ||
) { | ||
updateDigestSettings( | ||
spaceId: $spaceId, | ||
isEnabled: $isEnabled | ||
) { | ||
...ValidationFields | ||
digestSettings { | ||
...DigestSettingsFields | ||
} | ||
} | ||
} | ||
""" | ||
[ User.fragment | ||
, DigestSettings.fragment | ||
, ValidationFields.fragment | ||
] | ||
|
||
|
||
variables : Id -> Bool -> Maybe Encode.Value | ||
variables spaceId isEnabled = | ||
Just <| | ||
Encode.object | ||
[ ( "spaceId", Id.encoder spaceId ) | ||
, ( "isEnabled", Encode.bool isEnabled ) | ||
] | ||
|
||
|
||
successDecoder : Decoder Response | ||
successDecoder = | ||
Decode.map Success <| | ||
Decode.at [ "data", "updateDigestSettings", "digestSettings" ] DigestSettings.decoder | ||
|
||
|
||
failureDecoder : Decoder Response | ||
failureDecoder = | ||
Decode.map Invalid <| | ||
Decode.at [ "data", "updateDigestSettings", "errors" ] (Decode.list ValidationError.decoder) | ||
|
||
|
||
decoder : Decoder Response | ||
decoder = | ||
let | ||
conditionalDecoder : Bool -> Decoder Response | ||
conditionalDecoder success = | ||
case success of | ||
True -> | ||
successDecoder | ||
|
||
False -> | ||
failureDecoder | ||
in | ||
Decode.at [ "data", "updateDigestSettings", "success" ] Decode.bool | ||
|> Decode.andThen conditionalDecoder | ||
|
||
|
||
request : Id -> Bool -> Session -> Task Session.Error ( Session, Response ) | ||
request spaceId isEnabled session = | ||
Session.request session <| | ||
GraphQL.request document (variables spaceId isEnabled) decoder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.