Skip to content

Commit

Permalink
Allow logout from the Programs popup.
Browse files Browse the repository at this point in the history
  • Loading branch information
toddburnside committed Oct 13, 2023
1 parent 7e3b5d3 commit 207284a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 41 deletions.
28 changes: 25 additions & 3 deletions explore/src/main/scala/explore/ExploreLayout.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import explore.events.ExploreEvent.LogoutEventId
import explore.model.AppContext
import explore.model.*
import explore.model.enums.AppTab
import explore.programs.ProgramsPopup
import explore.shortcuts.*
import explore.shortcuts.given
import explore.utils.*
Expand Down Expand Up @@ -217,9 +218,30 @@ object ExploreLayout:
ctx.pageUrl(_, routingInfo.programId, routingInfo.focused),
_.separatorAfter
),
<.div(LayoutStyles.MainBody)(
props.resolution.renderP(props.view)
)
<.div(LayoutStyles.MainBody) {
val (showProgsPopup, msg) =
props.view.get.programSummaries.fold((false, none)) { pss =>
routingInfo.optProgramId.fold((true, none)) { id =>
if (pss.programs.get(id).exists(!_.deleted)) (false, none)
else
(true,
s"The program id in the url, '$id', either does not exist, is deleted, or you do not have authorization to view it.".some
)
}
}
if (showProgsPopup)
ProgramsPopup(
currentProgramId = none,
props.view
.zoom(RootModel.programSummaries.some)
.zoom(ProgramSummaries.programs),
undoStacks = props.view.zoom(RootModel.undoStacks),
onLogout = (onLogout >>
props.view.zoom(RootModel.vault).set(none).toAsync).some,
message = msg
): VdomElement
else props.resolution.renderP(props.view)
}
)
)
)
50 changes: 15 additions & 35 deletions explore/src/main/scala/explore/Routing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ package explore

import cats.Order.*
import cats.data.NonEmptySet
import cats.effect.IO
import cats.syntax.all.*
import crystal.*
import crystal.react.View
import explore.model.Page
import explore.model.Page.*
import explore.model.*
import explore.programs.ProgramsPopup
import explore.proposal.ProposalTabContents
import explore.tabs.ConstraintsTabContents
import explore.tabs.ProgramTabContents
import explore.tabs.*
import explore.undo.UndoContext
import japgolly.scalajs.react.React
import japgolly.scalajs.react.ReactMonocle.*
import japgolly.scalajs.react.extra.router.*
import japgolly.scalajs.react.vdom.VdomElement
Expand All @@ -35,27 +34,13 @@ import scala.util.Random

object Routing:

