Hi all,
I am new to scriptable/widget/coding, basically I am a newbie.
I have developed the following widget to hopefully get a transfer rate from Transferwise between GBP and USD. And what I want to do with the widget is that, whenever I tap the widget, it refresh the widget by “createWidget” again. But even through I set it to “Run Script” it is still redirecting me back to Scriptable.
Is there anyway we can tap and refresh it without redirecting me to somewhere else?
Thanks
let url = "https://api.sandbox.transferwise.tech/v2/quotes/";
let body = {};
//Source Current of the transfer : USD
body.sourceCurrency = "USD";
//Target Current of the transfer : GBP
body.targetCurrency = "GBP";
//change the amount
body.sourceAmount = 60000;
let imgURL = "https://cdn.iconscout.com/icon/free/png-256/transferwise-3089509-2567361.png"
const image = await new Request(imgURL).loadImage()
const resultJSON = await wiseCall(url,body)
let result = resultJSON.paymentOptions[0].targetAmount
console.log(result)
let resultDate = resultJSON.rateTimestamp
console.log(resultDate)
if (config.runsInWidget){
let widget = createWidget()
Script.setWidget(widget)
Script.complete()
}
async function wiseCall(url,body) {
let request = new Request(url)
request.headers = {"Content-type" : "application/json"}
request.method = "POST"
request.body = JSON.stringify(body)
console.log(body)
let resultJSON = await request.loadJSON()
return resultJSON
}
function createWidget() {
const v = new ListWidget()
v.backgroundColor = new Color("FFFFFF00")
const w = v.addStack()
// w.layoutHorizontally()
const stackImg = w.addStack()
let imageBlock = stackImg.addImage(image)
imageBlock.imageSize = new Size (50,50)
stackImg.topAlignContent()
// Old stack to an empty line between logo
let stackSpace = w.addStack().addText(" ")
//New stack to contain the text
const stackVert = w.addStack()
stackVert.layoutVertically()
const stack = stackVert.addStack()
stack.addText("USD 60,000 = GBP ").textColor = Color.white()
let resultText = stack.addText(String(result))
resultText.textColor = Color.green()
stack.addSpacer()
// Time format 2021-06-27T21:56:37Z
const df = new DateFormatter()
df.useShortDateStyle()
df.useShortTimeStyle()
resultDate = new Date(resultDate)
log(df.string(resultDate))
const stackDate = stackVert.addStack()
stackDate.addText("Updated : "+df.string(resultDate)).textColor = Color.white()
stackDate.addSpacer()
return v
}