Skip to content

Commit

Permalink
Extract generic step functions
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed May 30, 2024
1 parent c5bccc7 commit 3124195
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions packages/react-providers/src/election/use-election-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,36 +333,48 @@ export const useElectionProvider = ({
}
}

const blindCspVote = async () => {
if (!client) {
throw new Error('no client initialized')
}
if (!(election instanceof PublishedElection) || election?.census?.type !== CensusType.CSP) {
throw new Error('not a CSP election')
}

// CSP generic steps
const cspStep0 = async (handler: string, data: any[], cb?: (step0: ICspIntermediateStepResponse) => void) => {
let step0: ICspIntermediateStepResponse
try {
// TODO: properly type when ICspIntermediateStepResponse is exposed from SDK
step0 = (await client.cspStep(0, ['Name test'])) as ICspIntermediateStepResponse
step0 = (await client.cspStep(0, data)) as ICspIntermediateStepResponse
actions.csp0({ handler, token: step0.authToken })
if (cb) {
cb(step0)
}
return step0
} catch (e) {
actions.votingError(e)
console.warn('CSP step 0 error', e)
return
}
}

// CSP generic steps
const cspStep1 = async (data: any[], authToken?: string | undefined) => {
try {
const step1 = (await client.cspStep(
1,
[step0.response.reduce((acc, v) => +acc + +v, 0).toString()],
step0.authToken
)) as ICspFinalStepResponse
const step1 = (await client.cspStep(1, data, authToken)) as ICspFinalStepResponse
actions.csp1(step1.token)
} catch (e) {
actions.votingError(localize('errors.unauthorized'))
console.warn('CSP step 1 error', e)
}
}

// blind CSP flow
const blindCspVote = async () => {
if (!client) {
throw new Error('no client initialized')
}
if (!(election instanceof PublishedElection) || election?.census?.type !== CensusType.CSP) {
throw new Error('not a CSP election')
}
const handler = (election as PublishedElection).meta.csp?.service

const step0 = await cspStep0(handler, ['Name test'])
if (!step0) return
await cspStep1([step0.response.reduce((acc, v) => +acc + +v, 0).toString()], step0.authToken)
}

// CSP OAuth flow
const cspAuthAndVote = async () => {
const handler = (election as PublishedElection).meta.csp?.service
Expand All @@ -380,15 +392,9 @@ export const useElectionProvider = ({
const redirectURL: string = `${window.location.origin}${window.location.pathname}?${params.toString()}${
window.location.hash
}`

try {
// TODO: properly type when ICspIntermediateStepResponse is exposed from SDK
const step0: any = await client.cspStep(0, [handler, redirectURL])
actions.csp0({ handler, token: step0.authToken })
await cspStep0(handler, [handler, redirectURL], (step0) => {
openLoginPopup(handler, step0['response'][0])
} catch (e) {
actions.votingError(e)
}
})
}

// CSP OAuth flow
Expand Down Expand Up @@ -432,14 +438,7 @@ export const useElectionProvider = ({
const redirectURL = `${window.location.origin}${window.location.pathname}?${params.toString()}${
window.location.hash
}`

try {
const step1: any = await client.cspStep(1, [handler, code, redirectURL], csp.authToken)
actions.csp1(step1.token)
} catch (e) {
actions.votingError(localize('errors.unauthorized'))
console.warn('CSP step 1 error', e)
}
await cspStep1([handler, code, redirectURL], csp.authToken)
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[csp.token, csp.authToken, client]
Expand Down

0 comments on commit 3124195

Please sign in to comment.