-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves data fetching to a separate class
- Loading branch information
1 parent
c68fcdd
commit e2d5955
Showing
5 changed files
with
52 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] } } | ||
} | ||
} |
e2d5955
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
enquete-bbb – ./
enquete-bbb-git-main-luccasmaso.vercel.app
votacao-bbb.vercel.app
www.enquetebbb.app
enquetebbb.app
enquete-bbb-luccasmaso.vercel.app