Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
refactor elm
Browse files Browse the repository at this point in the history
  • Loading branch information
monty5811 committed Feb 2, 2017
1 parent 416e086 commit b5cce7c
Show file tree
Hide file tree
Showing 22 changed files with 200 additions and 340 deletions.
12 changes: 6 additions & 6 deletions apostello/static/js/app.js

Large diffs are not rendered by default.

66 changes: 0 additions & 66 deletions assets/elm/Actions.elm

This file was deleted.

25 changes: 15 additions & 10 deletions assets/elm/Decoders.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Decoders exposing (..)

import Json.Decode as Decode
import Json.Decode.Extra exposing (date)
import Json.Decode.Pipeline exposing (optional, required, decode)
import Json.Decode.Pipeline exposing (optional, required, decode, requiredAt)
import Models exposing (..)


Expand All @@ -11,22 +11,27 @@ decodeAlwaysTrue =
Decode.succeed True


dataFromResp : Decode.Decoder a -> RawResponse -> List a
dataFromResp decoder rawResp =
rawResp.body
|> Decode.decodeString (Decode.field "results" (Decode.list decoder))
|> Result.withDefault []


itemFromResp : a -> Decode.Decoder a -> RawResponse -> a
itemFromResp defaultCallback decoder rawResp =
rawResp.body
|> Decode.decodeString decoder
|> Result.withDefault defaultCallback


decodeFirstRunResp : Decode.Decoder FirstRunResp
decodeFirstRunResp =
decode FirstRunResp
|> required "status" Decode.string
|> optional "error" Decode.string ""


decodeApostelloResponse : Decode.Decoder a -> Decode.Decoder (ApostelloResponse a)
decodeApostelloResponse resultsDecoder =
decode ApostelloResponse
|> required "next" (Decode.maybe Decode.string)
|> required "previous" (Decode.maybe Decode.string)
|> required "count" Decode.int
|> required "results" (Decode.list resultsDecoder)


elvantogroupDecoder : Decode.Decoder ElvantoGroup
elvantogroupDecoder =
decode ElvantoGroup
Expand Down
46 changes: 5 additions & 41 deletions assets/elm/Helpers.elm
Original file line number Diff line number Diff line change
@@ -1,52 +1,13 @@
module Helpers exposing (..)

import Decoders exposing (..)
import Date
import Dict
import Http
import Json.Decode as Decode
import Messages exposing (..)
import Models exposing (..)
import Updates.Notification exposing (createLoadingFailedNotification, createNotSavedNotification)
import Regex


-- Fetch data from server


getData : String -> Decode.Decoder a -> Http.Request a
getData url decoder =
Http.request
{ method = "GET"
, headers = [ Http.header "Accept" "application/json" ]
, url = url
, body = Http.emptyBody
, expect = Http.expectJson decoder
, timeout = Nothing
, withCredentials = True
}


getApostelloResponse : String -> Decode.Decoder a -> Http.Request (ApostelloResponse a)
getApostelloResponse url decoder =
getData url (decodeApostelloResponse decoder)



-- determine loading status


determineLoadingStatus : ApostelloResponse a -> LoadingStatus
determineLoadingStatus resp =
case resp.next of
Nothing ->
Finished

_ ->
WaitingForSubsequent



-- increase page size for next response


Expand All @@ -70,8 +31,7 @@ mergeItems existingItems newItems =
|> List.map (\x -> ( x.pk, x ))
|> Dict.fromList
|> addNewItems newItems
|> Dict.toList
|> List.map (\x -> Tuple.second x)
|> Dict.values


addNewItems : List { a | pk : Int } -> Dict.Dict Int { a | pk : Int } -> Dict.Dict Int { a | pk : Int }
Expand Down Expand Up @@ -99,6 +59,10 @@ handleNotSaved model =
( createNotSavedNotification model, Cmd.none )



-- sorting functions


sortByTimeReceived : List { a | time_received : String } -> List { a | time_received : String }
sortByTimeReceived items =
items
Expand Down
5 changes: 2 additions & 3 deletions assets/elm/Main.elm
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module Main exposing (..)

import Actions exposing (fetchData)
import Html exposing (programWithFlags)
import Messages exposing (Msg)
import Messages exposing (..)
import Models exposing (..)
import Subscriptions exposing (subscriptions)
import Update exposing (update)
Expand All @@ -28,7 +27,7 @@ init flags =
model =
initialModel flags.csrftoken page flags.dataUrl flags.fabData
in
( model, fetchData page flags.dataUrl )
update (LoadData (initialLoadingStatus page)) model


decodePage : String -> Page
Expand Down
37 changes: 11 additions & 26 deletions assets/elm/Messages.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import Time

type Msg
= LoadData LoadingStatus
| ReceiveRawResp (Result Http.Error RawResponse)
| UpdateTableFilter String
| ElvantoMsg ElvantoMsg
| InboundTableMsg InboundTableMsg
| OutboundTableMsg OutboundTableMsg
| RecipientTableMsg RecipientTableMsg
| KeywordTableMsg KeywordTableMsg
| GroupTableMsg GroupTableMsg
Expand Down Expand Up @@ -58,76 +58,61 @@ type FirstRunMsg


