Skip to content

Commit

Permalink
Merge pull request #244 from Sterbenfr/feat/modify
Browse files Browse the repository at this point in the history
feat(prestataire): adding required in modify
  • Loading branch information
Aurelienschmi authored Jul 29, 2024
2 parents 3265566 + 3865274 commit 4e0f3cb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 39 deletions.
2 changes: 1 addition & 1 deletion app/api/dons/[donsID]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function DELETE(
}

try {
const query = 'DELETE FROM `dons` WHERE `code_Don` = ?'
const query = 'DELETE FROM `Dons` WHERE `code_Don` = ?'
const [rows] = await connection.query(query, donsID)
return NextResponse.json(rows)
} catch (error) {
Expand Down
116 changes: 78 additions & 38 deletions app/prestataire/[prestataireID]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function PrestatairePage({ params }: { params: { prestataireID: string } }) {
const [modifiedPrestataire, setModifiedPrestataire] = useState<
Partial<PrestataireID>
>({})
let [canSubmit] = useState<boolean>(true)

useEffect(() => {
const fetchSessionAndPrestataire = async () => {
Expand Down Expand Up @@ -107,41 +108,6 @@ function PrestatairePage({ params }: { params: { prestataireID: string } }) {
: new Date().toISOString().split('T')[0]
}

const handleSubmit = async () => {
const jsonPayload = {
...modifiedPrestataire,
}

// Convert non-file data to JSON
const body = JSON.stringify(jsonPayload)

try {
const res = await fetch(
`../../api/prestataire/${params.prestataireID}`,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: body,
},
)

if (!res.ok) {
const errorDetail = await res.text()
console.error('Failed to update data:', errorDetail)
throw new Error('Failed to update data')
}

const updatedPrestataire: PrestataireID[] = await res.json()
setPrestataire(updatedPrestataire)
setModify(false)
} catch (error) {
console.error('Error submitting form:', error)
}
window.location.reload()
}

if (
!Array.isArray(prestataire) ||
prestataire.length === 0 ||
Expand All @@ -153,6 +119,79 @@ function PrestatairePage({ params }: { params: { prestataireID: string } }) {
</div>
)

const requiredValue = () => {
const keysToCheck = [
'raison_sociale',
'TP_libelle',
'mail_contact_prestataire',
'telephone_contact_prestataire',
]

keysToCheck.forEach(key => {
if (
modifiedPrestataire[key as keyof PrestataireID] === null ||
modifiedPrestataire[key as keyof PrestataireID] === ''
) {
setModifiedPrestataire(prevState => ({
...prevState,
[key]: prestataire[0][key as keyof PrestataireID],
}))
}
})
}

const handleSubmit = async () => {
requiredValue()

// Vérifier les valeurs après avoir réinitialisé les champs requis
if (
!modifiedPrestataire.raison_sociale ||
modifiedPrestataire.raison_sociale.trim() === '' ||
!(
modifiedPrestataire.telephone_contact_prestataire ||
modifiedPrestataire.mail_contact_prestataire
)
) {
canSubmit = false;
} else {
canSubmit = true;
}

if (canSubmit) {
const jsonPayload = {
...modifiedPrestataire,
}
// Convert non-file data to JSON
const body = JSON.stringify(jsonPayload)

try {
const res = await fetch(
`../../api/prestataire/${params.prestataireID}`,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: body,
},
)

if (!res.ok) {
const errorDetail = await res.text()
console.error('Failed to update data:', errorDetail)
throw new Error('Failed to update data')
}

const updatedPrestataire: PrestataireID[] = await res.json()
setPrestataire(updatedPrestataire)
setModify(false)
} catch (error) {
console.error('Error submitting form:', error)
}
window.location.reload()
}
}

const initialValue = () => {
const keysToCheck = [
'raison_sociale',
Expand Down Expand Up @@ -308,7 +347,8 @@ function PrestatairePage({ params }: { params: { prestataireID: string } }) {
type='input'
name='raison_sociale'
value={
modifiedPrestataire.raison_sociale
modifiedPrestataire.raison_sociale ??
''
}
placeholder={
prestataire[0].raison_sociale ===
Expand Down Expand Up @@ -603,7 +643,7 @@ function PrestatairePage({ params }: { params: { prestataireID: string } }) {
type='number'
name='telephone_contact_prestataire'
value={
modifiedPrestataire.telephone_contact_prestataire
modifiedPrestataire.telephone_contact_prestataire ?? ''
}
placeholder={
prestataire[0]
Expand Down Expand Up @@ -651,7 +691,7 @@ function PrestatairePage({ params }: { params: { prestataireID: string } }) {
type='mail'
name='mail_contact_prestataire'
value={
modifiedPrestataire.mail_contact_prestataire
modifiedPrestataire.mail_contact_prestataire ?? ''
}
placeholder={
prestataire[0]
Expand Down

0 comments on commit 4e0f3cb

Please sign in to comment.