Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.

Commit

Permalink
(FIX) Fix quality check
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorEnaud committed Oct 7, 2021
1 parent 011cbaa commit db68b9e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
22 changes: 14 additions & 8 deletions src/components/pages/Offers/Offer/Stocks/EventStocks.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* @debt complexity "Gaël: file over 300 lines"
* @debt complexity "Gaël: the file contains eslint error(s) based on our new config"
* @debt directory "Gaël: this file should be migrated within the new directory structure"
*/
* @debt complexity "Gaël: file over 300 lines"
* @debt complexity "Gaël: the file contains eslint error(s) based on our new config"
* @debt directory "Gaël: this file should be migrated within the new directory structure"
*/

import PropTypes from 'prop-types'
import React, { Fragment, useCallback, useEffect, useMemo, useState } from 'react'
Expand Down Expand Up @@ -112,10 +112,12 @@ const EventStocks = ({
})
}, [])

const areValid = stocks => {
const areValid = (stocks, isEvent, isEducational) => {
const stocksErrors = stocks.reduce((stocksErrors, stock) => {
const isNewStock = stock.id === undefined
const stockErrors = isNewStock ? validateCreatedStock(stock, offer.isEvent, offer.isEducational) : validateUpdatedStock(stock, offer.isEvent, offer.isEducational)
const stockErrors = isNewStock
? validateCreatedStock(stock, isEvent, isEducational)
: validateUpdatedStock(stock, isEvent, isEducational)
const stockHasErrors = Object.keys(stockErrors).length > 0
return stockHasErrors ? { ...stocksErrors, [stock.key]: stockErrors } : stocksErrors
}, {})
Expand All @@ -137,7 +139,7 @@ const EventStocks = ({

const submitStocks = useCallback(() => {
const updatedStocks = existingStocks.filter(stock => stock.updated)
if (areValid([...stocksInCreation, ...updatedStocks])) {
if (areValid([...stocksInCreation, ...updatedStocks], offer.isEvent, offer.isEducational)) {
setIsSendingStocksOfferCreation(true)
const stocksToCreate = stocksInCreation.map(stockInCreation =>
createEventStockPayload(stockInCreation, offer.venue.departementCode)
Expand Down Expand Up @@ -184,6 +186,8 @@ const EventStocks = ({
location,
stocksInCreation,
offer.id,
offer.isEducational,
offer.isEvent,
isOfferDraft,
offer.venue.departementCode,
loadStocks,
Expand Down Expand Up @@ -211,7 +215,9 @@ const EventStocks = ({
</h3>

<div className="cancellation-information">
Les utilisateurs ont un délai de 48h pour annuler leur réservation mais ne peuvent pas le faire moins de 48h avant le début de l’événement. Si la date limite de réservation n’est pas encore passée, la place est alors automatiquement remise en vente.
Les utilisateurs ont un délai de 48h pour annuler leur réservation mais ne peuvent pas le
faire moins de 48h avant le début de l’événement. Si la date limite de réservation n’est pas
encore passée, la place est alors automatiquement remise en vente.
</div>
{hasNoStock ? (
<button
Expand Down
48 changes: 26 additions & 22 deletions src/components/pages/Offers/Offer/Stocks/ThingStocks.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @debt complexity "Gaël: the file contains eslint error(s) based on our new config"
* @debt directory "Gaël: this file should be migrated within the new directory structure"
*/
* @debt complexity "Gaël: the file contains eslint error(s) based on our new config"
* @debt directory "Gaël: this file should be migrated within the new directory structure"
*/

import PropTypes from 'prop-types'
import React, { Fragment, useCallback, useEffect, useState } from 'react'
Expand Down Expand Up @@ -100,8 +100,28 @@ const ThingStocks = ({
}))
}, [])

const checkStockIsValid = (stock, isEvent, isEducational) => {
const isNewStock = stock.id === undefined
const stockErrors = isNewStock
? validateCreatedStock(stock, isEvent, isEducational)
: validateUpdatedStock(stock, isEvent, isEducational)
const stockHasErrors = Object.keys(stockErrors).length > 0

if (stockHasErrors) {
const formErrors = {
global: 'Une ou plusieurs erreurs sont présentes dans le formulaire.',
...stockErrors,
}
setFormErrors(formErrors)
} else {
setFormErrors({})
}

return !stockHasErrors
}

const submitStocks = useCallback(() => {
if (checkStockIsValid(stock)) {
if (checkStockIsValid(stock, offer.isEvent, offer.isEducational)) {
setEnableSubmitButtonSpinner(true)
const stockToCreateOrEdit = {
...createThingStockPayload(stock, offer.venue.departementCode),
Expand Down Expand Up @@ -161,6 +181,8 @@ const ThingStocks = ({
history,
location,
offer.id,
offer.isEducational,
offer.isEvent,
isOfferDraft,
offer.venue.departementCode,
loadStocks,
Expand All @@ -169,24 +191,6 @@ const ThingStocks = ({
showErrorNotification,
])

const checkStockIsValid = stock => {
const isNewStock = stock.id === undefined
const stockErrors = isNewStock ? validateCreatedStock(stock, offer.isEvent, offer.isEducational) : validateUpdatedStock(stock, offer.isEvent, offer.isEducational)
const stockHasErrors = Object.keys(stockErrors).length > 0

if (stockHasErrors) {
const formErrors = {
global: 'Une ou plusieurs erreurs sont présentes dans le formulaire.',
...stockErrors,
}
setFormErrors(formErrors)
} else {
setFormErrors({})
}

return !stockHasErrors
}

if (isLoading) {
return null
}
Expand Down

0 comments on commit db68b9e

Please sign in to comment.