-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from aleinin/improve-csv-export
Improve csv export
- Loading branch information
Showing
6 changed files
with
224 additions
and
69 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
29 changes: 29 additions & 0 deletions
29
goty-client/src/components/results/ExportButton/ExportButton.tsx
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,29 @@ | ||
import styles from '../ResultsPage.module.scss' | ||
import { Export } from '../../../icons/export/Export' | ||
import { Button } from '../../controls/Button/Button' | ||
import React from 'react' | ||
import { downloadCSV } from '../downloadCSV' | ||
import { useSelector } from 'react-redux' | ||
import { | ||
selectResults, | ||
selectSubmissions, | ||
} from '../../../state/results/selectors' | ||
import { selectProperties } from '../../../state/properties/selectors' | ||
import { createExportData } from './createExportData' | ||
|
||
export const ExportButton = () => { | ||
const results = useSelector(selectResults) | ||
const submissions = useSelector(selectSubmissions) | ||
const properties = useSelector(selectProperties) | ||
const handleExport = () => { | ||
downloadCSV( | ||
`goty-${properties.year}-results.csv`, | ||
createExportData(results, submissions, properties), | ||
) | ||
} | ||
return ( | ||
<Button isIcon className={styles.exportButton} onClick={handleExport}> | ||
<Export /> | ||
</Button> | ||
) | ||
} |
138 changes: 138 additions & 0 deletions
138
goty-client/src/components/results/ExportButton/createExportData.ts
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,138 @@ | ||
import { GameResult } from '../../../models/gameResult' | ||
import { GameOfTheYearResult } from '../../../models/gameOfTheYearResult' | ||
import { Game } from '../../../models/game' | ||
import { Results } from '../../../models/results' | ||
import { Submission } from '../../../models/submission' | ||
import { Properties } from '../../../models/properties' | ||
import { indexToOrdinal } from '../../../util/index-to-ordinal' | ||
import { Column, Section } from '../downloadCSV' | ||
|
||
const gameColumns: Column<GameResult>[] = [ | ||
{ | ||
label: 'Rank', | ||
accessorFn: (game: GameResult) => game.rank + 1, | ||
}, | ||
{ | ||
label: 'Title', | ||
accessorKey: 'title', | ||
}, | ||
{ | ||
label: 'Votes', | ||
accessorKey: 'votes', | ||
}, | ||
] | ||
const gotyColumns: Column<GameOfTheYearResult>[] = [ | ||
...gameColumns, | ||
{ | ||
label: 'Points', | ||
accessorKey: 'points', | ||
}, | ||
] | ||
const gameAccessor = (game: Game | null) => game?.title ?? '' | ||
|
||
const createTextSection = (label: string, data: any[]): Section<any> => ({ | ||
columns: [{ label }], | ||
data, | ||
}) | ||
|
||
const createGameSection = ( | ||
title: string, | ||
data: GameResult[], | ||
): Section<GameResult> => ({ | ||
title, | ||
columns: gameColumns, | ||
data, | ||
}) | ||
|
||
const createGotySection = ( | ||
title: string, | ||
data: GameOfTheYearResult[], | ||
): Section<GameOfTheYearResult> => ({ | ||
title, | ||
columns: gotyColumns, | ||
data, | ||
}) | ||
|
||
const createPropertiesSection = ( | ||
title: string, | ||
data: Properties, | ||
): Section<Properties> => ({ | ||
title, | ||
data, | ||
columns: [ | ||
{ | ||
label: 'gotyYear', | ||
accessorKey: 'year', | ||
}, | ||
{ | ||
label: 'deadline', | ||
accessorKey: 'deadline', | ||
}, | ||
{ | ||
label: 'hasGiveaway', | ||
accessorKey: 'hasGiveaway', | ||
}, | ||
{ | ||
label: 'giveawayAmountUSD', | ||
accessorKey: 'giveawayAmountUSD', | ||
}, | ||
], | ||
}) | ||
|
||
const createSubmissionsSection = ( | ||
title: string, | ||
data: Submission[], | ||
maxGamesOfTheYear: number, | ||
): Section<Submission> => ({ | ||
title, | ||
data, | ||
columns: [ | ||
{ | ||
label: 'ID', | ||
accessorKey: 'submissionUUID', | ||
}, | ||
{ | ||
label: 'Name', | ||
accessorKey: 'name', | ||
}, | ||
{ | ||
label: 'Best Old Game', | ||
accessorFn: (submission: Submission) => | ||
gameAccessor(submission.bestOldGame), | ||
}, | ||
{ | ||
label: 'Most Anticipated', | ||
accessorFn: (submission: Submission) => | ||
gameAccessor(submission.mostAnticipated), | ||
}, | ||
{ | ||
label: 'Entered Giveaway', | ||
accessorFn: (submission: Submission) => | ||
submission.enteredGiveaway ? 'Yes' : 'No', | ||
}, | ||
...[...Array(maxGamesOfTheYear)].map((_, index) => ({ | ||
label: indexToOrdinal(index), | ||
accessorFn: (submission: Submission) => | ||
submission.gamesOfTheYear[index]?.title ?? '', | ||
})), | ||
], | ||
}) | ||
|
||
export const createExportData = ( | ||
results: Results, | ||
submissions: Submission[], | ||
properties: Properties, | ||
) => [ | ||
createTextSection('Respondents', results.participants), | ||
createGotySection('Games of the Year', results.gamesOfTheYear), | ||
createGameSection('Old Game', results.bestOldGames), | ||
createGameSection('Most Anticipated', results.mostAnticipated), | ||
createTextSection('Giveaway Entries', results.giveawayParticipants), | ||
createSubmissionsSection( | ||
'Submissions', | ||
submissions, | ||
properties.maxGamesOfTheYear, | ||
), | ||
createPropertiesSection('Properties', properties), | ||
createTextSection('Tie points', properties.tiePoints), | ||
] |
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