Skip to content

Commit

Permalink
refactor: to use fcuntions to manage iri
Browse files Browse the repository at this point in the history
  • Loading branch information
botisSmile committed Oct 21, 2024
1 parent 9a7a872 commit 19000f3
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ISearchLimitations,
ITreeItem,
flatTree,
getIri,
} from '@elastic-suite/gally-admin-shared'

import TextFieldTagsMultiple from './TextFieldTagsMultiple'
Expand Down Expand Up @@ -109,7 +110,7 @@ function RequestTypeItem(props: IRequestTypeItem): JSX.Element {
if (idItem === 'categoryLimitations') {
const newData = (val as ITreeItem[]).map((item) => {
return {
category: `/categories/${item.id}`,
category: getIri('categories', item.id),
}
})
return onChange({ ...value, [idItem]: newData })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
RuleCombinationOperator,
RuleType,
RuleValueType,
getIri,
getListApiParameters,
getOptionsFromEnum,
getOptionsFromOptionResource,
Expand Down Expand Up @@ -149,7 +150,7 @@ function RuleOptionsProvider(props: IProps): JSX.Element {
async (fetchApi: IFetchApi) => {
const optionLabelFilters = {
'order[position]': 'asc',
sourceField: `/source_fields/${field.id}`,
sourceField: getIri('source_fields', field.id),
}

let currentPage = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
IRuleEngineOperators,
ISortingOption,
LoadStatus,
getIdFromIri,
getIri,
getProductPosition,
} from '@elastic-suite/gally-admin-shared'

Expand Down Expand Up @@ -164,7 +166,7 @@ function ProductsContainer(props: IProps): JSX.Element {
0
)
const newTopProducts = bottomSelectedRows.map((row) => ({
productId: row.split('/')[2],
productId: getIdFromIri(row),
position: ++maxPosition,
}))
setProductPositions({
Expand All @@ -183,7 +185,7 @@ function ProductsContainer(props: IProps): JSX.Element {
result: JSON.stringify(
topProducts.filter(
({ productId }) =>
!topSelectedRows.includes(`/products/${productId}`)
!topSelectedRows.includes(getIri('products', productId))
)
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ISourceField,
ISourceFieldLabel,
RuleAttributeType,
getIri,
parseRule,
serializeRule,
} from '@elastic-suite/gally-admin-shared'
Expand Down Expand Up @@ -101,7 +102,10 @@ function RulesManager(props: IProps): JSX.Element {
const sourceFieldLabelFilters = useMemo(() => {
const filters: { localizedCatalog?: string } = {}
if (localizedCatalogId !== -1) {
filters.localizedCatalog = `/localized_catalogs/${localizedCatalogId}`
filters.localizedCatalog = getIri(
'localized_catalogs',
localizedCatalogId
)
}
return filters
}, [localizedCatalogId])
Expand All @@ -124,7 +128,7 @@ function RulesManager(props: IProps): JSX.Element {
)

const fields = sourceFields.data['hydra:member'].map((field) => {
const label = sourceFieldLabelsMap.get(`/source_fields/${field.id}`)
const label = sourceFieldLabelsMap.get(getIri('source_fields', field.id))
return {
id: field.id,
code: field.code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
LoadStatus,
ProductRequestType,
cleanBeforeSaveCatConf,
getIdFromIri,
getSearchPreviewProductsQuery,
productTableheader,
serializeCatConf,
Expand Down Expand Up @@ -106,7 +107,7 @@ function TopTable(props: IProps): JSX.Element {
function handleReorder(rows: ITableRow[]): void {
let position = 0
const positions = rows.map((row) => ({
productId: String(row.id).split('/')[2],
productId: getIdFromIri(String(row.id)),
position: ++position,
}))
setProductPositions({
Expand All @@ -126,7 +127,7 @@ function TopTable(props: IProps): JSX.Element {
const tableRows =
(products.data?.products?.collection.sort(
(a, b) =>
topProductsMap[a.id.split('/')[2]] - topProductsMap[b.id.split('/')[2]]
topProductsMap[getIdFromIri(a.id)] - topProductsMap[getIdFromIri(b.id)]
) as unknown as ITableRow[]) ?? []
const withSelection = selectedRows?.length !== undefined
const massiveSelectionState =
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/utils/RuleOptionsTestProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { ReactNode } from 'react'
import {
IRuleEngineOperators,
RuleAttributeType,
getIri,
} from '@elastic-suite/gally-admin-shared'

import ruleEngineOperator from '../../public/mocks/rule_engine_operators.json'
Expand All @@ -22,7 +23,7 @@ const sourceFieldLabelsMap = new Map(
)

const fields = sourceFields['hydra:member'].map((field) => {
const label = sourceFieldLabelsMap.get(`/source_fields/${field.id}`)
const label = sourceFieldLabelsMap.get(getIri('source_fields', field.id))
return {
id: field.id,
code: field.code,
Expand Down
9 changes: 9 additions & 0 deletions packages/shared/src/services/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getFieldLabelTranslationArgs,
getHeadTitle,
getIdFromIri,
getIri,
getNameFromDefault,
humanize,
roundNumber,
Expand Down Expand Up @@ -80,6 +81,14 @@ describe('Format service', () => {
})
})

describe('getIri', () => {
it('Should get iri', () => {
expect(getIri('localized_catalog', 1)).toEqual('/localized_catalog/1')
expect(getIri('localized_catalog', '1')).toEqual('/localized_catalog/1')
expect(getIri('/localized_catalog/', '1')).toEqual('/localized_catalog/1')
})
})

describe('roundNumber', () => {
it('Should round the number to the decimal given', () => {
expect(typeof roundNumber(1, 4)).toEqual('number')
Expand Down
6 changes: 6 additions & 0 deletions packages/shared/src/services/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ export function getIdFromIri(iri: string): string {
return iri.split('/').pop()
}

export function getIri(api: string, id: string | number): string {
return `/${
process.env.NEXT_PUBLIC_API_ROUTE_PREFIX ?? ''
}/${api}/${id}`.replace(/\/+/g, '/')
}

export function roundNumber(
value: number,
decimal: number,
Expand Down

0 comments on commit 19000f3

Please sign in to comment.