add new routes for filtering and the search bar works !
continuous-integration/drone/push Build is failing Details

pull/20/head
Rayhân HASSOU 1 year ago
parent a29c5b78ec
commit 1c2a392d00

@ -3,5 +3,6 @@ import Ingredient from "../../Models/Ingredient";
export default interface IIngredientService { export default interface IIngredientService {
getAllIngredient(): Promise<Ingredient[]>; getAllIngredient(): Promise<Ingredient[]>;
getIngredientById(id: Number): Promise<Ingredient | null>; getIngredientById(id: Number): Promise<Ingredient | null>;
getIngredientByLetter(id: String): Promise<any>; getIngredientByLetter(id: String): Promise<Ingredient[]>;
getfilteredIngredient(prompt: String): Promise<Ingredient[]>;
} }

@ -18,8 +18,12 @@ export default class IngredientService implements IIngredientService {
async getIngredientById(id: Number): Promise<Ingredient | null>{ async getIngredientById(id: Number): Promise<Ingredient | null>{
try {
return; const response = await axios.get(`${this.API_URL}/${id}`);
return response.data as Ingredient;
} catch (error) {
throw new Error('Erreur lors de la récupération des ingrédients : ' + error.message);
}
} }
async getIngredientByLetter(letter: String): Promise<any>{ async getIngredientByLetter(letter: String): Promise<any>{
@ -30,4 +34,14 @@ export default class IngredientService implements IIngredientService {
throw new Error('Erreur lors de la récupération des ingrédients : ' + error.message); throw new Error('Erreur lors de la récupération des ingrédients : ' + error.message);
} }
} }
async getfilteredIngredient(prompt: String): Promise<Ingredient[]> {
try {
const response = await axios.get(`${this.API_URL}/filter/${prompt}`);
return response.data as Ingredient[];
} catch (error) {
throw new Error('Erreur lors de la récupération des ingrédients : ' + error.message);
}
return;
}
} }

@ -1,24 +1,27 @@
import React from 'react'; import React from 'react';
import {StyleSheet,Pressable, Text, View, Image} from 'react-native'; import {StyleSheet,Pressable, Text, View, Image} from 'react-native';
import Separator from '../components/Separator'; import Separator from '../components/Separator';
import plus from '../assets/images/plus.png';
import moins from '../assets/images/minus.png';
interface foodElementImageProps { interface foodElementImageProps {
source : string source : string | null
title : string title : string
} }
const componentHeight = 60;
const componentWidth = 280;
export default function FoodElementText(props : any) { export default function FoodElementText(props : any) {
const isTextOverflowing = props.title.length * 10 > 260;
return ( return (
<Pressable style={styles.button}> <Pressable style={styles.button}>
<View style={styles.container}> <View style={styles.container}>
<View style={styles.view}> <View style={styles.view}>
<Text style={styles.text}>{props.title}</Text> <Text style={styles.text}>{props.title}</Text>
<Separator/> {!isTextOverflowing && <Separator />}
</View> </View>
</View> </View>
</Pressable> </Pressable>
@ -26,11 +29,13 @@ export default function FoodElementText(props : any) {
} }
const styles = StyleSheet.create({ const styles = StyleSheet.create({
button: { button: {
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', justifyContent: 'center',
width : 270, width : componentWidth,
height: 60, minHeight: componentHeight,
borderRadius: 5, borderRadius: 5,
elevation: 3, elevation: 3,
backgroundColor: '#E3DEC9', backgroundColor: '#E3DEC9',
@ -40,8 +45,9 @@ const styles = StyleSheet.create({
lineHeight: 21, lineHeight: 21,
fontWeight: 'bold', fontWeight: 'bold',
letterSpacing: 0.25, letterSpacing: 0.25,
padding : 7, padding: 7,
color: 'black', color: 'black',
width: 250,
}, },
view: { view: {
alignItems: 'flex-start', alignItems: 'flex-start',
@ -49,8 +55,8 @@ const styles = StyleSheet.create({
marginRight: 5 // Centre le contenu horizontalement marginRight: 5 // Centre le contenu horizontalement
}, },
container: { container: {
width :260, width : componentWidth - 10,
height: 50, minHeight : componentHeight - 10,
borderRadius: 5, borderRadius: 5,
elevation: 3, elevation: 3,
borderWidth: 2, borderWidth: 2,

@ -19,6 +19,33 @@ export default function IngredientSelection(props) {
const [selectedIngredients, setSelectedIngredients] = useState([]); const [selectedIngredients, setSelectedIngredients] = useState([]);
const ingredientService = new IngredientService(); const ingredientService = new IngredientService();
const [searchQuery, setSearchQuery] = useState('');
const [filteredIngredients, setFilteredIngredients] = useState([]);
const filterIngredients = async (query) => {
try {
setIsLoading(true);
if (query === '') {
// Si le query (prompt) est vide, charger tous les ingrédients
loadIngredients();
} else {
const filtered = await ingredientService.getfilteredIngredient(query);
setResponse(filtered);
}
} catch (error) {
setError(error);
} finally {
setIsLoading(false);
}
};
// Appelée à chaque changement de la recherche
const handleSearch = (query) => {
setSearchQuery(query);
filterIngredients(query);
};
const loadIngredients = async () => { const loadIngredients = async () => {
try { try {
const ingredients = await ingredientService.getAllIngredient(); const ingredients = await ingredientService.getAllIngredient();
@ -99,8 +126,8 @@ export default function IngredientSelection(props) {
<View> <View>
<Searchbar <Searchbar
placeholder="Rechercher" placeholder="Rechercher"
onChangeText={query => setSearchValue(query)} onChangeText={handleSearch}
value={searchValue} value={searchQuery}
style={{ style={{
margin: 10, margin: 10,
backgroundColor: '#F2F0E4', backgroundColor: '#F2F0E4',

Loading…
Cancel
Save