Skip to content

Commit

Permalink
unify code, action buttons working as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiaggio committed Oct 25, 2024
1 parent 47c7489 commit c3eb1ad
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 42 deletions.
29 changes: 7 additions & 22 deletions explore/src/main/scala/explore/observationtree/ObsActions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,43 +145,28 @@ object ObsActions:
singleObsSetter(obsId)(obsOpt)(acc)
}

def deleteObservations(
def obsExistence(
obsIds: List[Observation.Id],
setSummary: IO[Unit] = IO.unit,
focusObs: Observation.Id => Callback = _ => Callback.empty,
postMessage: String => IO[Unit] = _ => IO.unit
)(using
c: FetchClient[IO, ObservationDB]
): Action[ObservationList, List[Option[(Observation, NonNegInt)]]] =
Action(getter = obsListGetter(obsIds), setter = obsListSetter(obsIds))(
onSet = (_, loObs) =>
loObs.sequence.fold(ObsQueries.deleteObservations[IO](obsIds))(_ =>
ObsQueries.undeleteObservations[IO](obsIds)
),
onRestore = (_, loObs) =>
loObs.sequence.fold(
ObsQueries.deleteObservations[IO](obsIds) >> setSummary >>
postMessage(s"Deleted ${obsIds.length} observation(s)")
)(_ =>
ObsQueries.undeleteObservations[IO](obsIds) >> setSummary >>
postMessage(s"Restored ${obsIds.length} observation(s)")
)
)

def obsExistence(obsIds: List[Observation.Id], focusObs: Observation.Id => Callback)(using
FetchClient[IO, ObservationDB]
): Action[ObservationList, List[Option[obsListMod.ElemWithIndex]]] =
Action(getter = obsListGetter(obsIds), setter = obsListSetter(obsIds))(
onSet = (_, elemWithIndexListOpt) =>
elemWithIndexListOpt.sequence.fold(
ObsQueries.deleteObservations[IO](obsIds)
ObsQueries.deleteObservations[IO](obsIds) >>
postMessage(s"Deleted ${obsIds.length} observation(s)")
)(obsList => // Not much to do here, the observation must be created before we get here
obsList.headOption.map(_._1).foldMap(obs => focusObs(obs.id).toAsync)
),
onRestore = (_, elemWithIndexOpt) =>
elemWithIndexOpt.sequence.fold(
ObsQueries.deleteObservations[IO](obsIds)
ObsQueries.deleteObservations[IO](obsIds) >>
postMessage(s"Deleted ${obsIds.length} observation(s)")
)(obsList =>
ObsQueries.undeleteObservations[IO](obsIds) >>
postMessage(s"Restored ${obsIds.length} observation(s)") >>
obsList.headOption.map(_._1).foldMap(obs => focusObs(obs.id).toAsync)
)
)
30 changes: 18 additions & 12 deletions explore/src/main/scala/explore/observationtree/ObsList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ import lucuma.core.syntax.display.*
import lucuma.react.common.Css
import lucuma.react.common.ReactFnProps
import lucuma.react.primereact.Button
import lucuma.react.primereact.ConfirmDialog
import lucuma.react.primereact.DialogPosition
import lucuma.react.primereact.PrimeStyles
import lucuma.react.primereact.Tree
import lucuma.react.primereact.Tree.Node
import lucuma.typed.primereact.treeTreeMod.TreeNodeTemplateOptions
Expand Down Expand Up @@ -261,14 +264,20 @@ object ObsList:
dropNodeId.map(id => props.expandedGroups.mod(_ + id)).getOrEmpty
}

val deleteObs: Observation.Id => Callback = oid =>
ObsActions
.obsExistence(
List(oid),
o => focusObs(props.programId, o.some, ctx)
val deleteObsList: List[Observation.Id] => Callback =
selectedObsIds =>
ConfirmDialog.confirmDialog(
message = <.div(s"This action will delete ${props.selectedText.orEmpty}."),
header = "Observations delete",
acceptLabel = "Yes, delete",
position = DialogPosition.Top,
accept = ObsActions
.obsExistence(selectedObsIds, postMessage = ToastCtx[IO].showToast(_))
.mod(props.observations)(_ => selectedObsIds.map(_ => none)),
acceptClass = PrimeStyles.ButtonSmall,
rejectClass = PrimeStyles.ButtonSmall,
icon = Icons.SkullCrossBones(^.color.red)
)
.mod(props.observations)(_ => List(none))
.showToastCB(s"Deleted obs ${oid.shortName}")

val deleteGroup: Group.Id => Callback = gid =>
ObsActions
Expand Down Expand Up @@ -321,7 +330,7 @@ object ObsList:
.set(props.observations)
.compose((_: Option[NonEmptyString]).some)
.some,
deleteCB = deleteObs(obsId),
deleteCB = deleteObsList(List(obsId)),
cloneCB = cloneObs(
props.programId,
List(obsId),
Expand Down Expand Up @@ -428,10 +437,7 @@ object ObsList:
),
ActionButtons.ButtonProps(
props.selectedObsIdSet
.map: obsIdSet =>
ObsActions
.deleteObservations(obsIdSet.idSet.toList)
.set(props.observations)(obsIdSet.idSet.toList.map(_ => none))
.map(obsIdSet => deleteObsList(obsIdSet.idSet.toList))
.orElse(props.focusedGroup.map(deleteGroup))
.orEmpty,
disabled = props.deleteDisabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package explore.observationtree
import cats.effect.IO
import cats.syntax.all.*
import clue.FetchClient
import crystal.react.*
import explore.components.ui.ExploreStyles
import explore.model.AppContext
import explore.model.ObsIdSet
Expand Down Expand Up @@ -113,12 +112,8 @@ trait ViewCommon {
ObsActions
.obsExistence(
List(obsId),
oid =>
ToastCtx[IO].showToast(s"Restore deleted obs: ${obsId.show}").runAsyncAndForget *>
afterUndo(oid)
postMessage = ToastCtx[IO].showToast(_)
)
.mod(observations)(_ => List(none))
.flatMap(_ => afterDelete)
.showToastCB(s"Deleted obs: ${obsId.show}")

}
13 changes: 11 additions & 2 deletions explore/src/main/scala/explore/observationtree/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import explore.syntax.ui.*
import explore.undo.KIListMod
import explore.undo.KITreeMod
import explore.undo.UndoSetter
import explore.utils.ToastCtx
import japgolly.scalajs.react.*
import lucuma.core.model.Group
import lucuma.core.model.ObsAttachment
Expand Down Expand Up @@ -72,7 +73,11 @@ def cloneObs(
.traverse(cloneObservation[IO](_, newGroupId))
.flatMap: newObsList =>
ObsActions
.obsExistence(newObsList.map(_.id), o => focusObs(programId, o.some, ctx))
.obsExistence(
newObsList.map(_.id),
focusObs = obsId => focusObs(programId, obsId.some, ctx),
postMessage = ToastCtx[IO].showToast(_)
)
.mod(observations): obsList =>
obsList
.zip(newObsList)
Expand Down Expand Up @@ -117,7 +122,11 @@ def insertObs(
createObservation[IO](programId, parentId)
.flatMap: (obs, groupIndex) =>
((ObsActions
.obsExistence(List(obs.id), o => focusObs(programId, o.some, ctx))
.obsExistence(
List(obs.id),
focusObs = obsId => focusObs(programId, obsId.some, ctx),
postMessage = ToastCtx[IO].showToast(_)
)
.mod(observations): obsList =>
obsList
.zip(List(obs))
Expand Down

0 comments on commit c3eb1ad

Please sign in to comment.