Created
August 14, 2016 19:45
-
-
Save danyx23/35cea7421be6691cbda9e437d58641f0 to your computer and use it in GitHub Desktop.
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
-- new library function for using a FileReader contains this function: | |
readAsText : FileRef -> Task Error String | |
-- new helper to render HTML input with type set to "file" and a built-in decoder for a getting a List of FileRefs out. | |
-- I think this will be less prone to abuse than exposing the decoder and letting the user create the input herself | |
fileInput : (List FileRef -> msg) -> List (Attribute msg) -> List (Html msg) -> Html msg | |
type Model | |
= Empty | |
| Loaded String | |
| Failed String | |
type Msg | |
= FilesSelected (List FileRef) | |
| ReadSucceeded String | |
| ReadFailed String | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ fileInput FilesSelected [] [] | |
, (resultView model) | |
] | |
resultView : Model -> Html Msg | |
resultView model = | |
case model of | |
Empty -> | |
text "Please select a file" | |
Loaded content -> | |
text content | |
Failed message -> | |
text message | |
update : Msg -> Model -> (Model, Cmd Msg) | |
update msg model = | |
case msg of | |
FileSelected files -> | |
case files of | |
[] -> | |
( model, Cmd.none ) | |
first :: rest -> | |
( model | |
, readChosenFile first | |
) | |
ReadSucceeded content -> | |
( Loaded content | |
, Cmd.none | |
) | |
ReadFailed message -> | |
(Failed message | |
, Cmd.none | |
) | |
readChosenFile : FileRef -> Cmd Msg | |
readChosenFile fileRef = | |
Task.perform ReadFailed ReadSucceeded (FileReader.readAsText fileRef) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment