genericUpdateViewOf :: (Show d, Data d) => d -> Template -> ServerPart Response
genericUpdateViewOf value template = msum [ viewForm, processForm ]
  where
    viewForm = 
        let
            ValueInfo name fields values = extractInfo value
            toField k v = 
                H.div ! A.class_ "clearfix" $ do
                    H.label ! A.for ka $ kh
                    H.div ! A.class_ "input" $ H.input ! A.type_ "text" ! A.name ka ! A.id ka ! A.value va
                where kh = H.toHtml k
                      ka = H.toValue k
                      va = H.toValue v
        in
            do  method GET
                ok $ template $ do
                    H.div ! A.class_ "row" $ H.form ! A.method "post" $ H.fieldset $ do
                            H.legend $ H.toHtml name
                            zipWithM_ toField fields values
                            H.div ! A.class_ "actions" $ H.input ! A.class_ "btn primary" ! A.type_ "submit" ! A.value "save"
    processForm = do
        method POST
        let ValueInfo name fields values = extractInfo value
        mapM_ (\f -> lookText f >>= \x -> liftIO $ print x) fields
        genericReadViewOf value template []

-- genericListOf
-- genericDeleteOf

-- TODO: work out how to abstract over the Update & Query types from acid-state
-- Are these sensible types?
{-
type CreateData = Data d => ServerPart d
type UpdateData = Data d => ServerPart d -> ServerPart d
type DeleteData = Data d => ServerPart ()
-}

-- Is it possible to do something like this or do I need template haskell?
-- genericCrudOf :: String -> CreateData -> UpdateData -> DeleteData ->
{-
genericCrudOf prefix create update delete = msum
    [
        dir $ prefix ++ "/create" $ genericCreateOf 
        dir $ prefix ++ "/read" $ genericReadOf 
        dir $ prefix ++ "/update" $ genericUpdateOf 
        dir $ prefix ++ "/list" $ genericListOf 
    ]
-}
-- Utility Functions
genericUpdateViewOf :: (Show d, Data d) => d -> Template -> ServerPart Response
genericUpdateViewOf value template = msum [ viewForm, processForm ]
  where
    viewForm = 
        let
            ValueInfo name fields values = extractInfo value
            toField k v = 
                H.div ! A.class_ "clearfix" $ do
                    H.label ! A.for ka $ kh
                    H.div ! A.class_ "input" $ H.input ! A.type_ "text" ! A.name ka ! A.id ka ! A.value va
                where kh = H.toHtml k
                      ka = H.toValue k
                      va = H.toValue v
        in
            do  method GET
                ok $ template $ do
                    H.div ! A.class_ "row" $ H.form ! A.method "post" $ H.fieldset $ do
                            H.legend $ H.toHtml name
                            zipWithM_ toField fields values
                            H.div ! A.class_ "actions" $ H.input ! A.class_ "btn primary" ! A.type_ "submit" ! A.value "save"
    processForm = do
        method POST
        let ValueInfo name fields values = extractInfo value
        mapM_ (\f -> lookText f >>= \x -> liftIO $ print x) fields
        genericReadViewOf value template []

-- genericListOf
-- genericDeleteOf

-- TODO: work out how to abstract over the Update & Query types from acid-state
-- Are these sensible types?
{-
type CreateData = Data d => ServerPart d
type UpdateData = Data d => ServerPart d -> ServerPart d
type DeleteData = Data d => ServerPart ()
-}

-- Is it possible to do something like this or do I need template haskell?
-- genericCrudOf :: String -> CreateData -> UpdateData -> DeleteData ->
{-
genericCrudOf prefix create update delete = msum
    [
        dir $ prefix ++ "/create" $ genericCreateOf 
        dir $ prefix ++ "/read" $ genericReadOf 
        dir $ prefix ++ "/update" $ genericUpdateOf 
        dir $ prefix ++ "/list" $ genericListOf 
    ]
-}
-- Utility Functions
genericUpdateViewOf value template = msum [ viewForm, processForm ]
  where
    viewForm = 
        let
            ValueInfo name fields values = extractInfo value
            toField k v = 
                H.div ! A.class_ "clearfix" $ do
                    H.label ! A.for ka $ kh
                    H.div ! A.class_ "input" $ H.input ! A.type_ "text" ! A.name ka ! A.id ka ! A.value va
                where kh = H.toHtml k
                      ka = H.toValue k
                      va = H.toValue v
        in
            do  method GET
                ok $ template $ do
                    H.div ! A.class_ "row" $ H.form ! A.method "post" $ H.fieldset $ do
                            H.legend $ H.toHtml name
                            zipWithM_ toField fields values
                            H.div ! A.class_ "actions" $ H.input ! A.class_ "btn primary" ! A.type_ "submit" ! A.value "save"
    processForm = do
        method POST
        let ValueInfo name fields values = extractInfo value
        mapM_ (\f -> lookText f >>= \x -> liftIO $ print x) fields
        genericReadViewOf value template []

-- genericListOf
-- genericDeleteOf

-- TODO: work out how to abstract over the Update & Query types from acid-state
-- Are these sensible types?
{-
type CreateData = Data d => ServerPart d
type UpdateData = Data d => ServerPart d -> ServerPart d
type DeleteData = Data d => ServerPart ()
-}

-- Is it possible to do something like this or do I need template haskell?
-- genericCrudOf :: String -> CreateData -> UpdateData -> DeleteData ->
{-
genericCrudOf prefix create update delete = msum
    [
        dir $ prefix ++ "/create" $ genericCreateOf 
        dir $ prefix ++ "/read" $ genericReadOf 
        dir $ prefix ++ "/update" $ genericUpdateOf 
        dir $ prefix ++ "/list" $ genericListOf 
    ]
-}
-- Utility Functions