Skip to content

Commit

Permalink
Update attachments endpoints for odb changes
Browse files Browse the repository at this point in the history
  • Loading branch information
toddburnside committed Feb 28, 2025
1 parent 37d9923 commit 53132ce
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 38 deletions.
24 changes: 11 additions & 13 deletions common/src/main/scala/explore/utils/OdbRestClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ trait OdbRestClient[F[_]] {
// Allows us to have a reuse - needed for memoization, etc.
def authToken: NonEmptyString

def getAttachment(programId: Program.Id, attachmentId: Attachment.Id): F[Stream[F, Byte]]
def getAttachment(attachmentId: Attachment.Id): F[Stream[F, Byte]]

def getAttachmentUrl(programId: Program.Id, attachmentId: Attachment.Id): F[String]
def getAttachmentUrl(attachmentId: Attachment.Id): F[String]

def insertAttachment(
programId: Program.Id,
Expand All @@ -40,14 +40,13 @@ trait OdbRestClient[F[_]] {
): F[Attachment.Id]

def updateAttachment(
programId: Program.Id,
attachmentId: Attachment.Id,
fileName: NonEmptyString,
description: Option[NonEmptyString],
data: Stream[F, Byte]
): F[Unit]

def deleteAttachment(programId: Program.Id, attachmentId: Attachment.Id): F[Unit]
def deleteAttachment(attachmentId: Attachment.Id): F[Unit]
}

object OdbRestClient {
Expand Down Expand Up @@ -96,22 +95,21 @@ object OdbRestClient {
val authToken: NonEmptyString = authToken

def getAttachment(
programId: Program.Id,
attachmentId: Attachment.Id
): F[Stream[F, Byte]] =
runRequest("Getting Attachment")(baseUri =>
Request[F](
method = Method.GET,
uri = baseUri / programId.show / attachmentId.show,
uri = baseUri / attachmentId.show,
headers = authHeader
)
).use(r => Async[F].pure(r.body))

def getAttachmentUrl(programId: Program.Id, attachmentId: Attachment.Id): F[String] =
def getAttachmentUrl(attachmentId: Attachment.Id): F[String] =
runRequest("Getting URL")(baseUri =>
Request[F](
method = Method.GET,
uri = baseUri / "url" / programId.show / attachmentId.show,
uri = baseUri / "url" / attachmentId.show,
headers = authHeader
)
).use(
Expand All @@ -126,7 +124,8 @@ object OdbRestClient {
data: Stream[F, Byte]
): F[Attachment.Id] =
runRequest("Adding Attachment") { baseUri =>
val uri = (baseUri / programId.show)
val uri = baseUri
.withQueryParam("programId", programId.show)
.withQueryParam("fileName", fileName)
.withQueryParam("attachmentType", attachmentType.tag)
.withOptionQueryParam("description", description)
Expand All @@ -143,14 +142,13 @@ object OdbRestClient {
)

def updateAttachment(
programId: Program.Id,
attachmentId: Attachment.Id,
fileName: NonEmptyString,
description: Option[NonEmptyString],
data: Stream[F, Byte]
): F[Unit] =
runRequest("Updating Attachment") { baseUri =>
val uri = (baseUri / programId.show / attachmentId.show)
val uri = (baseUri / attachmentId.show)
.withQueryParam("fileName", fileName)
.withOptionQueryParam("description", description)
Request[F](
Expand All @@ -161,9 +159,9 @@ object OdbRestClient {
)
}.use(_ => Async[F].unit)

def deleteAttachment(programId: Program.Id, attachmentId: Attachment.Id): F[Unit] =
def deleteAttachment(attachmentId: Attachment.Id): F[Unit] =
runRequest("Deleting Attachment") { baseUri =>
val uri = baseUri / programId.show / attachmentId.show
val uri = baseUri / attachmentId.show
Request[F](
method = Method.DELETE,
uri = uri,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ object ObsAttachmentsTableBody extends ObsAttachmentUtils:
val name = NonEmptyString.unsafeFrom(f.name)
client
.updateAttachment(
props.pid,
oa.id,
name,
oa.description,
Expand Down Expand Up @@ -138,7 +137,7 @@ object ObsAttachmentsTableBody extends ObsAttachmentUtils:
aid: Attachment.Id
)(using ToastCtx[IO]): IO[Unit] =
props.attachments.mod(_.removed(aid)).toAsync *>
client.deleteAttachment(props.pid, aid).toastErrors
client.deleteAttachment(aid).toastErrors

def deletePrompt(
props: Props,
Expand Down Expand Up @@ -168,7 +167,7 @@ object ObsAttachmentsTableBody extends ObsAttachmentUtils:
.useMemoBy((p, _) => p.authToken): (_, ctx) => // client
token => OdbRestClient[IO](ctx.environment, token) // This could be in the shared state
.useStateView[UrlMap](Map.empty) // urlMap
.useEffectWithDepsBy((props, _, _, _) => props.attachments.get): (props, _, client, urlMap) =>
.useEffectWithDepsBy((props, _, _, _) => props.attachments.get): (_, _, client, urlMap) =>
attachments =>
val allCurrentKeys = attachments.observationList.map(_.toMapKey).toSet
val newOas = allCurrentKeys.filter(key => !urlMap.get.contains(key)).toList
Expand All @@ -179,7 +178,7 @@ object ObsAttachmentsTableBody extends ObsAttachmentUtils:
newOas.foldRight(filteredMap)((key, m) => m.updated(key, pending))
}.toAsync
val getUrls =
newOas.traverse_(key => getAttachmentUrl(props.pid, client, key, urlMap))
newOas.traverse_(key => getAttachmentUrl(client, key, urlMap))

updateUrlMap *> getUrls
// Columns
Expand Down
3 changes: 1 addition & 2 deletions explore/src/main/scala/explore/attachments/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,12 @@ trait ObsAttachmentUtils extends AttachmentUtils:
.when_(files.nonEmpty)

def getAttachmentUrl(
pid: Program.Id,
client: OdbRestClient[IO],
mapKey: UrlMapKey,
urlMap: View[UrlMap]
): IO[Unit] =
client
.getAttachmentUrl(pid, mapKey._1)
.getAttachmentUrl(mapKey._1)
.attempt
.map {
case Right(url) => Pot(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ object FinderChartsTile:
}.toAsync

val getUrls =
newOas.traverse_(key => getAttachmentUrl(props.programId, client, key, urlMap))
newOas.traverse_(key => getAttachmentUrl(client, key, urlMap))

val defaultSelected =
if (allCurrentKeys.size === 1) props.selected.set(allCurrentKeys.headOption.map(_._1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ trait ProposalAttachmentUtils extends AttachmentUtils:
.orEmpty

def updateAttachment(
programId: Program.Id,
modAttachments: Endo[AttachmentList] => Callback,
client: OdbRestClient[IO],
att: Attachment,
Expand All @@ -82,7 +81,6 @@ trait ProposalAttachmentUtils extends AttachmentUtils:
val name = NonEmptyString.unsafeFrom(f.name)
client
.updateAttachment(
programId,
att.id,
name,
none,
Expand All @@ -104,20 +102,18 @@ trait ProposalAttachmentUtils extends AttachmentUtils:
.orEmpty

def deleteAttachment(
programId: Program.Id,
modAttachments: Endo[AttachmentList] => Callback,
client: OdbRestClient[IO],
attId: Attachment.Id
)(using ToastCtx[IO]): IO[Unit] =
modAttachments(_.removed(attId)).toAsync *>
client.deleteAttachment(programId, attId).toastErrors
client.deleteAttachment(attId).toastErrors

def getAttachmentUrl(
programId: Program.Id,
attId: Attachment.Id,
client: OdbRestClient[IO]
attId: Attachment.Id,
client: OdbRestClient[IO]
): IO[Pot[String]] =
client.getAttachmentUrl(programId, attId).attempt.map {
client.getAttachmentUrl(attId).attempt.map {
case Right(url) => Pot(url)
case Left(t) => Pot.error(t)
}
Expand All @@ -136,15 +132,14 @@ trait ProposalAttachmentUtils extends AttachmentUtils:
.runAsync).when_(files.nonEmpty)

def onUpdateFileSelected(
programId: Program.Id,
modAttachments: Endo[AttachmentList] => Callback,
thisAtt: Attachment,
client: OdbRestClient[IO],
action: View[Action]
)(e: ReactEventFromInput)(using ToastCtx[IO], Logger[IO]): Callback =
val files = e.target.files.toList
(Callback(e.target.value = null) *>
updateAttachment(programId, modAttachments, client, thisAtt, files)
updateAttachment(modAttachments, client, thisAtt, files)
.switching(action.async, Action.Replace, Action.None)
.runAsync)
.when_(files.nonEmpty)
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ object ProposalAttachmentsTable extends ProposalAttachmentUtils {
extension (a: Attachment) private def toMapKey: UrlMapKey = (a.id, a.updatedAt)

def deletePrompt(
programId: Program.Id,
modAttachments: Endo[AttachmentList] => Callback,
client: OdbRestClient[IO],
att: Attachment
Expand All @@ -86,7 +85,7 @@ object ProposalAttachmentsTable extends ProposalAttachmentUtils {
s"Delete attachment? This action is not undoable.",
acceptLabel = "Delete",
rejectLabel = "Cancel",
accept = deleteAttachment(programId, modAttachments, client, att.id).runAsync
accept = deleteAttachment(modAttachments, client, att.id).runAsync
)
.show

Expand All @@ -99,7 +98,7 @@ object ProposalAttachmentsTable extends ProposalAttachmentUtils {
.useStateView(Action.None)
.useStateView[UrlMap](Map.empty)
.useEffectWithDepsBy((props, _, _, _, _) => props.attachments.get.proposalList):
(props, _, client, _, urlMap) =>
(_, _, client, _, urlMap) =>
attachments =>
val allCurrentKeys = attachments.map(_.toMapKey).toSet
val newPas = allCurrentKeys.filter(key => !urlMap.get.contains(key)).toList
Expand All @@ -111,9 +110,7 @@ object ProposalAttachmentsTable extends ProposalAttachmentUtils {
}.toAsync
val getUrls =
newPas.traverse_(key =>
getAttachmentUrl(props.programId, key._1, client).flatMap(p =>
urlMap.mod(_.updated(key, p)).toAsync
)
getAttachmentUrl(key._1, client).flatMap(p => urlMap.mod(_.updated(key, p)).toAsync)
)

updateUrlMap *> getUrls
Expand Down Expand Up @@ -165,7 +162,6 @@ object ProposalAttachmentsTable extends ProposalAttachmentUtils {
tableLabelButtonClasses,
Icons.Trash,
^.onClick ==> deletePrompt(
props.programId,
props.attachments.mod,
client,
thisAtt
Expand All @@ -183,7 +179,6 @@ object ProposalAttachmentsTable extends ProposalAttachmentUtils {
ExploreStyles.FileUpload,
^.tpe := "file",
^.onChange ==> onUpdateFileSelected(
props.programId,
props.attachments.mod,
thisAtt,
client,
Expand Down

0 comments on commit 53132ce

Please sign in to comment.