Skip to content

Commit

Permalink
webapp: fix clipboard, fix typo, minor UI improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioPinheiro committed Oct 28, 2023
1 parent 9081eb5 commit 7ba97e3
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 51 deletions.
2 changes: 1 addition & 1 deletion demo/shared/src/main/scala/fmgp/did/demo/DemoMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import fmgp.did.method.peer._
_ <- Console.printLine(s"auth msg: ${msg3.toJson /*Pretty*/}")
} yield ()

Unsafe.unsafe { implicit unsafe => // Run side efect
Unsafe.unsafe { implicit unsafe => // Run side effect
Runtime.default.unsafe
.run(
program.provide(
Expand Down
2 changes: 1 addition & 1 deletion demo/shared/src/main/scala/fmgp/did/demo/ExampleMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import fmgp.util.Base64
afterDecryot <- authDecrypt(msg2).provideSomeLayer(Agent1Mediators.agentLayer)
} yield ()

Unsafe.unsafe { implicit unsafe => // Run side efect
Unsafe.unsafe { implicit unsafe => // Run side effect
Runtime.default.unsafe
.run(program.provide(Operations.layerDefault ++ DidPeerResolver.layer))
.getOrThrowFiberFailure()
Expand Down
2 changes: 1 addition & 1 deletion docs/quickstart-basic-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ val program = for {
_ <- Console.printLine(msg.toJsonPretty)
} yield ()

Unsafe.unsafe { implicit unsafe => // Run side efect
Unsafe.unsafe { implicit unsafe => // Run side effect
Runtime.default.unsafe
.run(program.provide(Operations.layerDefault ++ DidPeerResolver.layer))
.getOrThrowFiberFailure()
Expand Down
6 changes: 3 additions & 3 deletions webapp/src/main/scala/fmgp/Client.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ object Client {
// @scala.scalajs.js.annotation.JSExport var tmp: Any = _

@scala.scalajs.js.annotation.JSExport
def runNewKeyX255199 = Unsafe.unsafe { implicit unsafe => // Run side efect
def runNewKeyX255199 = Unsafe.unsafe { implicit unsafe => // Run side effect
Runtime.default.unsafe.runToFuture(
newKeyX25519.map(key => println(key.toJsonPretty)).mapError(DidException(_))
) // .getOrThrowFiberFailure()
}
@scala.scalajs.js.annotation.JSExport
def runNewKeyEd255199 = Unsafe.unsafe { implicit unsafe => // Run side efect
def runNewKeyEd255199 = Unsafe.unsafe { implicit unsafe => // Run side effect
Runtime.default.unsafe.runToFuture(
newKeyEd255199.map(key => println(key.toJsonPretty)).mapError(DidException(_))
) // .getOrThrowFiberFailure()
Expand Down Expand Up @@ -93,7 +93,7 @@ object Client {

// def runGetDB: CancelableFuture[Option[MessageDB]] = {
// val program: IO[DidFail, Option[MessageDB]] = getDB()
// Unsafe.unsafe { implicit unsafe => // Run side efect
// Unsafe.unsafe { implicit unsafe => // Run side effect
// Runtime.default.unsafe.runToFuture(
// program
// .catchAll(ex => ZIO.logError(ex.toString) *> ZIO.fail(new RuntimeException(ex.toString)))
Expand Down
4 changes: 2 additions & 2 deletions webapp/src/main/scala/fmgp/ServiceWorkerUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object ServiceWorkerUtils {

@scala.scalajs.js.annotation.JSExport
def runSubscribeToNotifications =
Unsafe.unsafe { implicit unsafe => // Run side efect
Unsafe.unsafe { implicit unsafe => // Run side effect
Runtime.default.unsafe.runToFuture({
subscribeToNotifications
})
Expand All @@ -87,7 +87,7 @@ object ServiceWorkerUtils {

@scala.scalajs.js.annotation.JSExport
def runPushNotificationsSubscription(id: String) =
Unsafe.unsafe { implicit unsafe => // Run side efect
Unsafe.unsafe { implicit unsafe => // Run side effect
Runtime.default.unsafe.runToFuture({
pushNotificationsSubscription(id)
})
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/main/scala/fmgp/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import fmgp.webapp.ResolverTool

object Utils {

def runProgram(program: ZIO[Any, DidFail, Unit]) = Unsafe.unsafe { implicit unsafe => // Run side efect
def runProgram(program: ZIO[Any, DidFail, Unit]) = Unsafe.unsafe { implicit unsafe => // Run side effect
Runtime.default.unsafe.fork(
program.catchAll { case error =>
ZIO.succeed(println(error))
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/main/scala/fmgp/webapp/AgentDB.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object AgentDB {
def updateDB =
val program: IO[DidFail, Option[MessageDB]] = Client.getDB()
Unsafe.unsafe { implicit unsafe => // Run side efect
Unsafe.unsafe { implicit unsafe => // Run side effect
Runtime.default.unsafe.fork(
program
.map(db => dbVar.update(_ => db))
Expand Down
4 changes: 2 additions & 2 deletions webapp/src/main/scala/fmgp/webapp/AgentManagement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object AgentManagement {
}
// .tap(_ => ZIO.succeed(urlEndpoint.ref.value = "")) // clean up

Unsafe.unsafe { implicit unsafe => // Run side efect
Unsafe.unsafe { implicit unsafe => // Run side effect
Runtime.default.unsafe.runToFuture(
programe.mapError(DidException(_))
)
Expand Down Expand Up @@ -140,7 +140,7 @@ object AgentManagement {
)
),
td(code("N/A")),
td(a(element.value.did, MyRouter.navigateTo(MyRouter.ResolverPage(element.value.did)))),
td(AppUtils.linkToResolveDID(element.value)),
)
}
),
Expand Down
6 changes: 4 additions & 2 deletions webapp/src/main/scala/fmgp/webapp/AppUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.raquo.laminar.codecs._
import com.raquo.laminar.api.L._

import MyRouter._

import fmgp.did._
@JSExportTopLevel("AppUtils")
object AppUtils { // TODO RENAME TO UIUtils

Expand Down Expand Up @@ -165,7 +165,9 @@ object AppUtils { // TODO RENAME TO UIUtils
span(className("mdc-button__label"), text),
)
)

}

def linkToResolveDID(subject: DIDSubject) =
a(subject.did, MyRouter.navigateTo(MyRouter.ResolverPage(subject.did)))

}
14 changes: 8 additions & 6 deletions webapp/src/main/scala/fmgp/webapp/DecryptTool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,14 @@ object DecryptTool {
})),
button(
"Copy to clipboard",
onClick --> Global.clipboardSideEffect(
decryptMessageVar.now() match
case None => "None"
case Some(Left(didFail)) => didFail.toString
case Some(Right(msg)) => msg.toJson
)
onClick --> { _ =>
Global.copyToClipboard(
decryptMessageVar.now() match
case None => "None"
case Some(Left(didFail)) => didFail.toString
case Some(Right(msg)) => msg.toJson
)
}
)
)

Expand Down
34 changes: 18 additions & 16 deletions webapp/src/main/scala/fmgp/webapp/EncryptTool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ object EncryptTool {
program(pMsg: PlaintextMessage, eMsg: EncryptedMessage)
.map(forwardMessageVar.set(_))
)
} // Run side efect
} // Run side effect
case _ => None
}
.observe(owner)
Expand All @@ -92,7 +92,7 @@ object EncryptTool {
.either
.map(_.map((pMsg, _)))
.map(e => encryptedMessageVar.update(_ => Some(e)))
Unsafe.unsafe { implicit unsafe => // Run side efect
Unsafe.unsafe { implicit unsafe => // Run side effect
Runtime.default.unsafe.fork(
program.provideSomeLayer(Global.resolverLayer)
)
Expand All @@ -106,7 +106,7 @@ object EncryptTool {
.map(_.map((pMsg, _)))
.map(e => encryptedMessageVar.update(_ => Some(e)))

Unsafe.unsafe { implicit unsafe => // Run side efect
Unsafe.unsafe { implicit unsafe => // Run side effect
Runtime.default.unsafe.fork(
program
.provideSomeLayer(Global.resolverLayer)
Expand All @@ -130,7 +130,7 @@ object EncryptTool {
.either
.map(_.map((pMsg, _)))
.map(e => signMessageVar.update(_ => Some(e)))
Unsafe.unsafe { implicit unsafe => // Run side efect
Unsafe.unsafe { implicit unsafe => // Run side effect
Runtime.default.unsafe.fork(
programSign
.provideSomeLayer(Global.resolverLayer)
Expand All @@ -157,7 +157,7 @@ object EncryptTool {
)
} yield (ret)

Unsafe.unsafe { implicit unsafe => // Run side efect
Unsafe.unsafe { implicit unsafe => // Run side effect
Runtime.default.unsafe.fork(
program.provide(Global.resolverLayer)
)
Expand All @@ -178,14 +178,16 @@ object EncryptTool {
overflowWrap.:=("anywhere"),
"Agent: ",
" ",
code(child.text <-- Global.agentVar.signal.map(_.map(_.id.string).getOrElse(Global.noneOption)))
child <-- Global.agentVar.signal
.map(_.map(agent => code(AppUtils.linkToResolveDID(agent.id))).getOrElse(code(Global.noneOption))),
),
p(
overflowWrap.:=("anywhere"),
"Send TO (used by Templates only. Does not influence the encryption): ",
Global.makeSelectElementTO(Global.recipientVar),
" ",
code(child.text <-- Global.recipientVar.signal.map(_.map(_.toDID.string).getOrElse(Global.noneOption))),
child <-- Global.recipientVar.signal
.map(_.map(to => code(AppUtils.linkToResolveDID(to.toDIDSubject))).getOrElse(code(Global.noneOption))),
),
p(
"Templates:",
Expand Down Expand Up @@ -380,7 +382,7 @@ object EncryptTool {
pre(code(msg.toJsonPretty)),
button(
"Copy Plaintext Message to clipboard",
onClick --> Global.clipboardSideEffect(msg.toJson)
onClick --> { _ => Global.copyToClipboard(msg.toJson) }
)
)
}
Expand All @@ -407,9 +409,9 @@ object EncryptTool {
eMsg,
from,
plaintext
) // side efect
) // side effect
)
Global.clipboardSideEffect(eMsg.toJson)
Global.copyToClipboard(eMsg.toJson)
}
)
)
Expand All @@ -435,9 +437,9 @@ object EncryptTool {
sMsg,
from,
plaintext
) // side efect
) // side effect
)
Global.clipboardSideEffect(sMsg.toJson)
Global.copyToClipboard(sMsg.toJson)
}
)
)
Expand All @@ -460,7 +462,7 @@ object EncryptTool {
.map {
case Some(curlStr) =>
div(
button("Copy to curl", onClick --> Global.clipboardSideEffect(curlStr)),
button("Copy to curl", onClick --> { _ => Global.copyToClipboard(curlStr) }),
button(
"Make HTTP POST",
onClick --> Sink.jsCallbackToSink(_ =>
Expand All @@ -470,19 +472,19 @@ object EncryptTool {
eMsg,
Global.agentVar.now().map(_.id.asFROM).getOrElse(???),
plaintext
) // side efect
) // side effect
Utils.runProgram(
Utils
.curlProgram(eMsg)
.map { case output: String =>
output.fromJson[EncryptedMessage] match {
case Left(value) => outputFromCallVar.set(None) // side efect
case Left(value) => outputFromCallVar.set(None) // side effect
case Right(value: EncryptedMessage) => outputFromCallVar.set(Some(value))
}
}
.tapError { e =>
println("ERROR:" + e.toString) // REMOVE
ZIO.logError(e.toString) *> ZIO.succeed(outputFromCallVar.set(None)) // side efect
ZIO.logError(e.toString) *> ZIO.succeed(outputFromCallVar.set(None)) // side effect
}
.provide(Global.resolverLayer)
)
Expand Down
13 changes: 4 additions & 9 deletions webapp/src/main/scala/fmgp/webapp/Global.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fmgp.webapp

import scala.util._
import scala.scalajs.js
import scala.scalajs.js.annotation.JSExport
import org.scalajs.dom
Expand All @@ -12,9 +13,6 @@ import fmgp.did.comm._
import fmgp.did.method.peer.DIDPeer
import fmgp.did.agent.MessageStorage
import fmgp.crypto.error._
import scala.util.Try
import scala.util.Failure
import scala.util.Success
import fmgp.Utils

object Global {
Expand Down Expand Up @@ -81,8 +79,9 @@ object Global {
}
)

def clipboardSideEffect(text: => String): Any => Unit =
(_: Any) => { dom.window.navigator.clipboard.writeText(text) }
/** side effect of writing the text to the clipboard */
def copyToClipboard(text: => String): Unit =
dom.window.navigator.clipboard.writeText(text) // side effect

@JSExport
def update(htmlPath: String) = {
Expand All @@ -97,21 +96,17 @@ object Global {
def messageSend(msg: SignedMessage | EncryptedMessage, from: FROM, plaintext: PlaintextMessage) =
messageStorageVar.tryUpdate {
case Success(messageStorage) =>
println(s"DEBUG: Store messageSend id: ${plaintext.id}")
Success(messageStorage.messageSend(msg, from, plaintext))
case Failure(exception) =>
println(s"DEBUG: Store messageSend id: ${plaintext.id} FAIL: ${exception}")
Failure(exception)
}

/** Store in BD */
def messageRecive(msg: SignedMessage | EncryptedMessage, plaintext: PlaintextMessage) =
messageStorageVar.tryUpdate {
case Success(messageStorage) =>
println(s"DEBUG: Store messageRecive id: ${plaintext.id}")
Success(messageStorage.messageRecive(msg, plaintext))
case Failure(exception) =>
println(s"DEBUG: Store messageRecive id: ${plaintext.id} FAIL: ${exception}")
Failure(exception)
}

Expand Down
14 changes: 8 additions & 6 deletions webapp/src/main/scala/fmgp/webapp/ResolverTool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object ResolverTool {
errorInfo => didDocumentVar.update(_ => Left(errorInfo.toString)),
doc => didDocumentVar.update(_ => Right(doc))
).provide(Global.resolverLayer)
Unsafe.unsafe { implicit unsafe => Runtime.default.unsafe.fork(program) } // Run side efect
Unsafe.unsafe { implicit unsafe => Runtime.default.unsafe.fork(program) } // Run side effect
}
.observe(owner)

Expand Down Expand Up @@ -96,11 +96,13 @@ object ResolverTool {
),
button(
"Copy to clipboard",
onClick --> Global.clipboardSideEffect(
didDocumentVar.now() match
case Right(doc) => doc.toJson
case Left(errorInfo) => errorInfo
)
onClick --> { _ =>
Global.copyToClipboard(
didDocumentVar.now() match
case Right(doc) => doc.toJson
case Left(errorInfo) => errorInfo
)
}
)
)
}

0 comments on commit 7ba97e3

Please sign in to comment.