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

Enbales CSV login through URI hash #122

Merged
merged 2 commits into from
Dec 1, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import {
import { useDisclosure } from '@chakra-ui/react-use-disclosure'
import { ChakraProps, useMultiStyleConfig } from '@chakra-ui/system'
import { useToast } from '@chakra-ui/toast'
import { Wallet } from '@ethersproject/wallet'
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'

export const SpreadsheetAccess = (rest: ChakraProps) => {
Expand All @@ -41,6 +42,31 @@ export const SpreadsheetAccess = (rest: ChakraProps) => {
formState: { errors },
} = useForm<any>()

const shouldRender = election?.get('census.type') === 'spreadsheet' && !(election instanceof ArchivedElection)
const privkey = window.location.hash ? window.location.hash.split('#')[1] : ''

// In case of spreadsheet census and a private provided through the URI, do intent to login automatically
// Example url with private key:
// https://app.vocdoni.io/processes/0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef#0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
useEffect(() => {
;(async () => {
try {
if (!shouldRender || !privkey) return
const privKeyWallet = new Wallet(privkey)
let client = new VocdoniSDKClient({
env,
wallet: privKeyWallet,
electionId: election?.id,
})
setClient(client)
} catch (error) {
console.warn('Error trying to login with private key ', error)
setClient(cl)
}
})()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [election, env, shouldRender, privkey])

const onSubmit = async (vals: any) => {
try {
setLoading(true)
Expand Down Expand Up @@ -115,7 +141,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 (!shouldRender) return null

if (connected) {
return (
Expand Down Expand Up @@ -144,7 +170,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