|
| 1 | +module Tutorial where |
| 2 | + |
| 3 | + -- This is a mostly PureScript implementation of the tutorial found here: |
| 4 | + -- http://facebook.github.io/react/docs/tutorial.html |
| 5 | + |
| 6 | + import Control.Monad.Eff |
| 7 | + import Data.Array |
| 8 | + import React |
| 9 | + import Showdown |
| 10 | + |
| 11 | + import qualified React.DOM as DOM |
| 12 | + |
| 13 | + cBoxRender = do |
| 14 | + state <- readState |
| 15 | + pure $ DOM.div { className: "commentBox" } |
| 16 | + [ DOM.h1 {} [ DOM.text "Comments" ] |
| 17 | + , commentList { data': state } |
| 18 | + , commentForm { onCommentSubmit: handle commentSubmit } |
| 19 | + ] |
| 20 | + foreign import initialState |
| 21 | + "function initialState() { return {state: []}; }" :: forall a b. a -> b |
| 22 | + |
| 23 | + commentBox = mkStatefulUIFromSpec $ |
| 24 | + defaultStatefulSpec { getInitialState = initialState |
| 25 | + , componentWillMount = componentWillMount |
| 26 | + , render = cBoxRender |
| 27 | + } |
| 28 | + |
| 29 | + commentList = mkUI do |
| 30 | + props <- getProps |
| 31 | + pure $ DOM.div { className: "commentList" } |
| 32 | + (commentNodes <$> props.data') |
| 33 | + |
| 34 | + commentForm = mkUI do |
| 35 | + props <- getProps |
| 36 | + pure $ DOM.form { className: "commentForm" |
| 37 | + , onSubmit: handle submit |
| 38 | + } |
| 39 | + [ DOM.input { attrType: "text" |
| 40 | + , placeholder: "Your name" |
| 41 | + , ref: "author" |
| 42 | + } |
| 43 | + [] |
| 44 | + , DOM.input { attrType: "text" |
| 45 | + , placeholder: "Say something..." |
| 46 | + , ref: "text" |
| 47 | + } |
| 48 | + [] |
| 49 | + , DOM.input { attrType: "submit" |
| 50 | + , value: "Post" |
| 51 | + } |
| 52 | + [] |
| 53 | + ] |
| 54 | + |
| 55 | + comment = mkUI do |
| 56 | + props <- getProps |
| 57 | + pure $ DOM.div { className: "comment" } |
| 58 | + [ DOM.h2 { className: "commentAuthor" } |
| 59 | + [ DOM.text props.author ] |
| 60 | + , DOM.span { dangerouslySetInnerHTML: { __html: makeHtml props.children } } |
| 61 | + [] |
| 62 | + ] |
| 63 | + |
| 64 | + commentNodes c = comment { author: c.author, children: c.text } |
| 65 | + |
| 66 | + foreign import commentSubmit |
| 67 | + "function commentSubmit(comment) {\ |
| 68 | + \ var comments = this.state.state;\ |
| 69 | + \ var newComments = comments.concat([comment]);\ |
| 70 | + \ this.setState({state: newComments});\ |
| 71 | + \ $.ajax({\ |
| 72 | + \ url: this.props.url,\ |
| 73 | + \ dataType: 'json',\ |
| 74 | + \ type: 'POST',\ |
| 75 | + \ data: comment,\ |
| 76 | + \ success: function(data) {\ |
| 77 | + \ this.setState({state: data});\ |
| 78 | + \ }.bind(this)\ |
| 79 | + \ });\ |
| 80 | + \}" :: forall eff. Eff eff {} |
| 81 | + |
| 82 | + foreign import submit |
| 83 | + "function submit() {\ |
| 84 | + \ var author = this.refs.author.getDOMNode().value.trim();\ |
| 85 | + \ var text = this.refs.text.getDOMNode().value.trim();\ |
| 86 | + \ this.props.onCommentSubmit.call(this, {author: author, text: text});\ |
| 87 | + \ this.refs.author.getDOMNode().value = '';\ |
| 88 | + \ this.refs.text.getDOMNode().value = '';\ |
| 89 | + \ return false;\ |
| 90 | + \}" :: forall eff. Eff eff Boolean |
| 91 | + |
| 92 | + foreign import loadCommentsFromServer |
| 93 | + "function loadCommentsFromServer() {\ |
| 94 | + \ $.ajax({\ |
| 95 | + \ url: this.props.url,\ |
| 96 | + \ dataType: 'json',\ |
| 97 | + \ success: function(data) {\ |
| 98 | + \ this.replaceState({state: data});\ |
| 99 | + \ }.bind(this)\ |
| 100 | + \ });\ |
| 101 | + \}" :: forall a r. {props :: {url :: String}, replaceState :: {state :: a} -> {} | r} -> {} |
| 102 | + |
| 103 | + foreign import componentWillMount |
| 104 | + "function componentWillMount() {\ |
| 105 | + \ var load = loadCommentsFromServer.bind(this);\ |
| 106 | + \ load();\ |
| 107 | + \ setInterval(function() { load(); }, this.props.pollInterval);\ |
| 108 | + \}" :: forall r. {} -> {} |
| 109 | + |
| 110 | + main = renderToElementById "content" $ commentBox { url: "comments.json" |
| 111 | + , pollInterval: 2000 |
| 112 | + } |
0 commit comments