personFormSplices
  :: PhbRuntimeSplice (Maybe (Entity Person,Maybe (Entity PersonLogin)))
  -> PhbSplice
personFormSplices rts = do
  promise <- C.newEmptyPromise
  out <- C.withSplices
         C.runChildren
         ("personForm" ## formSplice mempty mempty)
         (C.getPromise promise)
  pure . C.yieldRuntime $ do
    e  <- rts
    ct <- liftIO getCurrentTime
    (v, result) <- lift $ runForm "person" $ personForm e

    case result of
      Just x  -> do
        lift (createPerson x ct (e ^?_Just._1.eKey) (e ^?_Just._2._Just.eKey))
      Nothing -> C.putPromise promise v >> C.codeGen out
  where
    createPerson x ct pkMay plkMay = do
      -- Should probably change this so that a DB error wouldn't just
      -- Crash us and should put a nice error in the form.
      case pkMay of
       Nothing -> do
         void . runPersist $ do
           pk <- insert (newPerson x)
           traverse (createLogin ct pk plkMay) (x^.personInputLoginName)
         flashSuccess $ "Person Created"
       Just pk  -> do
         void . runPersist $ do
           replace pk (newPerson x)
           case x^.personInputLoginName of
             Nothing  -> traverse_ delete plkMay
             Just ln  -> createLogin ct pk plkMay ln
         flashSuccess $ "Person Updated"
      redirect "/people"
    createLogin _ _ (Just plk) ln = update plk [PersonLoginLogin =. ln]
    createLogin ct pk _ ln        = void . insert $ PersonLogin
      pk
      ln
      Nothing
      Nothing
      Nothing
      0
      0
      Nothing
      Nothing
      Nothing
      Nothing
      Nothing
      ct
      ct
      Nothing
      Nothing
      ""
      ""

    newPerson (PersonInput n e d _ rh lt) = Person n e d rh lt
personFormSplices
  :: PhbRuntimeSplice (Maybe (Entity Person,Maybe (Entity PersonLogin)))
  -> PhbSplice
personFormSplices rts = do
  promise <- C.newEmptyPromise
  out <- C.withSplices
         C.runChildren
         ("personForm" ## formSplice mempty mempty)
         (C.getPromise promise)
  pure . C.yieldRuntime $ do
    e  <- rts
    ct <- liftIO getCurrentTime
    (v, result) <- lift $ runForm "person" $ personForm e

    case result of
      Just x  -> do
        lift (createPerson x ct (e ^?_Just._1.eKey) (e ^?_Just._2._Just.eKey))
      Nothing -> C.putPromise promise v >> C.codeGen out
  where
    createPerson x ct pkMay plkMay = do
      -- Should probably change this so that a DB error wouldn't just
      -- Crash us and should put a nice error in the form.
      case pkMay of
       Nothing -> do
         void . runPersist $ do
           pk <- insert (newPerson x)
           traverse (createLogin ct pk plkMay) (x^.personInputLoginName)
         flashSuccess $ "Person Created"
       Just pk  -> do
         void . runPersist $ do
           replace pk (newPerson x)
           case x^.personInputLoginName of
             Nothing  -> traverse_ delete plkMay
             Just ln  -> createLogin ct pk plkMay ln
         flashSuccess $ "Person Updated"
      redirect "/people"
    createLogin _ _ (Just plk) ln = update plk [PersonLoginLogin =. ln]
    createLogin ct pk _ ln        = void . insert $ PersonLogin
      pk
      ln
      Nothing
      Nothing
      Nothing
      0
      0
      Nothing
      Nothing
      Nothing
      Nothing
      Nothing
      ct
      ct
      Nothing
      Nothing
      ""
      ""

    newPerson (PersonInput n e d _ rh lt) = Person n e d rh lt
personFormSplices rts = do
  promise <- C.newEmptyPromise
  out <- C.withSplices
         C.runChildren
         ("personForm" ## formSplice mempty mempty)
         (C.getPromise promise)
  pure . C.yieldRuntime $ do
    e  <- rts
    ct <- liftIO getCurrentTime
    (v, result) <- lift $ runForm "person" $ personForm e

    case result of
      Just x  -> do
        lift (createPerson x ct (e ^?_Just._1.eKey) (e ^?_Just._2._Just.eKey))
      Nothing -> C.putPromise promise v >> C.codeGen out
  where
    createPerson x ct pkMay plkMay = do
      -- Should probably change this so that a DB error wouldn't just
      -- Crash us and should put a nice error in the form.
      case pkMay of
       Nothing -> do
         void . runPersist $ do
           pk <- insert (newPerson x)
           traverse (createLogin ct pk plkMay) (x^.personInputLoginName)
         flashSuccess $ "Person Created"
       Just pk  -> do
         void . runPersist $ do
           replace pk (newPerson x)
           case x^.personInputLoginName of
             Nothing  -> traverse_ delete plkMay
             Just ln  -> createLogin ct pk plkMay ln
         flashSuccess $ "Person Updated"
      redirect "/people"
    createLogin _ _ (Just plk) ln = update plk [PersonLoginLogin =. ln]
    createLogin ct pk _ ln        = void . insert $ PersonLogin
      pk
      ln
      Nothing
      Nothing
      Nothing
      0
      0
      Nothing
      Nothing
      Nothing
      Nothing
      Nothing
      ct
      ct
      Nothing
      Nothing
      ""
      ""

    newPerson (PersonInput n e d _ rh lt) = Person n e d rh lt