refactor: Add CreateForm to write comments
This commit is contained in:
+41
-1
@@ -1,4 +1,7 @@
|
||||
import {createDirectus, rest, authentication} from '@directus/sdk'
|
||||
import {
|
||||
createDirectus, rest, authentication, withToken, createItem
|
||||
} from '@directus/sdk'
|
||||
import {hasRestrictedChar} from './format.js'
|
||||
|
||||
const apiUrl = process.env.DIRECTUS_API_URL || process.env.NEXT_PUBLIC_DIRECTUS_API_URL
|
||||
|
||||
@@ -6,3 +9,40 @@ export const directusClient = createDirectus(apiUrl)
|
||||
.with(authentication('cookie', {credentials: 'include', autoRefresh: true}))
|
||||
.with(rest())
|
||||
|
||||
export async function handleSubmit({
|
||||
accessToken,
|
||||
content,
|
||||
collection,
|
||||
requestObject,
|
||||
setError,
|
||||
setSuccess,
|
||||
setIsErrorAlertOpen,
|
||||
setIsSuccessAlertOpen,
|
||||
countdownRef,
|
||||
}) {
|
||||
try {
|
||||
if (hasRestrictedChar(content)) {
|
||||
setError('Le texte ne doit pas contenir certains caractères spéciaux : <, >, ", .')
|
||||
setIsErrorAlertOpen(true)
|
||||
return
|
||||
}
|
||||
|
||||
await directusClient.request(
|
||||
withToken(
|
||||
accessToken,
|
||||
createItem(collection, requestObject)
|
||||
)
|
||||
)
|
||||
|
||||
setSuccess('Envoyé avec succès. En attente de validation.')
|
||||
setIsSuccessAlertOpen(true)
|
||||
} catch (error) {
|
||||
if (error?.errors[0]?.message === 'Token expired.') {
|
||||
countdownRef.current.startCountdown()
|
||||
} else {
|
||||
console.log(error?.errors[0]?.message)
|
||||
setError(error?.errors[0]?.message)
|
||||
setIsErrorAlertOpen(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,3 +24,17 @@ export function formatDate(date, formatStr = 'PP') {
|
||||
locale: fr
|
||||
})
|
||||
}
|
||||
|
||||
export function hasRestrictedChar(text) {
|
||||
const regex = /[<>&"]/g
|
||||
|
||||
return Boolean(regex.test(text))
|
||||
}
|
||||
|
||||
export function formatFormContent(currentTarget) {
|
||||
const formData = new FormData(currentTarget)
|
||||
const formJson = Object.fromEntries(formData.entries())
|
||||
const {content} = formJson
|
||||
|
||||
return content.replaceAll('\'', '’')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user