feat: récupère le total des votes
This commit is contained in:
@@ -24,7 +24,7 @@ import ShareButton from './share-button.js'
|
||||
import ExportPdfButton from './export-pdf-button.js'
|
||||
import PrintButton from './print-button.js'
|
||||
import {formatDate} from '@/lib/format.js'
|
||||
import {compareVersion} from '@/lib/directus.js'
|
||||
import {compareVersion, getVoteCounts} from '@/lib/directus.js'
|
||||
import {filterVersions, getFilterStats} from '@/lib/version-utils.js'
|
||||
|
||||
const columns = [
|
||||
@@ -85,7 +85,8 @@ function rowContent({
|
||||
setIsErrorAlertOpen,
|
||||
setIsOpenComparison,
|
||||
setVersionCompare,
|
||||
outdatedStatusMap
|
||||
outdatedStatusMap,
|
||||
voteCountsMap
|
||||
}) {
|
||||
const handleButtonClick = async versionId => {
|
||||
const version = await compareVersion({
|
||||
@@ -104,6 +105,7 @@ function rowContent({
|
||||
}
|
||||
|
||||
const isOutdated = outdatedStatusMap[row.id] || false
|
||||
const voteCounts = voteCountsMap[row.id] || null
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -141,12 +143,14 @@ function rowContent({
|
||||
<ExportPdfButton
|
||||
versionData={row}
|
||||
isOutdated={isOutdated}
|
||||
voteCounts={voteCounts}
|
||||
size='small'
|
||||
variant='text'
|
||||
/>
|
||||
<PrintButton
|
||||
versionData={row}
|
||||
isOutdated={isOutdated}
|
||||
voteCounts={voteCounts}
|
||||
size='small'
|
||||
variant='text'
|
||||
/>
|
||||
@@ -188,11 +192,13 @@ export default function ListVersions({
|
||||
status: ''
|
||||
})
|
||||
const [outdatedStatusMap, setOutdatedStatusMap] = useState({})
|
||||
const [voteCountsMap, setVoteCountsMap] = useState({})
|
||||
|
||||
// Fetch outdated status for all versions
|
||||
// Fetch outdated status and vote counts for all versions
|
||||
useEffect(() => {
|
||||
async function fetchOutdatedStatus() {
|
||||
async function fetchVersionsData() {
|
||||
const statusMap = {}
|
||||
const countsMap = {}
|
||||
|
||||
await Promise.all(
|
||||
data.map(async version => {
|
||||
@@ -209,18 +215,27 @@ export default function ListVersions({
|
||||
if (comparisonData) {
|
||||
statusMap[version.id] = comparisonData.outdated || false
|
||||
}
|
||||
|
||||
// Fetch vote counts
|
||||
const counts = await getVoteCounts({
|
||||
accessToken,
|
||||
versionId: version.id
|
||||
})
|
||||
countsMap[version.id] = counts
|
||||
} catch (error) {
|
||||
console.warn(`Failed to fetch outdated status for version ${version.id}:`, error)
|
||||
console.warn(`Failed to fetch data for version ${version.id}:`, error)
|
||||
statusMap[version.id] = false
|
||||
countsMap[version.id] = {positive: 0, negative: 0, total: 0}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
setOutdatedStatusMap(statusMap)
|
||||
setVoteCountsMap(countsMap)
|
||||
}
|
||||
|
||||
if (data.length > 0) {
|
||||
fetchOutdatedStatus()
|
||||
fetchVersionsData()
|
||||
}
|
||||
}, [data, accessToken, userId, countdownRef, setError, setIsErrorAlertOpen])
|
||||
|
||||
@@ -230,6 +245,19 @@ export default function ListVersions({
|
||||
|
||||
const versionData = data.find(({id}) => id === versionCompare?.versionId)
|
||||
|
||||
// Function to refresh vote counts for a specific version after voting
|
||||
const refreshVoteCounts = async versionId => {
|
||||
try {
|
||||
const counts = await getVoteCounts({
|
||||
accessToken,
|
||||
versionId
|
||||
})
|
||||
setVoteCountsMap(prev => ({...prev, [versionId]: counts}))
|
||||
} catch (error) {
|
||||
console.warn(`Failed to refresh vote counts for version ${versionId}:`, error)
|
||||
}
|
||||
}
|
||||
|
||||
const handleSearchChange = newSearchTerm => {
|
||||
setSearchTerm(newSearchTerm)
|
||||
}
|
||||
@@ -305,7 +333,7 @@ export default function ListVersions({
|
||||
components={VirtuosoTableComponents}
|
||||
fixedHeaderContent={fixedHeaderContent}
|
||||
itemContent={(index, row) => rowContent({
|
||||
index, row, accessToken, userId, countdownRef, setError, setIsErrorAlertOpen, setIsOpenComparison, setVersionCompare, outdatedStatusMap
|
||||
index, row, accessToken, userId, countdownRef, setError, setIsErrorAlertOpen, setIsOpenComparison, setVersionCompare, outdatedStatusMap, voteCountsMap
|
||||
})}
|
||||
/>
|
||||
</Paper>
|
||||
@@ -316,13 +344,20 @@ export default function ListVersions({
|
||||
userId={userId}
|
||||
setError={setError}
|
||||
setIsErrorAlertOpen={setIsErrorAlertOpen}
|
||||
onVoteSuccess={refreshVoteCounts}
|
||||
/>
|
||||
)
|
||||
)}
|
||||
</Box>
|
||||
|
||||
{isOpenComparison && (
|
||||
<VersionDialog versionData={versionData} versionCompare={versionCompare} isOpen={isOpenComparison} setIsOpen={setIsOpenComparison} />
|
||||
<VersionDialog
|
||||
versionData={versionData}
|
||||
versionCompare={versionCompare}
|
||||
isOpen={isOpenComparison}
|
||||
setIsOpen={setIsOpenComparison}
|
||||
onVoteSuccess={refreshVoteCounts}
|
||||
/>
|
||||
)}
|
||||
<SessionExpired ref={countdownRef} setError={setError} setIsErrorAlertOpen={setIsErrorAlertOpen} />
|
||||
</>
|
||||
|
||||
Reference in New Issue
Block a user