type ElvantoMsg
= LoadElvantoResp (Result Http.Error (ApostelloResponse ElvantoGroup))
| ToggleGroupSync ElvantoGroup
= ToggleGroupSync ElvantoGroup
| ReceiveToggleGroupSync (Result Http.Error ElvantoGroup)
| PullGroups
| FetchGroups
| ReceiveButtonResp (Result Http.Error Bool)


type InboundTableMsg
= LoadInboundTableResp (Result Http.Error (ApostelloResponse SmsInbound))
| ReprocessSms Int
= ReprocessSms Int
| ReceiveReprocessSms (Result Http.Error SmsInbound)


type GroupMemberSelectMsg
= LoadGroupMemberSelectResp (Result Http.Error RecipientGroup)
| UpdateMemberFilter String
= UpdateMemberFilter String
| UpdateNonMemberFilter String
| ToggleMembership RecipientSimple
| ReceiveToggleMembership (Result Http.Error RecipientGroup)


type WallMsg
= LoadWallResp (Result Http.Error (ApostelloResponse SmsInboundSimple))
| ToggleWallDisplay Bool Int
= ToggleWallDisplay Bool Int
| ReceiveToggleWallDisplay (Result Http.Error SmsInboundSimple)


type UserProfileTableMsg
= LoadUserProfileTableResp (Result Http.Error (ApostelloResponse UserProfile))
| ToggleField UserProfile
= ToggleField UserProfile
| ReceiveToggleProfile (Result Http.Error UserProfile)


type RecipientTableMsg
= LoadRecipientTableResp (Result Http.Error (ApostelloResponse Recipient))
| ToggleRecipientArchive Bool Int
= ToggleRecipientArchive Bool Int
| ReceiveRecipientToggleArchive (Result Http.Error Recipient)


type KeywordTableMsg
= LoadKeywordTableResp (Result Http.Error (ApostelloResponse Keyword))
| ToggleKeywordArchive Bool Int
= ToggleKeywordArchive Bool Int
| ReceiveToggleKeywordArchive (Result Http.Error Keyword)


type GroupTableMsg
= LoadGroupTableResp (Result Http.Error (ApostelloResponse RecipientGroup))
| ToggleGroupArchive Bool Int
= ToggleGroupArchive Bool Int
| ReceiveToggleGroupArchive (Result Http.Error RecipientGroup)


type OutboundTableMsg
= LoadOutboundTableResp (Result Http.Error (ApostelloResponse SmsOutbound))


type GroupComposerMsg
= UpdateQueryString String
| LoadGroupComposerResp (Result Http.Error (ApostelloResponse RecipientGroup))


type ScheduledSmsTableMsg
= LoadScheduledSmsTableResp (Result Http.Error (ApostelloResponse QueuedSms))
| CancelSms Int
= CancelSms Int
| ReceiveCancelSms (Result Http.Error Bool)


type KeyRespTableMsg
= LoadKeyRespTableResp (Result Http.Error (ApostelloResponse SmsInbound))
| ToggleInboundSmsArchive Bool Int
= ToggleInboundSmsArchive Bool Int
| ToggleInboundSmsDealtWith Bool Int
| ReceiveToggleInboundSmsArchive (Result Http.Error SmsInbound)
| ReceiveToggleInboundSmsDealtWith (Result Http.Error SmsInbound)
14 changes: 6 additions & 8 deletions assets/elm/Models.elm
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ type alias Flags =
}


type alias RawResponse =
{ body : String
, next : Maybe String
}


type alias Notification =
{ type_ : NotificationType
, text : String
Expand Down Expand Up @@ -366,14 +372,6 @@ initialKeyRespModel =
-- Apostello Models


type alias ApostelloResponse a =
{ next : Maybe String
, previous : Maybe String
, count : Int
, results : List a
}


type alias GroupPk =
Int

Expand Down
41 changes: 41 additions & 0 deletions assets/elm/Remote.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Remote exposing (..)

import Http
import Messages exposing (..)
import Models exposing (..)
import Json.Decode as Decode


fetchData : String -> Cmd Msg
fetchData dataUrl =
makeRequest dataUrl
|> Http.send ReceiveRawResp


makeRequest : String -> Http.Request RawResponse
makeRequest dataUrl =
Http.request
{ method = "GET"
, headers =
[ Http.header "Accept" "application/json" ]
, url = dataUrl
, body = Http.emptyBody
, expect =
Http.expectStringResponse (\resp -> Ok (extractRawResponse resp))
, timeout = Nothing
, withCredentials = True
}


extractRawResponse : Http.Response String -> RawResponse
extractRawResponse resp =
{ body = resp.body
, next = nextFromBody resp.body
}


nextFromBody : String -> Maybe String
nextFromBody body =
body
|> Decode.decodeString (Decode.field "next" (Decode.maybe Decode.string))
|> Result.withDefault Nothing

0 comments on commit b5cce7c

Please sign in to comment.