Skip to content

Commit

Permalink
Implement envelope error boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed Sep 5, 2024
1 parent 3ffb674 commit 7df3c60
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
49 changes: 47 additions & 2 deletions packages/chakra-components/src/components/Election/Envelope.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
PublishedElection,
} from '@vocdoni/sdk'
import { format } from 'date-fns'
import { Component, ErrorInfo, PropsWithChildren, ReactNode, useEffect, useState } from 'react'

export type VotePackageType = IVotePackage | IVoteEncryptedPackage

Expand Down Expand Up @@ -48,8 +49,10 @@ export const Envelope = ({
{election.questions.map((q, i) => {
return (
<chakra.div sx={styles.question}>
<Text sx={styles.title}>{localize('envelopes.question_title', { title: q.title.default })}</Text>
<SelectedOptions question={q} questionIndex={i} votes={votePackage.votes} />
<EnvelopeErrorBoundary question={q}>
<Text sx={styles.title}>{localize('envelopes.question_title', { title: q.title.default })}</Text>
<SelectedOptions question={q} questionIndex={i} votes={votePackage.votes} />
</EnvelopeErrorBoundary>
</chakra.div>
)
})}
Expand Down Expand Up @@ -115,3 +118,45 @@ const SelectedOptions = ({
</>
)
}

type IErrorParams = {
question: IQuestion
}

const EnvelopeError = ({ question }: IErrorParams) => {
const styles = useMultiStyleConfig('Envelope')
const { localize } = useElection()

return (
<Text sx={styles.error}>
{localize('envelopes.error_processing_envelope', { title: question?.title?.default || '' })}
</Text>
)
}

class EnvelopeErrorBoundary extends Component<
IErrorParams & PropsWithChildren,
{
hasError: boolean
}
> {
public state = {
hasError: false,
}

public static getDerivedStateFromError(_: Error) {
return { hasError: true }
}

public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error('Uncaught error:', error, errorInfo)
}

public render() {
if (this.state.hasError) {
return <EnvelopeError question={this.props.question} />
}

return this.props.children
}
}
1 change: 1 addition & 0 deletions packages/chakra-components/src/i18n/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const locales = {
envelopes: {
envelope_abstain_count: 'Abstained {{ count }} times',
question_title: 'Option/s selected in "{{ title }}":',
error_processing_envelope: 'Cannot process envelope data for question "{{ title }}"',
},
errors: {
wrong_data_title: 'Wrong data',
Expand Down
2 changes: 2 additions & 0 deletions packages/chakra-components/src/theme/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const envelopeAnatomy = [
'choiceTitle',
// secret envelope (no results until the end text)
'secret',
// Error boundary message
'error',
]

const { defineMultiStyleConfig, definePartsStyle } = createMultiStyleConfigHelpers(envelopeAnatomy)
Expand Down

0 comments on commit 7df3c60

Please sign in to comment.