private def withProgramSummaries(pid: Option[Program.Id], model: View[RootModel])(
private def withProgramSummaries(model: View[RootModel])(
render: UndoContext[ProgramSummaries] => VdomNode
): VdomElement =
model
.zoom(RootModel.programSummaries)
.mapValue { (pss: View[ProgramSummaries]) =>
val (showProgsPopup, msg) = pid.fold((true, none)) { id =>
if (pss.get.programs.get(id).exists(!_.deleted)) (false, none)
else
(true,
s"The program id in the url, '$id', either does not exist, is deleted, or you do not have authorization to view it.".some
)
}
if (showProgsPopup)
ProgramsPopup(
currentProgramId = none,
pss.zoom(ProgramSummaries.programs).asViewOpt,
undoStacks = model.zoom(RootModel.undoStacks),
message = msg
): VdomElement
else render(UndoContext(model.zoom(RootModel.undoStacks), pss))
render(UndoContext(model.zoom(RootModel.undoStacks), pss))
}
.toPot
.renderPot(identity)
Expand All @@ -68,7 +53,7 @@ object Routing:

private def overviewTab(page: Page, model: View[RootModel]): VdomElement =
val routingInfo = RoutingInfo.from(page)
withProgramSummaries(routingInfo.programId.some, model)(programSummaries =>
withProgramSummaries(model)(programSummaries =>
OverviewTabContents(
routingInfo.programId,
model.zoom(RootModel.vault).get,
Expand All @@ -80,7 +65,7 @@ object Routing:

private def targetTab(page: Page, model: View[RootModel]): VdomElement =
val routingInfo = RoutingInfo.from(page)
withProgramSummaries(routingInfo.programId.some, model)(programSummaries =>
withProgramSummaries(model)(programSummaries =>
model
.zoom(RootModel.userPreferences)
.mapValue(userPrefs =>
Expand All @@ -98,7 +83,7 @@ object Routing:

private def obsTab(page: Page, model: View[RootModel]): VdomElement =
val routingInfo = RoutingInfo.from(page)
withProgramSummaries(routingInfo.programId.some, model)(programSummaries =>
withProgramSummaries(model)(programSummaries =>
model
.zoom(RootModel.userPreferences)
.mapValue(userPrefs =>
Expand All @@ -117,7 +102,7 @@ object Routing:

private def constraintSetTab(page: Page, model: View[RootModel]): VdomElement =
val routingInfo = RoutingInfo.from(page)
withProgramSummaries(routingInfo.programId.some, model)(programSummaries =>
withProgramSummaries(model)(programSummaries =>
ConstraintsTabContents(
model.zoom(RootModel.userId).get,
routingInfo.programId,
Expand All @@ -129,8 +114,7 @@ object Routing:
)

private def schedulingTab(page: Page, model: View[RootModel]): VdomElement =
val routingInfo = RoutingInfo.from(page)
withProgramSummaries(routingInfo.programId.some, model)(programSummaries =>
withProgramSummaries(model)(programSummaries =>
val routingInfo = RoutingInfo.from(page)
SchedulingTabContents(
model.zoom(RootModel.userId).get,
Expand All @@ -144,13 +128,10 @@ object Routing:

private def proposalTab(page: Page, model: View[RootModel]): VdomElement =
val routingInfo = RoutingInfo.from(page)
// we don't need the summaries, but we still want to validate the progam id
withProgramSummaries(routingInfo.programId.some, model)(_ =>
ProposalTabContents(
routingInfo.programId,
model.zoom(RootModel.user).get,
model.zoom(RootModel.otherUndoStacks).zoom(ModelUndoStacks.forProposal)
)
ProposalTabContents(
routingInfo.programId,
model.zoom(RootModel.user).get,
model.zoom(RootModel.otherUndoStacks).zoom(ModelUndoStacks.forProposal)
)

private def programTab(page: Page, model: View[RootModel]): VdomElement =
Expand All @@ -161,9 +142,8 @@ object Routing:
userPreferences(model)
)

private def showProgramSelectionPopup(model: View[RootModel]): VdomElement =
// Because we are not supplying a program id, the ProgramsPopup will be displayed
withProgramSummaries(none, model)(_ => <.div("Programmer error!"))
// The programs popup will be shown
private def noProgram: VdomElement = React.Fragment()

def config: RouterWithPropsConfig[Page, View[RootModel]] =
RouterWithPropsConfigDsl[Page, View[RootModel]].buildConfig: dsl =>
Expand All @@ -189,7 +169,7 @@ object Routing:

val rules =
(emptyRule
| staticRoute(root, NoProgramPage) ~> renderP(showProgramSelectionPopup _)
| staticRoute(root, NoProgramPage) ~> render(noProgram)

| dynamicRouteCT((root / id[Program.Id]).xmapL(HomePage.iso)) ~> dynRenderP {
case (p, m) => overviewTab(p, m)
Expand Down
16 changes: 14 additions & 2 deletions explore/src/main/scala/explore/programs/ProgramTable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ case class ProgramTable(
programInfos: View[ProgramInfoList],
selectProgram: Program.Id => Callback,
isRequired: Boolean,
onClose: Option[Callback]
onClose: Option[Callback],
onLogout: Option[IO[Unit]]
) extends ReactFnProps(ProgramTable.component)

object ProgramTable:
Expand Down Expand Up @@ -194,6 +195,16 @@ object ProgramTable:
).small.compact.some
)

val logoutButton =
props.onLogout.fold(none)(io =>
Button(
label = "Logout",
icon = Icons.Logout,
severity = Button.Severity.Danger,
onClick = (ctx.sso.logout >> io).runAsync
).small.compact.some
)

<.div(
React.Fragment(
<.div(ExploreStyles.ProgramTable)(
Expand All @@ -220,7 +231,8 @@ object ProgramTable:
value = showDeleted.zoom(ShowDeleted.value.asLens),
label = "Show deleted"
),
closeButton
closeButton,
logoutButton
)
)
)
Expand Down
4 changes: 3 additions & 1 deletion explore/src/main/scala/explore/programs/ProgramsPopup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ case class ProgramsPopup(
programInfos: ViewOpt[ProgramInfoList],
undoStacks: View[UndoStacks[IO, ProgramSummaries]],
onClose: Option[Callback] = none,
onLogout: Option[IO[Unit]] = none,
message: Option[String] = none
) extends ReactFnProps(ProgramsPopup.component)

Expand Down Expand Up @@ -78,7 +79,8 @@ object ProgramsPopup {
pis,
selectProgram = selectProgram(props.onClose, props.undoStacks, ctx),
props.onClose.isEmpty,
onHide
onHide,
props.onLogout
)
)
.toPot
Expand Down

0 comments on commit 207284a

Please sign in to comment.