Skip to content

Commit

Permalink
fixed tree data rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
katyhonguyen committed Dec 7, 2024
1 parent 8223ce2 commit 609ddf8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
28 changes: 16 additions & 12 deletions src/screens/TreeSearch/TreeSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function TreeSearch({ navigation }: TreeSearchProps) {
setLoading(true);
const { data, error } = await supabase
.from('trees')
.select('tree_id, species, sold, species(image_link)');
.select('tree_id, species, sold, species (name, image_link)');

if (error) {
throw new Error(`Error fetching tree data: ${error.message}`);
Expand All @@ -37,24 +37,28 @@ export default function TreeSearch({ navigation }: TreeSearchProps) {
const treeCounts: Record<string, number> = data.reduce(
(acc, tree) => {
if (!tree.sold) {
acc[tree.species] = (acc[tree.species] || 0) + 1;
acc[tree.species.name] = (acc[tree.species.name] || 0) + 1;
}
return acc;
},
{} as Record<string, number>,
);

const treesData: TreeItem[] = Object.keys(treeCounts).map(species => {
const treeSample = data.find(tree => tree.species === species);
const treesData: TreeItem[] = Object.keys(treeCounts).map(
speciesName => {
const treeSample = data.find(
tree => tree.species.name === speciesName,
);

return {
tree_id: treeSample?.tree_id || 0,
species: species,
image_link: treeSample?.species?.image_link || '',
sold: false,
stockCount: treeCounts[species],
};
});
return {
tree_id: treeSample?.tree_id || 0,
species: speciesName,
image_link: treeSample?.species?.image_link || '',
sold: false,
stockCount: treeCounts[speciesName],
};
},
);

setTrees(treesData);
setError(null);
Expand Down
10 changes: 7 additions & 3 deletions src/screens/TreeSearch/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const { width, height } = Dimensions.get('window');

const responsivePadding = width * 0.05;

const screenWidth = Dimensions.get('window').width;

export const styles = StyleSheet.create({
backgroundContainer: {
flexGrow: 1,
Expand Down Expand Up @@ -45,11 +47,13 @@ export const styles = StyleSheet.create({
// Tree Cards

treeGrid: {
gap: 10,
width: '100%',
gap: 16,
paddingHorizontal: width * 0.04,
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'space-between',
width: '100%',
alignItems: 'flex-start',
},

treeCard: {
Expand All @@ -63,7 +67,7 @@ export const styles = StyleSheet.create({

treeImage: {
width: '100%',
height: 135,
height: 150,
flexShrink: 0,
borderRadius: 5,
resizeMode: 'cover',
Expand Down

0 comments on commit 609ddf8

Please sign in to comment.