2020-01-29 11:54:03 +01:00
|
|
|
<template>
|
2020-01-29 14:13:06 +01:00
|
|
|
<div class="has-text-centered">
|
|
|
|
<div class="title is-size-1">Hello, Anonymous</div>
|
|
|
|
<div class="buttons is-centered">
|
2020-01-29 17:59:37 +01:00
|
|
|
<b-button
|
|
|
|
:disable="isContributeBtnDisabled"
|
|
|
|
@click="makeContribution"
|
|
|
|
type="is-primary"
|
|
|
|
outlined
|
2020-01-29 14:48:42 +01:00
|
|
|
>Make the contribution</b-button
|
|
|
|
>
|
2020-01-29 14:13:06 +01:00
|
|
|
<b-button type="is-primary" outlined>Sign In</b-button>
|
2020-01-29 11:54:03 +01:00
|
|
|
</div>
|
2020-01-29 17:59:37 +01:00
|
|
|
{{ status }}
|
2020-01-29 14:13:06 +01:00
|
|
|
</div>
|
2020-01-29 11:54:03 +01:00
|
|
|
</template>
|
2020-01-29 14:48:42 +01:00
|
|
|
|
|
|
|
<script>
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
|
|
|
const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
|
|
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
isContributeBtnDisabled: false,
|
|
|
|
status: ''
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
async makeContribution({ retry = 0 } = {}) {
|
|
|
|
try {
|
|
|
|
this.isContributeBtnDisabled = true
|
|
|
|
|
|
|
|
this.status = 'Downloading last contribution'
|
2020-01-29 17:59:37 +01:00
|
|
|
let data = await fetch('api/challenge')
|
2020-01-29 14:48:42 +01:00
|
|
|
data = new Uint8Array(await data.arrayBuffer())
|
|
|
|
|
|
|
|
this.status = 'Generating random contribution'
|
|
|
|
await timeout(100) // allow UI to update before freezing in wasm
|
|
|
|
console.log('Source params', data)
|
2020-01-29 17:59:37 +01:00
|
|
|
const contribute = await this.$contribute()
|
2020-01-29 14:48:42 +01:00
|
|
|
const result = contribute(data)
|
|
|
|
console.log('Updated params', result)
|
|
|
|
|
|
|
|
this.status = 'Uploading and verifying your contribution'
|
|
|
|
const formData = new FormData()
|
|
|
|
formData.append('response', new Blob([result], { type: 'application/octet-stream' }))
|
|
|
|
formData.append('name', 'William') // TODO put real name here
|
|
|
|
formData.append('company', 'Microsoft')
|
2020-01-29 17:59:37 +01:00
|
|
|
const resp = await fetch('api/response', {
|
2020-01-29 14:48:42 +01:00
|
|
|
method: 'POST',
|
|
|
|
body: formData
|
|
|
|
})
|
|
|
|
|
|
|
|
if (resp.ok) {
|
|
|
|
this.status = 'Your contribution is verified and recorded. THX BYE.'
|
|
|
|
} else if (resp.status === 422) {
|
|
|
|
if (retry < 3) {
|
|
|
|
console.log(`Looks like someone else uploaded contribution ahead of us, retrying`)
|
|
|
|
await this.makeContribution({ retry: retry++ })
|
|
|
|
} else {
|
|
|
|
this.status = `Failed to upload your contribution after ${retry} attempts`
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.status = 'Error uploading your contribution'
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this.isContributeBtnDisabled = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|