Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Donations after approved don't go back to pyodide, but are send to the bridge #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/framework/processing/py/port/api/props.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ class PropsUIPromptConsentForm:
"""Tables to be shown to the participant prior to donation

Attributes:
id: A unique string to identify the table after donation.
tables: a list of tables
meta_tables: a list of optional tables, for example for logging data
"""

id: str
tables: list[PropsUIPromptConsentFormTable]
meta_tables: list[PropsUIPromptConsentFormTable]
description: Optional[Translatable] = None
Expand All @@ -139,6 +141,7 @@ def translate_meta_tables(self):
def toDict(self):
dict = {}
dict["__type__"] = "PropsUIPromptConsentForm"
dict["id"] = self.id
dict["tables"] = self.translate_tables()
dict["metaTables"] = self.translate_meta_tables()
dict["description"] = self.description and self.description.toDict()
Expand Down
9 changes: 3 additions & 6 deletions src/framework/processing/py/port/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ def process(sessionId):

# STEP 2: ask for consent
meta_data.append(("debug", f"{key}: prompt consent"))
prompt = prompt_consent(data, meta_data)
prompt = prompt_consent(f"{sessionId}-{key}", data, meta_data)
consent_result = yield render_donation_page(prompt)
if consent_result.__type__ == "PayloadJSON":
meta_data.append(("debug", f"{key}: donate consent data"))
yield donate(f"{sessionId}-{key}", consent_result.value)
if consent_result.__type__ == "PayloadFalse":
value = json.dumps('{"status" : "donation declined"}')
yield donate(f"{sessionId}-{key}", value)
Expand Down Expand Up @@ -135,7 +132,7 @@ def extract_file(zipfile_ref, filename):
return "invalid"


def prompt_consent(data, meta_data):
def prompt_consent(id, data, meta_data):

table_title = props.Translatable({
"en": "Zip file contents",
Expand All @@ -156,7 +153,7 @@ def prompt_consent(data, meta_data):

meta_frame = pd.DataFrame(meta_data, columns=["type", "message"])
meta_table = props.PropsUIPromptConsentFormTable("log_messages", log_title, meta_frame)
return props.PropsUIPromptConsentForm(tables, [meta_table])
return props.PropsUIPromptConsentForm(id, tables, [meta_table])


def donate(key, json_string):
Expand Down
27 changes: 25 additions & 2 deletions src/framework/processing/worker_engine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandHandler, ProcessingEngine } from '../types/modules'
import { CommandSystemEvent, isCommand, Response } from '../types/commands'
import { CommandSystemEvent, CommandSystemDonate, isCommand, Response, PayloadDonate } from '../types/commands'

export default class WorkerProcessingEngine implements ProcessingEngine {
sessionId: String
Expand Down Expand Up @@ -88,9 +88,32 @@ export default class WorkerProcessingEngine implements ProcessingEngine {
handleRunCycle (command: any): void {
if (isCommand(command)) {
this.commandHandler.onCommand(command).then(
(response) => this.nextRunCycle(response),
(response) => {
switch(response.payload.__type__) {
case "PayloadDonate":
const donateCommand = constructDonateCommand(response.payload)
this.commandHandler.onCommand(donateCommand).then(
(response) => {
this.nextRunCycle(response)
}
)
break

default:
this.nextRunCycle(response)
}
},
() => {}
)
}
}
}

function constructDonateCommand(payload: PayloadDonate) {
const donateCommand: CommandSystemDonate = {
"__type__": "CommandSystemDonate",
"key": payload.key,
"json_string": payload.value,
}
return donateCommand
}
9 changes: 8 additions & 1 deletion src/framework/types/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export type PayloadResolved =
PayloadTrue |
PayloadString |
PayloadFile |
PayloadJSON
PayloadJSON |
PayloadDonate

export interface PayloadVoid {
__type__: 'PayloadVoid'
Expand Down Expand Up @@ -73,6 +74,12 @@ export function isPayloadJSON (arg: any): arg is PayloadJSON {
return isInstanceOf<PayloadJSON>(arg, 'PayloadJSON', ['value'])
}

export interface PayloadDonate {
__type__: 'PayloadDonate'
value: string
key: string
}

export type Command =
CommandUI |
CommandSystem
Expand Down
3 changes: 2 additions & 1 deletion src/framework/types/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ export interface PropsUIPromptConsentForm {
description?: Text
donateQuestion?: Text
donateButton?: Text
id: string
tables: PropsUIPromptConsentFormTable[]
metaTables: PropsUIPromptConsentFormTable[]
}
export function isPropsUIPromptConsentForm (arg: any): arg is PropsUIPromptConsentForm {
return isInstanceOf<PropsUIPromptConsentForm>(arg, 'PropsUIPromptConsentForm', ['tables', 'metaTables'])
return isInstanceOf<PropsUIPromptConsentForm>(arg, 'PropsUIPromptConsentForm', ['id', 'tables', 'metaTables'])
}

export interface PropsUIPromptConsentFormTable {
Expand Down
4 changes: 2 additions & 2 deletions src/framework/visualisation/react/ui/prompts/consent_form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const ConsentForm = (props: Props): JSX.Element => {
const tablesOut = React.useRef<Array<PropsUITable & TableContext>>(tablesIn.current)
const [waiting, setWaiting] = React.useState<boolean>(false)

const { locale, resolve } = props
const { locale, resolve, id } = props
const cancelButton = Translator.translate(cancelButtonLabel, props.locale)

function rowCell (dataFrame: any, column: string, row: number): PropsUITableCell {
Expand Down Expand Up @@ -104,7 +104,7 @@ export const ConsentForm = (props: Props): JSX.Element => {
function handleDonate (): void {
setWaiting(true)
const value = serializeConsentData()
resolve?.({ __type__: 'PayloadJSON', value })
resolve?.({ __type__: 'PayloadDonate', "value": value, "key": id })
}

function handleCancel (): void {
Expand Down