Skip to content

Commit

Permalink
Moves data fetching to a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
luccasmaso committed Jan 30, 2023
1 parent c68fcdd commit e2d5955
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lib/Components/Voting/VotingCardView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { motion } from 'framer-motion'
import { total } from './Animations'
import { getParticipantImage } from '../../Helpers/ParticipantImage'
import { Result } from './Types'
import { Result } from '../../Data/Types'
import ProgressCircle from './ProgressCircle'
import { formatNumber } from '../../Helpers/NumberFormat'
import CounterView from './CounterView'
Expand Down
2 changes: 1 addition & 1 deletion lib/Components/Voting/VotingView.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { motion } from 'framer-motion'
import { cards, card, header, text, footer } from './Animations'
import { Result } from './Types'
import { Result } from '../../Data/Types'
import VotingCardView from './VotingCardView'
import MoodView from './MoodView'

Expand Down
41 changes: 41 additions & 0 deletions lib/Data/Results.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { DynamoDBClient, ScanCommand } from '@aws-sdk/client-dynamodb'
import { Result } from './Types'

export default class Results {
client?: DynamoDBClient

constructor() {
this.client = new DynamoDBClient({
region: "us-east-1",
credentials: {
accessKeyId: `${process.env.ACCESS_KEY}`,
secretAccessKey: `${process.env.SECRET_KEY}`,
},
})
}

async list(): Promise<Result[] | undefined> {
try {
const data = await this.client!.send(new ScanCommand({
TableName: "enquete-bbb-results"
}))

const results = data.Items?.map((element) => {
return {
id: element.id.S,
totalVotes: parseInt(element.totalVotes.N || '0'),
status: element.status.S,
participants: element.participants.L?.map(participant => ({
name: participant.M?.name.S,
votesPercentage: parseFloat(participant.M?.votesPercentage.N || '0')
})),
}
}) || []

results
} catch (error) {
return undefined
}
}

}
File renamed without changes.
48 changes: 9 additions & 39 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,18 @@
import { DynamoDBClient, ScanCommand } from '@aws-sdk/client-dynamodb'
import { Result } from '../lib/Components/Voting/Types'

import { Result } from '../lib/Data/Types'
import VotingView from '../lib/Components/Voting/VotingView'
import Results from '../lib/Data/Results'

export default function Index(props: { results: Result[] }) {
return (
<VotingView results={props.results} />
)
return <VotingView results={props.results} />
}

export async function getStaticProps() {
const client = new DynamoDBClient({
region: "us-east-1",
credentials: {
accessKeyId: `${process.env.ACCESS_KEY}`,
secretAccessKey: `${process.env.SECRET_KEY}`,
},
})

try {
const data = await client.send(new ScanCommand({
TableName: "enquete-bbb-results"
}))

const results = data.Items?.map((element) => {
return {
id: element.id.S,
totalVotes: parseInt(element.totalVotes.N || '0'),
status: element.status.S,
participants: element.participants.L?.map(participant => ({
name: participant.M?.name.S,
votesPercentage: parseFloat(participant.M?.votesPercentage.N || '0')
})),
}
}) || []
const results = await new Results().list()

return {
props: {
results
},
revalidate: 60 * 15, // In seconds
}
} catch (error) {
return {
props: { results: [] }
}
if (results) {
return { props: { results }, revalidate: 60 * 15 }
} else {
return { props: { results: [] } }
}
}

1 comment on commit e2d5955

@vercel
Copy link

@vercel vercel bot commented on e2d5955 Jan 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.