Created
February 21, 2019 06:04
-
-
Save Woody88/5476ac288d63308371a1ac844791ad50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Data.PCM where | |
| import Prelude | |
| import Apex.Internal (ApexError(..), Visualforce) | |
| import Apex.Internal as Apex | |
| import Control.Monad.Except (runExcept) | |
| import Data.Array as Array | |
| import Data.Bifunctor (lmap) | |
| import Data.Either (Either) | |
| import Data.Foldable (class Foldable) | |
| import Data.List as List | |
| import Data.Map (Map) | |
| import Data.Map as Map | |
| import Data.Tuple (Tuple(..)) | |
| import Effect.Aff (Aff) | |
| import Foreign.Class (decode, encode) | |
| import Foreign.Object (Object) | |
| import Foreign.Object as Object | |
| import Text.Parsing.CSV (Parsers, makeParsers) | |
| import Text.Parsing.Parser (ParseError, runParser) | |
| type PCMRecords = Array (Object String) | |
| type CompanyCode | |
| = { label :: String | |
| , id :: String | |
| , "type" :: String | |
| , subTitle :: String | |
| } | |
| type SalesCenterCode | |
| = { label :: String | |
| , id :: String | |
| , "type" :: String | |
| , subTitle :: String | |
| } | |
| parseCSV :: String -> Either ParseError PCMRecords | |
| parseCSV csv = (map toObject <<< toArray) <$> runParser csv windowsCSVParsers.fileHeaded | |
| where | |
| toArray = List.toUnfoldable | |
| toObject map = Object.fromFoldable $ (Map.toUnfoldableUnordered map :: Array (Tuple String String)) | |
| windowsCSVParsers :: Parsers String | |
| windowsCSVParsers = makeParsers '"' "," "\r\n" | |
| --- I want to replace this create | |
| -- createPCMRecords :: Visualforce -> PCMRecords -> Aff (Either ApexError Unit) | |
| -- createPCMRecords vf r = do | |
| -- let obj = map (\m -> Array.foldl process Object.empty $ unfoldToArray m) r | |
| -- resp <- Apex.callApex_ vf "PCMMassController.createRecords" (encode obj) { escape: true } | |
| -- let decodeUnit f = lmap (ApexError <<< show) (runExcept $ decode f) :: Either ApexError Unit | |
| -- pure (decodeUnit =<< resp) | |
| -- where | |
| -- unfoldToArray :: Map String String -> Array (Tuple String String) | |
| -- unfoldToArray = Map.toUnfoldable | |
| -- process :: Object String -> Tuple String String -> Object String | |
| -- process obj (Tuple k v) = Object.insert k v obj | |
| fetchCompanyCode :: Aff (Either ApexError (Array CompanyCode)) | |
| fetchCompanyCode = pure $ pure [{ label: "CCBJI", id: "7827", "type": "account", subTitle: mempty }, { label: "CCBJB", id: "7833", "type": "account", subTitle: mempty }, { label: "FVJ", id: "7828", "type": "account", subTitle: mempty }] | |
| fetchSalesCenterCode :: Aff (Either ApexError (Array SalesCenterCode)) | |
| fetchSalesCenterCode = pure $ pure [{ label: "JW71", id: "JW71", "type": "account", subTitle: mempty }, { label: "JW73", id: "JW73", "type": "account", subTitle: mempty }, { label: "JW74", id: "JW74", "type": "account", subTitle: mempty }] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Apex.RemoteAction where | |
| import Prelude | |
| import Apex.Internal (ApexError(..), Visualforce, callApex, callApex_) | |
| import Control.Monad.Error.Class (class MonadError, throwError) | |
| import Control.Monad.Except (runExcept) | |
| import Control.Monad.Reader (class MonadReader, ask) | |
| import Data.Bifunctor (lmap) | |
| import Data.Either (Either, either) | |
| import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol) | |
| import Effect.Aff.Class (class MonadAff, liftAff) | |
| import Foreign (Foreign) | |
| import Foreign.Class (class Decode, class Encode, decode, encode) | |
| import Type.Proxy (Proxy) | |
| import Unsafe.Coerce (unsafeCoerce) | |
| class (IsSymbol ctrl, Encode args, Decode res) | |
| <= RemoteAction act ctrl args res | |
| | act -> ctrl args res | |
| class (MonadAff m, MonadError ApexError m, MonadReader Visualforce m) | |
| <= MonadApex m | |
| invokeAction | |
| :: forall act ctrl args res m | |
| . RemoteAction act ctrl args res | |
| => MonadApex m | |
| => act | |
| -> args | |
| -> m res | |
| invokeAction _ args = do | |
| visualforce <- ask | |
| eitherResult <- liftAff $ callApex_ visualforce ctrl (encode args) {escape: true} | |
| either throwError pure (eitherResult >>= decodeResult) | |
| where | |
| ctrl = reflectSymbol $ SProxy :: _ ctrl | |
| decodeResult f = lmap (ApexError <<< show) $ runExcept <<< decode $ f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment