@@ -12,25 +12,25 @@ import {
1212 Switch ,
1313 TextField ,
1414} from "@mui/material" ;
15+ import { Exercise } from "components/Exercises/models/exercise" ;
1516import { SERVER_URL } from "config" ;
1617import debounce from "lodash/debounce" ;
1718import * as React from "react" ;
1819import { useState } from "react" ;
1920import { useTranslation } from "react-i18next" ;
20- import { getExercise , searchExerciseTranslations } from "services" ;
21- import { ExerciseSearchResponse } from "services/responseType" ;
21+ import { searchExerciseTranslations } from "services" ;
2222import { LANGUAGE_SHORT_ENGLISH } from "utils/consts" ;
2323
2424type NameAutocompleterProps = {
25- callback : ( exerciseResponse : ExerciseSearchResponse | null ) => void ;
25+ callback : ( exercise : Exercise | null ) => void ;
2626 loadExercise ?: boolean ;
2727} ;
2828
2929export function NameAutocompleter ( { callback, loadExercise } : NameAutocompleterProps ) {
30- const [ value , setValue ] = React . useState < ExerciseSearchResponse | null > ( null ) ;
30+ const [ value , setValue ] = React . useState < Exercise | null > ( null ) ;
3131 const [ inputValue , setInputValue ] = React . useState ( "" ) ;
3232 const [ searchEnglish , setSearchEnglish ] = useState < boolean > ( true ) ;
33- const [ options , setOptions ] = React . useState < readonly ExerciseSearchResponse [ ] > ( [ ] ) ;
33+ const [ options , setOptions ] = React . useState < readonly Exercise [ ] > ( [ ] ) ;
3434 const [ t , i18n ] = useTranslation ( ) ;
3535
3636 loadExercise = loadExercise === undefined ? false : loadExercise ;
@@ -61,7 +61,7 @@ export function NameAutocompleter({ callback, loadExercise }: NameAutocompleterP
6161 < >
6262 < Autocomplete
6363 id = "exercise-name-autocomplete"
64- getOptionLabel = { ( option ) => option . value }
64+ getOptionLabel = { ( option ) => option . getTranslation ( ) . name }
6565 data-testid = "autocomplete"
6666 filterOptions = { ( x ) => x }
6767 options = { options }
@@ -70,13 +70,10 @@ export function NameAutocompleter({ callback, loadExercise }: NameAutocompleterP
7070 filterSelectedOptions
7171 value = { value }
7272 noOptionsText = { t ( "noResults" ) }
73- isOptionEqualToValue = { ( option , value ) => option . value === value . value }
74- onChange = { async ( event : React . SyntheticEvent , newValue : ExerciseSearchResponse | null ) => {
73+ isOptionEqualToValue = { ( option , value ) => option . id === value . id }
74+ onChange = { async ( event : React . SyntheticEvent , newValue : Exercise | null ) => {
7575 setOptions ( newValue ? [ newValue , ...options ] : options ) ;
7676 setValue ( newValue ) ;
77- if ( loadExercise && newValue !== null ) {
78- newValue . exercise = await getExercise ( newValue . data . base_id ) ;
79- }
8077 callback ( newValue ) ;
8178 } }
8279 onInputChange = { ( event , newInputValue ) => {
@@ -102,34 +99,39 @@ export function NameAutocompleter({ callback, loadExercise }: NameAutocompleterP
10299 } }
103100 />
104101 ) }
105- renderOption = { ( props , option , state ) => (
106- < li
107- { ...props }
108- key = { `exercise-${ state . index } -${ option . data . id } ` }
109- data-testid = { `autocompleter-result-${ option . data . base_id } ` }
110- >
111- < ListItem disablePadding component = "div" >
112- < ListItemIcon >
113- { option . data . image ? (
114- < Avatar alt = "" src = { `${ SERVER_URL } ${ option . data . image } ` } variant = "rounded" />
115- ) : (
116- < PhotoIcon fontSize = "large" />
117- ) }
118- </ ListItemIcon >
119- < ListItemText
120- primary = { option . value }
121- slotProps = { {
122- primary : {
123- whiteSpace : "nowrap" ,
124- overflow : "hidden" ,
125- textOverflow : "ellipsis" ,
126- } ,
127- } }
128- secondary = { option . data . category }
129- />
130- </ ListItem >
131- </ li >
132- ) }
102+ renderOption = { ( props , option , state ) => {
103+ const translation = option . getTranslation ( ) ;
104+ const mainImage = option . mainImage ;
105+
106+ return (
107+ < li
108+ { ...props }
109+ key = { `exercise-${ state . index } -${ option . id } ` }
110+ data-testid = { `autocompleter-result-${ option . id } ` }
111+ >
112+ < ListItem disablePadding component = "div" >
113+ < ListItemIcon >
114+ { mainImage ? (
115+ < Avatar alt = "" src = { `${ SERVER_URL } ${ mainImage . url } ` } variant = "rounded" />
116+ ) : (
117+ < PhotoIcon fontSize = "large" />
118+ ) }
119+ </ ListItemIcon >
120+ < ListItemText
121+ primary = { translation . name }
122+ slotProps = { {
123+ primary : {
124+ whiteSpace : "nowrap" ,
125+ overflow : "hidden" ,
126+ textOverflow : "ellipsis" ,
127+ } ,
128+ } }
129+ secondary = { option . category . name }
130+ />
131+ </ ListItem >
132+ </ li >
133+ ) ;
134+ } }
133135 />
134136 { i18n . language !== LANGUAGE_SHORT_ENGLISH && (
135137 < FormGroup >
0 commit comments