From 1980017210b545c83f8ff5a20c51fa2ae7c775bc Mon Sep 17 00:00:00 2001 From: Vinicius Souza Date: Tue, 5 Nov 2024 18:52:43 +0000 Subject: [PATCH] feat: fetch exercise history from backend --- src/screens/History.tsx | 54 +++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/src/screens/History.tsx b/src/screens/History.tsx index d6ef1d9..e18085e 100644 --- a/src/screens/History.tsx +++ b/src/screens/History.tsx @@ -1,23 +1,47 @@ -import { useState } from 'react'; +import { useCallback, useState } from 'react'; import { SectionList } from 'react-native'; -import { Heading, Text, VStack } from '@gluestack-ui/themed'; +import { useFocusEffect } from '@react-navigation/native'; +import { Heading, Text, useToast, VStack } from '@gluestack-ui/themed'; + +import { api } from '@services/api'; +import { AppError } from '@utils/AppError'; +import { ExerciseDTO } from '@dtos/ExerciseDTO'; import { HistoryCard } from '@components/HistoryCard'; import { ScreenHeader } from '@components/ScreenHeader'; - -const DATA = [ - { - title: '22.08.2024', - data: ['Puxada frontal', 'Puxada lateral'], - }, - { - title: '23.08.2024', - data: ['Puxada frontal'], - }, -]; +import { ToastMessage } from '@components/ToastMessage'; export function History() { - const [exercises, setExercises] = useState(DATA); + const [isLoading, setIsLoading] = useState(true); + const [exercises, setExercises] = useState([]); + + const toast = useToast(); + + async function fetchHistory() { + try { + setIsLoading(true); + + const response = await api.get('/history'); + console.debug(response.data); + } catch (error) { + const isAppError = error instanceof AppError; + const title = isAppError ? error.message : 'Não foi possível carregar o histórico.'; + toast.show({ + placement: 'top', + render: ({ id }) => ( + toast.close(id)} /> + ), + }); + } finally { + setIsLoading(false); + } + } + + useFocusEffect( + useCallback(() => { + fetchHistory(); + }, []), + ); return ( @@ -26,7 +50,7 @@ export function History() { item} - renderItem={({ item }) => } + renderItem={({ item }) => } renderSectionHeader={({ section }) => ( {section.title}