Skip to content

Commit

Permalink
(PC-32278)[PRO] feat: cover no venue selection case in income page - …
Browse files Browse the repository at this point in the history
…TESTS BREAK
  • Loading branch information
asaez-pass committed Nov 12, 2024
1 parent 0795146 commit 2e95101
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 94 deletions.
2 changes: 1 addition & 1 deletion pro/src/apiClient/v1/models/StatisticsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
/* eslint-disable */
import type { AggregatedRevenue } from './AggregatedRevenue';
export type StatisticsModel = {
incomeByYear: Record<string, (AggregatedRevenue | Record<string, any>)>;
incomeByYear: Record<string, (AggregatedRevenue | Record<string, never>)>;
};

57 changes: 57 additions & 0 deletions pro/src/commons/utils/factories/statisticsFactories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { StatisticsModel } from "apiClient/v1";

export const statisticsFactory = (): StatisticsModel => {
return {
incomeByYear: {
'2020': {},
'2021': {
revenue: {
total: 0,
individual: 0,
collective: 0,
},
expectedRevenue: {
total: 0,
individual: 0,
collective: 0,
},
},
'2022': {
expectedRevenue: {
total: 0,
individual: 0,
collective: 0,
},
revenue: {
total: 2000.27,
individual: 2000,
collective: 0,
},
},
'2023': {
expectedRevenue: {
total: 0,
individual: 0,
collective: 0,
},
revenue: {
total: 3000,
individual: 1500,
collective: 1500,
},
},
'2024': {
revenue: {
total: 4000,
individual: 2000,
collective: 2000,
},
expectedRevenue: {
total: 5000,
individual: 2500,
collective: 2500,
},
},
}
}
}
28 changes: 23 additions & 5 deletions pro/src/pages/Reimbursements/Income/Income.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IncomeError } from './IncomeError/IncomeError'
import { IncomeNoData } from './IncomeNoData/IncomeNoData'
import { IncomeResultsBox } from './IncomeResultsBox/IncomeResultsBox'
import { IncomeVenueSelector } from './IncomeVenueSelector/IncomeVenueSelector'
import { hasIncomeByYearData } from './utils'

export const Income = () => {
const venueSelectorRef = useRef<HTMLInputElement>(null)
Expand All @@ -32,8 +33,13 @@ export const Income = () => {
} = useIncome(selectedVenues)
const finalActiveYear = activeYear || years[0]
const activeYearIncome = incomeByYear?.[finalActiveYear] || {}
const activeYearHasData =
activeYearIncome.revenue || activeYearIncome.expectedRevenue
const activeYearHasData = hasIncomeByYearData(activeYearIncome)

useEffect(() => {
if (venuesDataReady && selectedVenues.length === 0) {
setSelectedVenues(venues.map((venue) => venue.value))
}
}, [venuesDataReady, venues, selectedVenues])

useEffect(() => {
if (!hasSingleVenue) {
Expand All @@ -49,8 +55,20 @@ export const Income = () => {

const onChange = useCallback(
(formiklySelectedVenues: string[]) => {
if (!isEqual(formiklySelectedVenues, selectedVenues)) {
const selectedVenuesWereInitiated = selectedVenues.length > 0
const formiklySelectionIsNotEmpty = formiklySelectedVenues.length > 0
const parentStateIsOutdated = !isEqual(
formiklySelectedVenues,
selectedVenues
)

if (
selectedVenuesWereInitiated &&
formiklySelectionIsNotEmpty &&
parentStateIsOutdated
) {
setSelectedVenues(formiklySelectedVenues)
setActiveYear(undefined)
}
},
[selectedVenues]
Expand All @@ -62,7 +80,7 @@ export const Income = () => {
{!venuesDataReady && (
<>
{areVenuesLoading ? (
<Spinner />
<Spinner testId="venues-spinner" />
) : venuesApiError ? (
<IncomeError />
) : (
Expand Down Expand Up @@ -125,7 +143,7 @@ export const Income = () => {
{!incomeDataReady && (
<>
{isIncomeLoading ? (
<Spinner />
<Spinner testId="income-spinner"/>
) : incomeApiError ? (
<IncomeError />
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fullHelpIcon from 'icons/full-help.svg'
import { BoxRounded } from 'ui-kit/BoxRounded/BoxRounded'
import { Button } from 'ui-kit/Button/Button'
import { ButtonVariant } from 'ui-kit/Button/types'
import { hasRevenueData } from '../utils'

import styles from './IncomeResultsBox.module.scss'

Expand Down Expand Up @@ -42,11 +43,11 @@ const IncomeResultsSubBox = ({ title, number, help }: IncomeSubBoxProps) => {

type IncomeResultsBoxProps = {
type: keyof AggregatedRevenue
income?: Revenue
income: Revenue
}

export const IncomeResultsBox = ({ type, income }: IncomeResultsBoxProps) => {
if (!income) {
if (!hasRevenueData(income)) {
return null
}

Expand All @@ -58,7 +59,7 @@ export const IncomeResultsBox = ({ type, income }: IncomeResultsBoxProps) => {
type === 'revenue'
? 'Montant des réservations validées et remboursées.'
: 'Montant des réservations en cours, validées et remboursées.'
const shouldDisplayIncomeDetails = income.individual && income.collective
const shouldDisplayIncomeDetails = income.individual !== 0 && income.collective !== 0

return (
<BoxRounded
Expand All @@ -72,13 +73,13 @@ export const IncomeResultsBox = ({ type, income }: IncomeResultsBoxProps) => {
/>
{shouldDisplayIncomeDetails && (
<div className={styles['income-results-box-subbox']}>
{income.individual && (
{income.individual !== 0 && (
<IncomeResultsSubBox
title="Part individuelle"
number={income.individual}
/>
)}
{income.collective && (
{income.collective !== 0 && (
<IncomeResultsSubBox
title="Part collective"
number={income.collective}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FormikProvider, useFormik } from 'formik'
import { useEffect, ForwardedRef } from 'react'
import { useDebounce } from 'use-debounce'
import * as yup from 'yup'

import { SelectAutocomplete } from 'ui-kit/form/SelectAutoComplete/SelectAutocomplete'

Expand All @@ -23,10 +24,23 @@ export const IncomeVenueSelector = ({
refForInput,
}: IncomeVenueSelectorProps) => {
const formik = useFormik<VenueFormValues>({
// SelectAutocomplete has two fields:
// - selectedVenues: for the <select> menu & SelectedValuesTags
// - 'search-selectedVenues': for the search input
initialValues: {
selectedVenues: venues.map((v) => v.value),
'search-selectedVenues': '',
},
validationSchema: yup.object().shape({
selectedVenues: yup.array().test({
name: 'selectedVenues',
message: 'Vous devez sélectionner au moins un partenaire',
test: (value) => {
return (value || []).length > 0
},
}),
'search-selectedVenues': yup.string(),
}),
onSubmit: () => {},
})

Expand Down
Loading

0 comments on commit 2e95101

Please sign in to comment.