Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
botisSmile committed Dec 15, 2023
1 parent 6d8d4ca commit 40e34ec
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ function ProductsCategoryPreview(props: IProps): JSX.Element {
onTopProductsLoaded,
} = props

onProductsLoaded(0)
onTopProductsLoaded(0)

const positionVariables = useMemo(
() => ({
localizedCatalogId: Number(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function ProductsPreviewBottom(props: IProps): JSX.Element {
const tableRows =
(products?.data?.products?.collection as unknown as ITableRow[]) ?? []

if (onProductsLoaded && products.status === LoadStatus.SUCCEEDED) {
if (typeof onProductsLoaded === 'function' && products.status === LoadStatus.SUCCEEDED) {
onProductsLoaded(products.data.products.paginationInfo.totalCount)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ interface IProps {

function ProductsSearchPreview(props: IProps): JSX.Element {
const { variables, configuration, limitationType, onProductsLoaded } = props
onProductsLoaded(0)

return (
<ProductsPreviewBottom
Expand Down
34 changes: 22 additions & 12 deletions packages/components/src/pages/admin/analyze/CatalogStructure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,21 @@ import ProductsCategoryPreview from '../../../components/stateful/ProductPreview

import { Paper } from '@mui/material'
import MerchandiseBar from '../../../components/stateful/ProductPreview/MerchandiseBar'
import {DropDownError, InputTextError} from "../../../components";
import {DropDown, InputText} from "../../../components";

/**
* Todo Raf:
* - OK - Tableau vide
* - OK - bug quand on cherche "zivab" on a de résulat, mais on voit des produits dans la Merchandize bag
* - OK - bug entre l'affichage de deux catgories on voit les résultat de la précédent catgory
* - faire la gestion des erreurs dans le formulaire de recherche, rendre champs obligatoire ?
* - la value ne se vide pas dans le champs de seearch car il est obligatoire
* - voir les todo
* - Enlever les console.log()
* - voir les todo, Enlever les console.log()
* - tests unitaire
* - nouveau endpoint pour les limitations ?
* - refactoriser + découper ce fichier ?
* - Créer le ticket
* enlever les request types product_ et ajouter des constantes
*
* Modifer:
* le front (call products + typage type script)
* connecteurs ?
* tests unitaire côté back
*/
const pagesSlug = ['analyze', 'catalog_structure']

Expand Down Expand Up @@ -141,6 +142,8 @@ function AdminAnalyzeCatalogStructure(): JSX.Element {
function handleSubmit(event: FormEvent<HTMLFormElement>): void {
event.preventDefault()
if (isValidVariables(variables, limitationType)) {
setNbResults(0)
setNbTopProducts(0)
setVariableValid(true)
} else {
setVariableValid(false)
Expand All @@ -151,6 +154,10 @@ function AdminAnalyzeCatalogStructure(): JSX.Element {
return null
}

if (!variables.requestType && requestTypeOptions[0]) {
setVariables({...variables, requestType: requestTypeOptions[0].value ? String(requestTypeOptions[0].value) : undefined })
}

return (
<>
<PageTitle
Expand All @@ -159,22 +166,22 @@ function AdminAnalyzeCatalogStructure(): JSX.Element {
/>
<form onSubmit={handleSubmit}>
<WrapperBlock>
<DropDownError
<DropDown
infoTooltip={t('select.tooltip')}
label={t('Scope')}
name="localizedCatalog"
onChange={handleChange}
options={localizedCatalogOptions}
value={variables?.localizedCatalog}
// value={catConf?.defaultSorting ?? ''}
useGroups
placeholder={t('Select a scope')}
required
sx={{width:196}}
/>
</WrapperBlock>

<WrapperBlock>
<DropDownError
<DropDown
infoTooltip={t('select.tooltip')}
label={t('Context')}
name="requestType"
Expand All @@ -185,6 +192,7 @@ function AdminAnalyzeCatalogStructure(): JSX.Element {
useGroups
placeholder={t('Select a scope')}
required
sx={{width:196}}
/>
{limitationType === 'category' && (
<TreeSelector
Expand All @@ -195,17 +203,19 @@ function AdminAnalyzeCatalogStructure(): JSX.Element {
value={variables?.category ?? null}
placeholder={t('Select a category')}
required
sx={{width:196}}
/>
)}

{limitationType === 'search' && (
<InputTextError
<InputText
label={t('search')}
name="search"
onChange={handleChange}
value={variables?.search ? String(variables.search) : undefined}
placeholder={t('Search')}
required
sx={{width:196}}
/>
)}
{limitationType ? (
Expand Down
84 changes: 20 additions & 64 deletions packages/shared/src/services/explain.test.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,26 @@
import {isValidVariables} from "./explain";
import {cleanExplainGraphQLVariables, isGraphQLValidVariables} from "./explain";

describe('Explain service', () => {
describe('isValidVariables', () => {
it('Test if ', () => {
expect(isValidVariables({requestType:'product_catalog', search:'', localizedCatalog:'1'}, 'search')).toEqual('Hello there')
})
})

describe('firstLetterLowercase', () => {
it('Should set first letter to lowercase', () => {
expect(firstLetterLowercase('Hello there')).toEqual('hello there')
})
})

describe('getNameFromDefault', () => {
it('Should get the name without the default part', () => {
expect(getNameFromDefault('defaultMaxSize')).toEqual('maxSize')
})
})

describe('humanize', () => {
it('Should humanize the label', () => {
expect(humanize('defaultLabel')).toEqual('Default label')
})
})

describe('getHeadTitle', () => {
it('Should add Gally at the end of string getHeadTitle', () => {
expect(getHeadTitle('Catégories')).toEqual('Catégories - Gally')
})
})

describe('getFieldLabelTranslationArgs', () => {
it('Should return field label (args for translation)', () => {
expect(getFieldLabelTranslationArgs('defaultLabel')).toEqual([
'fields.defaultLabel',
'Default label',
])
expect(getFieldLabelTranslationArgs('defaultLabel', 'metadata')).toEqual([
'resources.metadata.fields.defaultLabel',
'Default label',
])
})
})

describe('addPrefixKeyObject', () => {
it('Should add the prefix "category", on all keys of the object', () => {
expect(
addPrefixKeyObject(
{ localizedCatalog: 'com_fr', search: 'bag' },
'category'
)
).toEqual({ categoryLocalizedCatalog: 'com_fr', categorySearch: 'bag' })
})
})

describe('formatPrice', () => {
it('Should format price', () => {
expect(formatPrice(100, 'USD', 'fr-FR')).toContain('100,00')
expect(formatPrice(100, 'USD', 'fr-FR')).toContain('$US')
})
})
describe('getIdFromIri', () => {
it('Should format price', () => {
expect(getIdFromIri('/localized_catalog/1')).toEqual('1')
expect(getIdFromIri('/localized_catalog/random/10')).toEqual('10')
it('Should test if explain GraphQL variables are valid ', () => {
expect(isGraphQLValidVariables({
requestType:'product_catalog', search:'bag', localizedCatalog:'1'
}, 'search')).toEqual(true)
expect(isGraphQLValidVariables({
requestType:'product_catalog', search:'', localizedCatalog:'1'
}, 'search')).toEqual(false)
expect(isGraphQLValidVariables({
requestType:'product_catalog', search:'', localizedCatalog:''
}, 'search')).toEqual(false)
})
})

describe('cleanExplainGraphQLVariables', () => {
it('Should clean explain GraphQL variables', () => {
expect(cleanExplainGraphQLVariables({
requestType:'product_catalog', search:'bag', localizedCatalog:'1', category: { id: 'cat_7', name: 'Bag', level: 2, isVirtual: false, path: 'cat_2/cat_8' }
}, 'search')).toEqual({
requestType:'product_catalog', search:'bag', localizedCatalog:'1', category: { id: 'cat_7', name: 'Bag', level: 2, isVirtual: false, path: 'cat_2/cat_8' }})
})
})
})
16 changes: 5 additions & 11 deletions packages/shared/src/services/explain.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import { IExplainVariables } from '../types/explain'

export function isValidVariables(
export function isGraphQLValidVariables(
variables: IExplainVariables,
limitationType: string
): boolean {
// todo tests non valide car valeur peut être null par défaut.
let isValid =
variables.localizedCatalog !== undefined &&
variables.requestType !== undefined
let isValid = Boolean(variables?.localizedCatalog) && Boolean(variables?.requestType)
if (limitationType === 'category') {
isValid = isValid && variables.category !== undefined
isValid = isValid && Boolean(variables?.category)
} else if (limitationType === 'search') {
isValid =
isValid &&
variables.search !== undefined &&
variables.search.trim() !== ''
isValid = isValid && Boolean(variables?.search?.trim())
}

return isValid
}

export function cleanExplainVariables(
export function cleanExplainGraphQLVariables(
variables: IExplainVariables,
limitationType: string
): IExplainVariables {
Expand Down

0 comments on commit 40e34ec

Please sign in to comment.