matebeamter/src/View/Buy.hs

203 lines
6.5 KiB
Haskell
Raw Normal View History

{-# LANGUAGE PackageImports #-}
{-# LANGUAGE OverloadedStrings #-}
module View.Buy where
import Servant.Links
import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as HA
import qualified Data.Text as T
import Data.String (fromString)
import Data.Text.I18n
2019-10-24 16:05:22 +00:00
import Data.Maybe (isJust, fromJust, fromMaybe)
import Control.Monad (when)
-- imports from mateamt
import qualified "mateamt" Types as MT
-- internal imports
import Util
import Types
import View.Scaffold
import API
2019-10-22 18:43:50 +00:00
buyConfirmPage
:: L10n
-> Locale
-> Int
2019-10-22 18:43:50 +00:00
-> [(MT.PurchaseDetail, MT.ProductShortOverview)]
2019-10-23 18:35:07 +00:00
-> Int
2019-11-09 11:29:29 +00:00
-> Bool
2019-10-24 16:05:22 +00:00
-> Maybe T.Text
2019-10-22 18:43:50 +00:00
-> H.Html
2019-10-24 16:05:22 +00:00
buyConfirmPage l10n locale uid ziptups total forceBuy mcookie =
scaffold l10n locale mcookie (initPage $
translate "Matebeamter" <>
" - " <>
translate "Home"
) $ do
2019-11-09 11:29:29 +00:00
if not forceBuy
2019-10-24 16:05:22 +00:00
then
H.p $ H.toHtml $ translate "You are about to buy the following items:"
else
H.p $ H.toHtml $ translate "The following Items seem out of stock:"
H.form
H.! HA.method "post"
H.! HA.action
("/" <> fromString
(show $ linkURI $ purchaseCompleteLink uid forceBuy)
)
2019-10-24 16:05:22 +00:00
H.! HA.enctype "application/x-www-form-urlencoded"
$ do
mapM_
(\( MT.PurchaseDetail pdid amount
, MT.ProductShortOverview pid ident price _ _ mava) ->
H.div H.! HA.class_ "form-group required" $ do
H.input
H.! HA.id ("product-select-" <> fromString (show pid))
H.! HA.class_ "form-control"
H.! HA.name ("productSelect-" <> fromString (show pid))
H.! HA.type_ "hidden"
H.! HA.value "true"
H.label
H.! HA.for ("product-amount-" <> fromString (show pid))
$ do
H.img
H.!?
( isJust mava
, HA.src
(fromString $ show $ fromJust mava) -- TODO: ask for avatar
)
H.toHtml ident
H.input
2019-12-12 02:41:22 +00:00
H.! HA.id ("product-amount-" <> fromString (show pid))
2019-10-24 16:05:22 +00:00
H.! HA.class_ "form-control"
H.! HA.name ("productAmount-" <> fromString (show pid))
H.! HA.type_ "number"
H.! HA.min "0"
H.! HA.step "1"
H.! HA.value (fromString $ show amount)
-- H.! HA.disabled ""
H.label
H.! HA.for ("product-total-" <> fromString (show pid))
$ H.toHtml $
" @ " <> formatMoney price <> "" -- TODO: ask for currency symbol
H.input
2019-12-12 02:41:22 +00:00
H.! HA.id ("product-total-" <> fromString (show pid))
2019-10-24 16:05:22 +00:00
H.! HA.class_ "form-control"
H.! HA.name ("productTotal-" <> fromString (show pid))
H.! HA.type_ "text"
H.! HA.value
(fromString $ T.unpack $ formatMoney $ amount * price)
H.! HA.disabled ""
H.toHtml ("" :: T.Text) -- TODO: ask for currency symbol
)
ziptups
H.div H.! HA.class_ "form-group required" $ do
H.label
H.! HA.for "total-price"
$ H.toHtml ("Total price: " :: T.Text)
H.input
H.! HA.id "total-price"
H.! HA.class_ "form-control"
H.! HA.name "totalPrice"
H.! HA.type_ "text"
H.! HA.value (fromString $ T.unpack $ formatMoney total)
H.! HA.disabled ""
2019-11-01 11:22:36 +00:00
-- H.toHtml ("€" :: T.Text) -- TODO: ask for currency symbol
2019-10-24 16:05:22 +00:00
H.div H.! HA.class_ "form-group optional" $
H.button
H.! HA.class_ "btn btn-default"
H.! HA.type_ "submit"
2019-11-09 11:29:29 +00:00
$ H.toHtml $ if forceBuy
2019-10-24 16:05:22 +00:00
then translate "Buy anyway"
else translate "Buy"
2019-11-09 11:29:29 +00:00
when forceBuy $
2019-10-24 16:05:22 +00:00
H.button
H.! HA.formnovalidate "formnovalidate"
H.! HA.formmethod "get"
H.! HA.formaction
("/" <> fromString
(show $ linkURI $ userOverviewLink uid Nothing)
)
$ H.toHtml $ translate "Cancel"
2019-10-22 18:43:50 +00:00
where
translate = localize l10n locale . gettext
buyProductsForm
:: L10n
-> Locale
-> [MT.ProductShortOverview]
-> H.Html
buyProductsForm l10n locale prods = do
mapM_ (\(MT.ProductShortOverview pid ident price amount ml maid) ->
H.div $ do
H.div
H.! HA.class_ "form-group optional"
H.!?
( isJust maid
, productBgStyle (fromString $ show $ fromJust maid)
)
$ do
H.label
H.! HA.for ("product-select-" <> fromString (show pid)) $
H.toHtml ident
H.input
H.! HA.id ("product-select-" <> fromString (show pid))
H.! HA.class_ "form-control product-select"
H.! HA.name ("productSelect-" <> fromString (show pid))
H.! HA.type_ "checkbox"
H.! HA.value "true"
H.div
H.! HA.class_ "form-group optional"
$ do
H.label
H.! HA.for ("product-amount-" <> fromString (show pid)) $
H.toHtml (translate "Amount")
H.input
H.! HA.id ("product-amount-" <> fromString (show pid))
H.! HA.class_ "form-control product-amount"
H.! HA.name ("productAmount-" <> fromString (show pid))
H.! HA.type_ "number"
H.! HA.min "0"
H.! HA.step "1"
H.! HA.value "1"
)
prods
H.div H.! HA.class_ "form-group optional" $
H.button
H.! HA.class_ "btn btn-default"
H.! HA.type_ "submit"
2019-10-23 18:35:07 +00:00
$ H.toHtml $ translate "Proceed"
where
translate = localize l10n locale . gettext
2019-11-03 11:30:46 +00:00
cashBuyOverviewPage
:: L10n
-> Locale
-> Maybe T.Text
-> [MT.ProductShortOverview]
-> CashBuyOverviewPage
cashBuyOverviewPage l10n loc mcookie psos =
scaffold l10n loc mcookie (initPage $
translate "Matebeamter" <>
" - " <>
translate "Buy with Cash"
) $
H.form
H.! HA.method "post"
H.! HA.action ("/" <> fromString (
2019-11-09 11:29:29 +00:00
show $ linkURI cashBuyPostLink
2019-11-03 11:30:46 +00:00
))
H.! HA.enctype "application/x-www-form-urlencoded"
$ buyProductsForm l10n loc psos
where
translate = localize l10n loc . gettext