Skip to content

Commit

Permalink
Enbales CSV login through URI hash
Browse files Browse the repository at this point in the history
  • Loading branch information
emmdim committed Dec 1, 2023
1 parent 93919ca commit 6102d38
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import { ChakraProps, useMultiStyleConfig } from '@chakra-ui/system'
import { useToast } from '@chakra-ui/toast'
import { errorToString, useClient, useElection, walletFromRow } from '@vocdoni/react-providers'
import { AnonymousService, ArchivedElection, dotobject, VocdoniSDKClient } from '@vocdoni/sdk'
import { useState } from 'react'
import { useEffect, useState } from 'react'
import { useForm } from 'react-hook-form'
import { Wallet } from '@ethersproject/wallet'

export const SpreadsheetAccess = (rest: ChakraProps) => {
const { isOpen, onOpen, onClose } = useDisclosure()
Expand All @@ -32,6 +33,27 @@ export const SpreadsheetAccess = (rest: ChakraProps) => {
handleSubmit,
formState: { errors },
} = useForm<any>()
const isSpreadSheetorAndIsNotArchiveElection =
election?.get('census.type') === 'spreadsheet' && !(election instanceof ArchivedElection)

const pkey = window.location.hash ? window.location.hash.split('#')[1] : ''

// In case of spreadsheet census and a pkey provided intent to login automaticall
// Example usl with pkey:
// https://app.vocdoni.io/#0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef#0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
useEffect(() => {
;(async () => {
if (!isSpreadSheetorAndIsNotArchiveElection) return
if (pkey) {
let client = new VocdoniSDKClient({
env,
wallet: new Wallet(pkey),
electionId: election?.id,
})
setClient(client)
}
})()
}, [election, env, isSpreadSheetorAndIsNotArchiveElection, pkey])

const onSubmit = async (vals: any) => {
try {
Expand Down Expand Up @@ -107,7 +129,7 @@ export const SpreadsheetAccess = (rest: ChakraProps) => {
message: localize('validation.min_length', { min: 8 }),
}

if (election?.get('census.type') !== 'spreadsheet' || election instanceof ArchivedElection) return null
if (!isSpreadSheetorAndIsNotArchiveElection) return null

if (connected) {
return (
Expand Down Expand Up @@ -136,7 +158,7 @@ export const SpreadsheetAccess = (rest: ChakraProps) => {
<FormErrorMessage sx={styles.error}>{errors[key]?.message?.toString()}</FormErrorMessage>
</FormControl>
))}
{election.electionType.anonymous && (
{election?.electionType.anonymous && (
<FormControl isInvalid={!!errors.sik_password} sx={styles.sik_control}>
<FormLabel sx={styles.label}>{localize('spreadsheet.anon_sik_label')}</FormLabel>
<Input {...register('sik_password', { required, minLength })} type='password' sx={styles.input} />
Expand Down

0 comments on commit 6102d38

Please sign in to comment.