Skip to content

Commit 6da8eb3

Browse files
authored
Add ReadPrintFileContentsConcur (#250)
1 parent 35243fa commit 6da8eb3

File tree

8 files changed

+105
-0
lines changed

8 files changed

+105
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ Running a web-compatible recipe:
135135
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/PositionsHalogenHooks/src/Main.purs)) | [PositionsHalogenHooks](recipes/PositionsHalogenHooks) | A Halogen port of the ["Random - Positions" Elm Example](https://elm-lang.org/examples/positions). |
136136
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/PositionsReactHooks/src/Main.purs)) | [PositionsReactHooks](recipes/PositionsReactHooks) | A React port of the ["Random - Positions" Elm Example](https://elm-lang.org/examples/positions). |
137137
| :heavy_check_mark: | | [RandomNumberGameNode](recipes/RandomNumberGameNode) | This recipe shows how to build a "guess the random number" game using a custom `AppM` monad via the `ReaderT` design pattern and `Aff`, storing the game state in a mutable variable via a `Ref`. |
138+
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/ReadPrintFileContentsConcur/src/Main.purs) - [fixme](recipes/ReadPrintFileContentsConcur/tryFixMe.md)) | [ReadPrintFileContentsConcur](recipes/ReadPrintFileContentsConcur) | A Concur recipe to open a local file and display its content in a web page. |
138139
| :heavy_check_mark: | | [ReadPrintFileContentsNode](recipes/ReadPrintFileContentsNode) | Reads a file's contents and prints it to the console. |
139140
| | :heavy_check_mark: | [RoutingHashHalogenClassic](recipes/RoutingHashHalogenClassic) | This recipe shows how to use `purescript-routing` to do client-side hash-based routing in a Halogen-based single-page application (SPA). |
140141
| | :heavy_check_mark: | [RoutingHashLog](recipes/RoutingHashLog) | This recipe demonstrates hash-based routing with `purescript-routing`. No web framework is used. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/bower_components/
2+
/node_modules/
3+
/.pulp-cache/
4+
/output/
5+
/generated-docs/
6+
/.psc-package/
7+
/.psc*
8+
/.purs*
9+
/.psa*
10+
/.spago
11+
/.cache/
12+
/dist/
13+
/web-dist/
14+
/prod-dist/
15+
/prod/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# ReadPrintFileContentsConcur
2+
3+
A Concur recipe to open a local file and display its content in a web page.
4+
5+
## Expected Behavior:
6+
7+
When the input button is clicked, and a text file is chosen, the content of the file should be displayed on the page.
8+
9+
### Browser
10+
11+
The browser will display the text content as a <pre> element.
12+
13+
## NPM Dependencies
14+
* [react](https://www.npmjs.com/package/react)
15+
* [react-dom](https://www.npmjs.com/package/react-dom)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{ name = "ReadPrintFileContentsConcur"
2+
, dependencies =
3+
[ "concur-react", "console", "dom-filereader", "effect", "psci-support" ]
4+
, packages = ../../packages.dhall
5+
, sources = [ "recipes/ReadPrintFileContentsConcur/src/**/*.purs" ]
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module ReadPrintFileContentsConcur.Main where
2+
3+
import Prelude
4+
5+
import Concur.Core (Widget)
6+
import Concur.React (HTML)
7+
import Concur.React.DOM as D
8+
import Concur.React.Props as P
9+
import Concur.React.Run (runWidgetInDom)
10+
import Data.Maybe (Maybe(..), maybe)
11+
import Effect (Effect)
12+
import Effect.Aff.Class (liftAff)
13+
import Effect.Class (liftEffect)
14+
import Web.File.File (toBlob)
15+
import Web.File.FileList (FileList, item)
16+
import Web.File.FileReader.Aff (readAsText)
17+
import Web.HTML.HTMLInputElement (files, fromEventTarget)
18+
import React.SyntheticEvent (target, SyntheticEvent_, NativeEventTarget)
19+
import Unsafe.Coerce (unsafeCoerce)
20+
import Web.Event.Internal.Types (EventTarget)
21+
22+
fileList :: EventTarget -> Effect (Maybe FileList)
23+
fileList tar = case fromEventTarget tar of
24+
Just elem -> files elem
25+
_ -> pure Nothing
26+
27+
getContent :: forall t
28+
. Widget HTML (SyntheticEvent_ ( target :: NativeEventTarget
29+
| t
30+
)
31+
) -> Widget HTML String
32+
getContent ev = do
33+
nativeTarget <- liftEffect =<< target <$> ev
34+
mfs <- liftEffect $ fileList $ unsafeCoerce nativeTarget
35+
liftAff $ maybe (pure "")
36+
readAsText
37+
$ (Just <<< toBlob) =<< item 0 =<< mfs
38+
39+
textFileToScreenWidget Widget HTML String
40+
textFileToScreenWidget = do
41+
content <- getContent $ D.input [ P._type "file"
42+
, P.onChange
43+
]
44+
D.pre' [D.text content]
45+
46+
main :: Effect Unit
47+
main = do
48+
runWidgetInDom "app" textFileToScreenWidget
49+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This file indicates that this recipe is temporarily broken in the Try Purescript web environment.
2+
3+
Issue: https://github.com/JordanMartinez/purescript-cookbook/issues/204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Read and Print File Contents with Concur</title>
7+
</head>
8+
9+
<body>
10+
<div id="app"></div>
11+
<script src="./index.js"></script>
12+
</body>
13+
14+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
require("../../../output/ReadPrintFileContentsConcur.Main/index.js").main();

0 commit comments

Comments
 (0)