refactor(add-users): restructure code; add action data types
This commit is contained in:
parent
dfc017b10a
commit
ee90856b50
@ -24,6 +24,9 @@ import Control.Monad.Except (MonadError(..))
|
|||||||
import Generics.Deriving.Monoid (memptydefault, mappenddefault)
|
import Generics.Deriving.Monoid (memptydefault, mappenddefault)
|
||||||
|
|
||||||
|
|
||||||
|
type TutorialIdent = CI Text
|
||||||
|
|
||||||
|
|
||||||
data ButtonCourseRegisterMode = BtnCourseRegisterAdd | BtnCourseRegisterConfirm | BtnCourseRegisterAbort
|
data ButtonCourseRegisterMode = BtnCourseRegisterAdd | BtnCourseRegisterConfirm | BtnCourseRegisterAbort
|
||||||
deriving (Eq, Ord, Enum, Bounded, Read, Show, Generic, Typeable)
|
deriving (Eq, Ord, Enum, Bounded, Read, Show, Generic, Typeable)
|
||||||
instance Universe ButtonCourseRegisterMode
|
instance Universe ButtonCourseRegisterMode
|
||||||
@ -44,9 +47,40 @@ instance Button UniWorX ButtonCourseRegisterMode where
|
|||||||
btnValidate _ _ = True
|
btnValidate _ _ = True
|
||||||
|
|
||||||
|
|
||||||
|
data CourseRegisterAction
|
||||||
|
= CourseRegisterActionAddParticipant
|
||||||
|
| CourseRegisterActionAddTutorialMember
|
||||||
|
| CourseRegisterActionUnknownPerson
|
||||||
|
deriving (Eq, Ord, Enum, Bounded, Read, Show, Generic, Typeable)
|
||||||
|
instance Universe CourseRegisterAction
|
||||||
|
instance Finite CourseRegisterAction
|
||||||
|
|
||||||
|
data CourseRegisterActionData
|
||||||
|
= CourseRegisterActionAddParticipantData
|
||||||
|
{ crActParticipantUser :: UserId
|
||||||
|
, crActParticipantTutorial :: Maybe TutorialIdent
|
||||||
|
}
|
||||||
|
| CourseRegisterActionAddTutorialMemberData
|
||||||
|
{ crActTutorialMemberParticipant :: CourseParticipantId
|
||||||
|
, crActTutorialMemberTutorial :: TutorialIdent
|
||||||
|
}
|
||||||
|
| CourseRegisterActionUnknownPersonData -- pseudo-action; just for display
|
||||||
|
{ crActUnknownPersonIdent :: Text
|
||||||
|
}
|
||||||
|
deriving (Eq, Ord, Read, Show, Generic, Typeable)
|
||||||
|
|
||||||
|
makeLenses_ ''CourseRegisterActionData
|
||||||
|
|
||||||
|
classifyRegisterAction :: CourseRegisterActionData -> CourseRegisterAction
|
||||||
|
classifyRegisterAction = \case
|
||||||
|
CourseRegisterActionAddParticipantData{} -> CourseRegisterActionAddParticipant
|
||||||
|
CourseRegisterActionAddTutorialMemberData{} -> CourseRegisterActionAddTutorialMember
|
||||||
|
CourseRegisterActionUnknownPersonData{} -> CourseRegisterActionUnknownPerson
|
||||||
|
|
||||||
|
|
||||||
data AddUsers = AddUsers
|
data AddUsers = AddUsers
|
||||||
{ auUsers :: Set Text
|
{ auUsers :: Set Text
|
||||||
, auTutorial :: Maybe (CI Text)
|
, auTutorial :: Maybe TutorialIdent
|
||||||
} deriving (Eq, Ord, Read, Show, Generic, Typeable)
|
} deriving (Eq, Ord, Read, Show, Generic, Typeable)
|
||||||
|
|
||||||
|
|
||||||
@ -73,11 +107,10 @@ postCAddUserR tid ssh csh = do
|
|||||||
-- mr <- getMessageRender
|
-- mr <- getMessageRender
|
||||||
today <- localDay . TZ.utcToLocalTimeTZ appTZ <$> liftIO getCurrentTime
|
today <- localDay . TZ.utcToLocalTimeTZ appTZ <$> liftIO getCurrentTime
|
||||||
|
|
||||||
let
|
((usersToRegister :: FormResult AddUsers, formWgt), formEncoding) <- runFormPost . renderWForm FormStandard $ do
|
||||||
cfCommaSeparatedSet :: forall m. Functor m => Field m Text -> Field m (Set Text)
|
let
|
||||||
cfCommaSeparatedSet = guardField (not . Set.null) . convertField (Set.fromList . mapMaybe (assertM' (not . Text.null) . Text.strip) . Text.splitOn ",") (Text.intercalate ", " . Set.toList)
|
cfCommaSeparatedSet :: forall m. Functor m => Field m Text -> Field m (Set Text)
|
||||||
|
cfCommaSeparatedSet = guardField (not . Set.null) . convertField (Set.fromList . mapMaybe (assertM' (not . Text.null) . Text.strip) . Text.splitOn ",") (Text.intercalate ", " . Set.toList)
|
||||||
((usersToRegister, formWgt), formEncoding) <- runFormPost . renderWForm FormStandard $ do
|
|
||||||
auUsers <- wreq (textField & cfCommaSeparatedSet) (fslI MsgCourseParticipantsRegisterUsersField & setTooltip MsgCourseParticipantsRegisterUsersFieldTip) mempty
|
auUsers <- wreq (textField & cfCommaSeparatedSet) (fslI MsgCourseParticipantsRegisterUsersField & setTooltip MsgCourseParticipantsRegisterUsersFieldTip) mempty
|
||||||
auTutorial <- optionalActionW
|
auTutorial <- optionalActionW
|
||||||
( areq (textField & cfCI) (fslI MsgCourseParticipantsRegisterTutorialField & setTooltip MsgCourseParticipantsRegisterTutorialFieldTip) (Just . CI.mk $ tshow today) ) -- TODO: use user date display setting
|
( areq (textField & cfCI) (fslI MsgCourseParticipantsRegisterTutorialField & setTooltip MsgCourseParticipantsRegisterTutorialFieldTip) (Just . CI.mk $ tshow today) ) -- TODO: use user date display setting
|
||||||
@ -92,9 +125,9 @@ postCAddUserR tid ssh csh = do
|
|||||||
= CourseR tid ssh csh CUsersR
|
= CourseR tid ssh csh CUsersR
|
||||||
formResultModal usersToRegister dest $ \AddUsers{..} -> hoist runDBJobs $ do
|
formResultModal usersToRegister dest $ \AddUsers{..} -> hoist runDBJobs $ do
|
||||||
avsUsers :: Map Text (Maybe UserId) <- sequenceA . flip Map.fromSet auUsers $ liftHandler . upsertAvsUser
|
avsUsers :: Map Text (Maybe UserId) <- sequenceA . flip Map.fromSet auUsers $ liftHandler . upsertAvsUser
|
||||||
let retrievedUsers = catMaybes $ Map.elems avsUsers
|
case catMaybes $ Map.elems avsUsers of
|
||||||
if
|
[] -> tell . pure =<< messageI Error MsgCourseParticipantsRegisterNoneGiven
|
||||||
| uids@(_:_) <- retrievedUsers -> do
|
uids -> do
|
||||||
registerUsers cid avsUsers
|
registerUsers cid avsUsers
|
||||||
for_ auTutorial $ \tutorialName -> lift $ do
|
for_ auTutorial $ \tutorialName -> lift $ do
|
||||||
-- TODO: move somewhere else
|
-- TODO: move somewhere else
|
||||||
@ -125,8 +158,6 @@ postCAddUserR tid ssh csh = do
|
|||||||
}
|
}
|
||||||
[]
|
[]
|
||||||
-- tell . pure <=< messageI Success . MsgCourseParticipantsRegisteredTutorial $ length uids
|
-- tell . pure <=< messageI Success . MsgCourseParticipantsRegisteredTutorial $ length uids
|
||||||
| otherwise
|
|
||||||
-> tell . pure =<< messageI Error MsgCourseParticipantsRegisterNoneGiven
|
|
||||||
|
|
||||||
let heading = prependCourseTitle tid ssh csh MsgCourseParticipantsRegisterHeading
|
let heading = prependCourseTitle tid ssh csh MsgCourseParticipantsRegisterHeading
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user