Skip to content

Commit

Permalink
Merge pull request #5 from fga-eps-mds/BUG---Idoso
Browse files Browse the repository at this point in the history
Bug idoso
  • Loading branch information
viniciused26 authored Jan 10, 2025
2 parents 86c59c8 + bb02a27 commit c3f6c3a
Show file tree
Hide file tree
Showing 2 changed files with 335 additions and 297 deletions.
69 changes: 55 additions & 14 deletions src/app/private/pages/cadastrarIdoso.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import Metrica from "../../model/Metrica";
import { getTipoSanguineoOptions } from "../../shared/helpers/useNotification";
import styles from "../../components/style/styles";
interface IErrors {
nome?: string;
dataNascimento?: string;
tipoSanguineo?: string;
telefoneResponsavel?: string;
descricao?: string;
nome?: string;
dataNascimento?: string;
tipoSanguineo?: string;
telefoneResponsavel?: string;
descricao?: string;
}

export default function CadastrarIdoso() {
Expand All @@ -41,23 +41,25 @@ export default function CadastrarIdoso() {
const [telefoneResponsavel, setTelefoneResponsavel] = useState<string>("");
const [dataNascimento, setDataNascimento] = useState<string>("");
const [descricao, setDescricao] = useState<string>("");
const [token, setToken] = useState<string>("");
const [erros, setErros] = useState<IErrors>({});
const [showErrors, setShowErrors] = useState<boolean>(false);
const [showLoading, setShowLoading] = useState<boolean>(false);
const [idUsuario, setIdUsuario] = useState<number | null>(null);
const [maskedTelefoneResponsavel, setMaskedTelefoneResponsavel] = useState<string>("");

const router = useRouter();

const API_URL = process.env.EXPO_PUBLIC_API_URL;
const API_PORT = process.env.EXPO_PUBLIC_API_USUARIO_PORT;
const BASE_URL = `${API_URL}:${API_PORT}/api/saude/idoso`;

useEffect(() => {
const getIdUsuario = async () => {
try {
const response = await AsyncStorage.getItem("usuario");
if (response) {
const usuario = JSON.parse(response) as IUser;
setIdUsuario(usuario.id);
console.log("Usuário logado:", usuario);
// console.log("Usuário logado:", usuario);
} else {
console.log("Usuário não encontrado no AsyncStorage.");
}
Expand All @@ -66,14 +68,11 @@ export default function CadastrarIdoso() {
}
};


getIdUsuario();
}, []);


useEffect(() => handleErrors(), [nome, telefoneResponsavel, dataNascimento]);


const getDateIsoString = (value: string) => {
const dateArray = value.split("/");
return `${dateArray[2]}-${dateArray[1]}-${dateArray[0]}T12:00:00.000Z`;
Expand Down Expand Up @@ -177,10 +176,52 @@ export default function CadastrarIdoso() {
}

try {
const token = await AsyncStorage.getItem('token')

if (!token) {
console.error('Token não encontrado.');
return;
}

setShowLoading(true);
await salvarNoBancoLocal();
ToastAndroid.show("Idoso salvo no banco local com sucesso!", ToastAndroid.SHORT);
router.replace("/private/pages/listarIdosos");
// await salvarNoBancoLocal();

if (idUsuario === null) {
return
}

const body = {
nome: nome,
dataNascimento: getDateIsoString(dataNascimento),
telefoneResponsavel: telefoneResponsavel,
descricao: descricao,
tipoSanguineo: tipoSanguineo,
idUsuario: idUsuario,
foto: foto || ''
};

const response = await fetch(BASE_URL,
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(body)
}
)

const data = await response.json()

console.log('response', data)

if (data.message == "Salvo com sucesso!") {
ToastAndroid.show("Idoso salvo com sucesso!", ToastAndroid.SHORT);
router.replace("/private/pages/listarIdosos");
} else {
throw new Error("Erro ao salvar idoso")
}
} catch (err) {
const error = err as { message: string };
ToastAndroid.show(`Erro: ${error.message}`, ToastAndroid.SHORT);
Expand Down
Loading

0 comments on commit c3f6c3a

Please sign in to comment.