Skip to content

Commit

Permalink
added translation to categories and subjects page
Browse files Browse the repository at this point in the history
  • Loading branch information
amoutens committed Nov 25, 2024
1 parent 7d79f37 commit efa2bda
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/components/popular-categories/PopularCategories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const PopularCategories: FC<PopularCategoriesProps> = ({
iconColor={item.appearance.color}
key={item._id}
link={`${authRoutes.subjects.path}?categoryId=${item._id}`}
title={item.name}
title={t(`categories.${item.name}`)}
/>
)),
[response.items, oppositeRole, t]
Expand Down
2 changes: 1 addition & 1 deletion src/pages/categories/Categories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const Categories = () => {
iconColor={item.appearance.color}
key={item._id}
link={`${authRoutes.subjects.path}?categoryId=${item._id}`}
title={item.name}
title={t(`categories.${item.name}`)}
/>
)
}),
Expand Down
41 changes: 37 additions & 4 deletions src/pages/subjects/Subjects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ import { getSuffixes } from '~/utils/get-translation-suffixes'

import {
CategoryNameInterface,
ServiceFunction,
SizeEnum,
SubjectInterface,
SubjectNameInterface
} from '~/types'
import { itemsLoadLimit } from '~/constants'
import { authRoutes } from '~/router/constants/authRoutes'
import { styles } from '~/pages/subjects/Subjects.styles'
import { AxiosHeaders, AxiosResponse } from 'axios'

const Subjects = () => {
const [match, setMatch] = useState<string>('')
Expand Down Expand Up @@ -108,7 +110,7 @@ const Subjects = () => {
iconColor={item.category.appearance.color}
key={item._id}
link={`${authRoutes.findOffers.path}?categoryId=${categoryId}&subjectId=${item._id}`}
title={item.name}
title={t(`subjects.${item.name}`)}
/>
)
}),
Expand All @@ -130,13 +132,42 @@ const Subjects = () => {
const category = response.find((option) => option._id === categoryId)
setCategoryName(category?.name ?? '')
}

const fetchTranslatedCategories: ServiceFunction<
CategoryNameInterface[]
> = async () => {
try {
const response = await categoryService.getCategoriesNames()
const translatedCategories = response.data.map((category) => ({
...category,
displayName: t(`categories.${category.name}`, {
defaultValue: category.name
})
}))
return {
...response,
data: translatedCategories
}
} catch (error) {
console.error('Error fetching categories:', error)
return {
data: [],
status: 500,
statusText: 'Error',
headers: {},
config: {
headers: new AxiosHeaders(),
method: 'GET',
url: ''
}
} as AxiosResponse<CategoryNameInterface[]>
}
}
const autoCompleteCategories = (
<AsyncAutocomplete
axiosProps={{ onResponse: onResponseCategory }}
labelField='name'
labelField='displayName'
onChange={onCategoryChange}
service={categoryService.getCategoriesNames}
service={fetchTranslatedCategories}
sx={styles.categoryInput}
textFieldProps={{
label: t('breadCrumbs.categories')
Expand All @@ -160,6 +191,8 @@ const Subjects = () => {
style={styles.titleWithDescription}
title={t('subjectsPage.subjects.title', {
category: categoryName
? t(`categories.${categoryName}`)
: categoryName
})}
/>

Expand Down

0 comments on commit efa2bda

Please sign in to comment.