trusted-setup-server/store/user.js
Danil Kovtonyuk 684a7e0a2b refactor
-add name field validation
-disable fields after successful contribute
2020-02-07 01:41:43 +10:00

94 lines
2.5 KiB
JavaScript

const state = () => {
return {
name: null,
handle: 'Anonymous',
company: '',
contributionType: null,
contributionIndex: null
}
}
const mutations = {
SET_NAME(state, name) {
state.name = name
},
SET_HANDLE(state, handle) {
state.handle = handle
},
SET_COMPANY(state, company) {
state.company = company
},
SET_CONTRIBUTION_TYPE(state, contributionType) {
state.contributionType = contributionType
},
SET_CONTRIBUTION_INDEX(state, contributionIndex) {
state.contributionIndex = contributionIndex
}
}
const getters = {
isLoggedIn: (state) => {
return state.name !== null && state.name !== 'Anonymous'
},
hasErrorName: (state) => {
const name = state.name
if (name === null) {
return { invalid: false, msg: '' }
}
if (name === '') {
return { invalid: true, msg: 'Name is empty' }
}
if (name.length < 4) {
return { invalid: true, msg: 'Name is too short' }
}
if (name.length > 35) {
return { invalid: true, msg: 'Name is too long' }
}
return { invalid: false, msg: '' }
}
}
const actions = {
twitterLogIn() {
window.location.replace('/api/connect')
},
makeTweet({ state }) {
const tweetText = `Just made the contribution %23${state.contributionIndex} to Tornado.cash Trusted Setup Ceremony! 🚀`
const popUpWindowWidth = 600
const popUpWindowHeight = 250
const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : window.screenX
const dualScreenTop = window.screenTop !== undefined ? window.screenTop : window.screenY
const width = window.innerWidth
? window.innerWidth
: document.documentElement.clientWidth
? document.documentElement.clientWidth
: screen.width
const height = window.innerHeight
? window.innerHeight
: document.documentElement.clientHeight
? document.documentElement.clientHeight
: screen.height
const systemZoom = width / window.screen.availWidth
const left = (width - popUpWindowWidth) / 2 / systemZoom + dualScreenLeft
const top = (height - popUpWindowHeight) / 2 / systemZoom + dualScreenTop
window.open(
`https://twitter.com/intent/tweet?text=${tweetText}`,
'',
`menubar=no,toolbar=no,resizable=yes,scrollbars=no,height=${popUpWindowHeight},width=${popUpWindowWidth},top=${top},left=${left}`
)
},
async logOut() {
await fetch('/api/logout')
}
}
export default {
namespaced: true,
state,
getters,
mutations,
actions
}