MFlow  Create and maintain dynamic Web applications as easy and fast as console applications
Thou shall not write request handlers
 This release: 11/06/2014.  agocorona@gmail.com (@agocorona) <>< . issues & bugs .  Mflow Source code,     source code of this site




Using Amazon Web Services with TCache and the Rich Text editor


The code of this example is identical to any one for persistence in files that you can find here except the procedure  amazonSDBpersist that set the default persistence in Amazon simpleDB. It also shows how to edit HTML online with htmlEdit primitive, that creates a rich text editor out of a text input form. In this case, it is configured with bold and italic controls.

Here is the permanent link for the running example (This page)

http://mflowdemo.herokuapp.com/noscript/database

It also shows how to edit HTML with htmlEdit. It is a widget modifier that convert an input box or a  text box into a rich text editor 


Running example

(in the light red box):

Insert the text




Source code:

{-# LANGUAGE DeriveDataTypeable, RecordWildCards
           , OverloadedStrings, StandaloneDeriving
           , ScopedTypeVariables, CPP #-}
module Database where

import Data.Typeable
import Data.TCache.IndexQuery
import Data.TCache.DefaultPersistence
import Data.TCache.AWS
import Data.Monoid
import qualified Data.Text as T
import Data.String
import Data.ByteString.Lazy.Char8 hiding (index)


-- #define ALONE -- to execute it alone, uncomment this
#ifdef ALONE
import MFlow.Wai.Blaze.Html.All
main= do
  syncWrite  $ Asyncronous 120 defaultCheck  1000
  index idnumber
  runNavigation "" $ transientNav grid
#else
import MFlow.Wai.Blaze.Html.All hiding(select, page)
import Menu
#endif



-- to run it alone,  remove Menu.hs and uncomment this:

--askm= ask
--
--main= do
--  syncWrite  $ Asyncronous 120 defaultCheck  1000
--  index idnumber
--  runNavigation "" $ step database

data  MyData= MyData{idnumber :: Int, textdata :: T.Text} deriving (Typeable, Read, Show)  -- that is enough for file persistence
instance Indexable MyData where
   key=  show . idnumber    -- the key of the register


domain= "mflowdemo"

instance  Serializable MyData where
  serialize=  pack . show
  deserialize=  read . unpack
  setPersist =  const . Just $ amazonS3Persist domain -- False
 
data Options= NewText | Exit deriving (Show, Typeable)


     
database= do
     liftIO $ index idnumber
     database'


database'= do
     all <- allTexts

     r <- page $ listtexts all

     case r of
         NewText -> do
              text <- page $   p "Insert the text"
                           ++> htmlEdit ["bold","italic"] ""  -- rich text editor with bold and italic buttons
                                        (getMultilineText "" <! [("rows","3"),("cols","80")]) <++ br
                           <** submitButton "enter"

              addtext all text  -- store the name in the cache (later will be written to disk automatically)
              database' 

         Exit -> return ()
     where
     menu= wlink NewText   << p "enter a new text" <|>
           wlink Exit      << p "exit to the home page"

     listtexts all  =  do
           h3 "list of all texts"
           ++> mconcat[p $ preEscapedToHtml t >> hr | t <- all]
           ++> menu
           <++ b "or press the back button or enter the  URL any other page in the web site"

     addtext all text= liftIO . atomically . newDBRef $ MyData (Prelude.length all) text
     allTexts= liftIO . atomically . select textdata $ idnumber .>=. (0 :: Int)


comments powered by Disqus