Skip to content

Commit

Permalink
Notification basics
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Feb 25, 2019
1 parent d4bd9de commit 38e63bd
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 21 deletions.
3 changes: 2 additions & 1 deletion elm.json
Expand Up @@ -10,6 +10,7 @@
"Chadtech/return": "1.0.2",
"FabienHenon/elm-infinite-list-view": "3.0.0",
"NoRedInk/elm-json-decode-pipeline": "1.0.0",
"Skinney/murmur3": "2.0.8",
"avh4/elm-color": "1.0.0",
"danfishgold/base64-bytes": "1.0.1",
"danmarcab/material-icons": "1.0.0",
Expand All @@ -28,6 +29,7 @@
"elm/virtual-dom": "1.0.2",
"elm-community/list-extra": "8.1.0",
"elm-community/maybe-extra": "5.0.0",
"elm-explorations/markdown": "1.0.0",
"icidasset/elm-binary": "2.1.0",
"icidasset/elm-sha": "2.0.0",
"jorgengranseth/elm-string-format": "1.0.1",
Expand All @@ -43,7 +45,6 @@
"ymtszw/elm-xml-decode": "3.0.0"
},
"indirect": {
"Skinney/murmur3": "2.0.8",
"elm/parser": "1.1.0",
"elm-explorations/test": "1.2.0",
"fredcy/elm-parseint": "2.0.1",
Expand Down
13 changes: 7 additions & 6 deletions src/Applications/Brain/Sources/Processing/Common.elm
Expand Up @@ -2,6 +2,7 @@ module Brain.Sources.Processing.Common exposing (Model, Msg(..), contextToTagsCo

import Alien
import Brain.Reply exposing (Reply(..))
import Dict.Ext as Dict
import Http exposing (Error(..))
import Json.Encode as Encode
import List.Extra as List
Expand Down Expand Up @@ -71,14 +72,14 @@ isProcessing status =
reportHttpError : Source -> Http.Error -> Reply
reportHttpError source err =
reportError
{ sourceId = source.id
, error = translateHttpError source.service err
}
source
(translateHttpError source.service err)


reportError : { sourceId : String, error : String } -> Reply
reportError { sourceId, error } =
[ ( "sourceId", Encode.string sourceId )
reportError : Source -> String -> Reply
reportError source error =
[ ( "sourceId", Encode.string source.id )
, ( "sourceName", Encode.string (Dict.fetch "name" "Unnamed" source.data) )
, ( "error", Encode.string error )
]
|> Encode.object
Expand Down
67 changes: 53 additions & 14 deletions src/Applications/UI.elm
Expand Up @@ -10,6 +10,7 @@ import Color.Ext as Color
import Common exposing (Switch(..))
import Css exposing (url)
import Css.Global
import Dict.Ext as Dict
import File
import File.Download
import File.Select
Expand All @@ -18,6 +19,7 @@ import Html.Styled.Attributes exposing (id, style)
import Html.Styled.Lazy as Lazy
import Json.Decode
import Json.Encode
import Notifications
import Replying as N5 exposing (do, return)
import Return2 as R2
import Sources
Expand All @@ -32,6 +34,7 @@ import UI.Console
import UI.Core as Core exposing (Flags, Model, Msg(..))
import UI.Kit
import UI.Navigation as Navigation
import UI.Notifications
import UI.Page as Page
import UI.Ports as Ports
import UI.Queue as Queue
Expand Down Expand Up @@ -78,6 +81,7 @@ init flags url key =
, isAuthenticated = False
, isLoading = True
, navKey = key
, notifications = []
, page = Page.fromUrl url
, url = url
, viewport = flags.viewport
Expand Down Expand Up @@ -351,6 +355,10 @@ update msg model =
)

ImportJson json ->
let
notification =
Notifications.success "Imported data successfully!"
in
model
|> update
(json
Expand All @@ -361,15 +369,32 @@ update msg model =
|> N5.andThen2 (update Core.SaveFavourites)
|> N5.andThen2 (update Core.SaveSources)
|> N5.andThen2 (update Core.SaveTracks)
-- TODO:
-- Show notication relating to import
|> N5.andThen2 (update <| ShowNotification notification)
|> R2.addCmd (do <| ChangeUrlUsingPage Page.Index)

RequestImport ->
( model
, File.Select.file [ "application/json" ] Import
)

-----------------------------------------
-- Notifications
-----------------------------------------
DismissNotification { id } ->
( { model
| notifications =
List.filter
(Notifications.id >> (/=) id)
model.notifications
}
, Cmd.none
)

ShowNotification notification ->
( { model | notifications = notification :: model.notifications }
, Cmd.none
)

-----------------------------------------
-- URL
-----------------------------------------
Expand Down Expand Up @@ -505,20 +530,28 @@ translateAlienEvent event =
TracksMsg (Tracks.RemoveByPaths event.data)

Just Alien.ReportGenericError ->
let
dbg =
-- TODO
Debug.log "error" event
in
Bypass
event.data
|> Json.Decode.decodeValue Json.Decode.string
|> Result.map ((++) "Something went wrong, got the error: ")
|> Result.map (Notifications.error >> ShowNotification)
|> Result.withDefault Bypass

Just Alien.ReportProcessingError ->
let
dbg =
-- TODO
Debug.log "error" event
in
Bypass
case Json.Decode.decodeValue (Json.Decode.dict Json.Decode.string) event.data of
Ok dict ->
ShowNotification
(Notifications.stickyError
("Could not process the **"
++ Dict.fetch "sourceName" "" dict
++ "** source. I got the following response from the source:"
)
(Dict.fetch "error" "missingError" dict)
[]
)

Err _ ->
ShowNotification
(Notifications.error "Could not decode processing error")

Just Alien.SearchTracks ->
TracksMsg (Tracks.SetSearchResults event.data)
Expand Down Expand Up @@ -555,6 +588,12 @@ body model =
|> Lazy.lazy Backdrop.view
|> Html.map BackdropMsg

-----------------------------------------
-- Notifications
-----------------------------------------
, model.notifications
|> Lazy.lazy UI.Notifications.view

-----------------------------------------
-- Content
-----------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions src/Applications/UI/Core.elm
Expand Up @@ -6,6 +6,7 @@ import Browser.Navigation as Nav
import Common exposing (Switch(..))
import File exposing (File)
import Json.Encode as Json
import Notifications exposing (..)
import Queue
import Time
import UI.Authentication as Authentication
Expand Down Expand Up @@ -36,6 +37,7 @@ type alias Model =
, isAuthenticated : Bool
, isLoading : Bool
, navKey : Nav.Key
, notifications : List (Notification Msg)
, page : Page
, url : Url
, viewport : Viewport
Expand Down Expand Up @@ -115,6 +117,11 @@ type Msg
| Import File
| ImportJson String
| RequestImport
-----------------------------------------
-- Notifications
-----------------------------------------
| DismissNotification { id : Int }
| ShowNotification (Notification Msg)
-----------------------------------------
-- URL
-----------------------------------------
Expand Down
79 changes: 79 additions & 0 deletions src/Applications/UI/Notifications.elm
@@ -0,0 +1,79 @@
module UI.Notifications exposing (view)

import Chunky exposing (..)
import Color.Ext as Color
import Css
import Css.Global
import Html.Styled as Html exposing (Html)
import Html.Styled.Attributes exposing (css)
import Notifications exposing (..)
import Tachyons.Classes as T
import UI.Core exposing (Msg)
import UI.Kit



-- 🗺


view : List (Notification Msg) -> Html Msg
view notifications =
brick
[ css containerStyles ]
[ T.absolute
, T.bottom_0
, T.f6
, T.lh_title
, T.mb3
, T.mr3
, T.right_0
, T.z_9999
]
(List.map
(\notification ->
brick
[ css errorStyles ]
[ T.br2
, T.fw6
, T.measure_narrow
, T.mt2
, T.pa3
, T.white_90
]
[ contents notification
, if .sticky (options notification) then
chunk
[ T.f7, T.i, T.mt2, T.o_60 ]
[ Html.text "Double click to dismiss" ]

else
nothing
]
)
(List.reverse notifications)
)



-- 🖼


containerStyles : List Css.Style
containerStyles =
[ Css.lineHeight (Css.num 1.35)
, Css.Global.descendants
[ Css.Global.p
[ Css.margin Css.zero
, Css.padding Css.zero
]
, Css.Global.strong
[ Css.borderBottom3 (Css.px 1) Css.solid (Css.rgba 255 255 255 0.45)
, Css.fontWeight Css.inherit
]
]
]


errorStyles : List Css.Style
errorStyles =
[ Css.backgroundColor (Color.toElmCssColor UI.Kit.colors.error) ]

0 comments on commit 38e63bd

Please sign in to comment.