feat: Implémentation du système de vote sur les versions
- Ajout des fonctions de vote dans directus.js (handleVote, getUserVote) - Intégration des boutons de vote dans version-comparison.js - Support de l'annulation de vote par double-clic - Gestion des retours visuels (succès/erreur) - Passage des props nécessaires dans list-versions.js
This commit is contained in:
+98
-1
@@ -2,7 +2,7 @@
|
||||
import {
|
||||
createDirectus, rest, authentication, withToken, createItem,
|
||||
readUser, createContentVersion, readContentVersions, saveToContentVersion,
|
||||
readContentVersion,
|
||||
readContentVersion, readItems, updateItem, deleteItem,
|
||||
compareContentVersion
|
||||
} from '@directus/sdk'
|
||||
import {signOut} from 'next-auth/react'
|
||||
@@ -241,3 +241,100 @@ export async function createVersion({
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleVote({
|
||||
accessToken,
|
||||
userId,
|
||||
versionId,
|
||||
voteValue
|
||||
}) {
|
||||
try {
|
||||
await handleUserStatus(accessToken, userId)
|
||||
|
||||
const existingVotes = await directusClient.request(
|
||||
withToken(
|
||||
accessToken,
|
||||
readItems('votes', {
|
||||
filter: {
|
||||
_and: [
|
||||
{content_version_id: {_eq: versionId}},
|
||||
{user_created: {_eq: userId}}
|
||||
]
|
||||
}
|
||||
})
|
||||
)
|
||||
)
|
||||
|
||||
if (existingVotes && existingVotes.length > 0) {
|
||||
const existingVote = existingVotes[0]
|
||||
|
||||
if (existingVote.vote === voteValue) {
|
||||
await directusClient.request(
|
||||
withToken(
|
||||
accessToken,
|
||||
deleteItem('votes', existingVote.id)
|
||||
)
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const vote = {
|
||||
content_version_id: versionId,
|
||||
vote: voteValue
|
||||
}
|
||||
|
||||
await directusClient.request(
|
||||
withToken(
|
||||
accessToken,
|
||||
updateItem('votes', existingVote.id, vote)
|
||||
)
|
||||
)
|
||||
return voteValue
|
||||
}
|
||||
|
||||
const vote = {
|
||||
content_version_id: versionId,
|
||||
vote: voteValue
|
||||
}
|
||||
|
||||
await directusClient.request(
|
||||
withToken(
|
||||
accessToken,
|
||||
createItem('votes', vote)
|
||||
)
|
||||
)
|
||||
return voteValue
|
||||
} catch (error) {
|
||||
console.error('Error voting:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export async function getUserVote({
|
||||
accessToken,
|
||||
userId,
|
||||
versionId
|
||||
}) {
|
||||
try {
|
||||
await handleUserStatus(accessToken, userId)
|
||||
|
||||
const existingVotes = await directusClient.request(
|
||||
withToken(
|
||||
accessToken,
|
||||
readItems('votes', {
|
||||
filter: {
|
||||
_and: [
|
||||
{content_version_id: {_eq: versionId}},
|
||||
{user_created: {_eq: userId}}
|
||||
]
|
||||
}
|
||||
})
|
||||
)
|
||||
)
|
||||
|
||||
return existingVotes && existingVotes.length > 0 ? existingVotes[0].vote : null
|
||||
} catch (error) {
|
||||
console.error('Error fetching vote:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user