diff --git a/src/app/api/auth.js b/src/app/api/auth.js index a4efc18..56dccda 100644 --- a/src/app/api/auth.js +++ b/src/app/api/auth.js @@ -47,7 +47,7 @@ function signup({ async function userPersonalData() { const authToken = await authStorage.getToken(); const localClient = create({ - baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/auth/users", + baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/authtest/users", }); localClient.setHeader("Authorization", `Bearer ${authToken}`); @@ -61,7 +61,7 @@ async function userActivation(code) { // console.log("USER NAME: " + username); const localClient = create({ - baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/auth/users", + baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/authtest/users", }); localClient.setHeader("Authorization", `Bearer ${authToken}`); @@ -97,7 +97,7 @@ function loginByUsernamAnswers(username, secQuestionId, secQuestionAnswer) { function updatePassword(authToken, username, password) { const localClient = create({ - baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/auth/forgotpasswords", + baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/authtest/forgotpasswords", }); localClient.setHeader("Authorization", `Bearer ${authToken}`); @@ -110,7 +110,7 @@ function updatePassword(authToken, username, password) { async function UpdateUserInfo(authToken,defaultValue,formValues) { const localClient = create({ - baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/auth/users", + baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/authtest/users", }); localClient.setHeader("Authorization", `Bearer ${authToken}`); diff --git a/src/app/api/fetchInstutions.js b/src/app/api/fetchInstutions.js new file mode 100644 index 0000000..dcb6b4f --- /dev/null +++ b/src/app/api/fetchInstutions.js @@ -0,0 +1,51 @@ +import { create } from "apisauce"; + +const fetchInstitutions = create({ + baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/authtest/organizations/all", +}); + +function transformData(institutions) { + const transformedData = {}; + + institutions.forEach((institution) => { + let { uf, type, name, cidade } = institution; + + if (!uf) { + uf = "DF"; + } + + if (!cidade) { + cidade = "Brasília"; + } + + if (!transformedData[uf]) { + transformedData[uf] = {}; + } + + if (!transformedData[uf][cidade]) { + transformedData[uf][cidade] = {}; + } + + if (!transformedData[uf][cidade][type]) { + transformedData[uf][cidade][type] = []; + } + + transformedData[uf][cidade][type].push({ value: name, label: name }); + }); + + return transformedData; +} + +export function getInstitutions() { + return fetchInstitutions.get() + .then((response) => { + if (response.ok) { + return transformData(response.data); + } else { + throw new Error("Falha ao tentar buscar instituicoes no banco de dados"); + } + }) + .catch((error) => { + throw error; + }); +} diff --git a/src/app/auth/authClient.js b/src/app/auth/authClient.js index 290703a..f532073 100644 --- a/src/app/auth/authClient.js +++ b/src/app/auth/authClient.js @@ -1,11 +1,11 @@ import { create } from "apisauce"; const authClient = create({ - baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/auth/users", + baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/authtest/users", }); const authChangePswdClient = create({ - baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/auth/forgotpasswords" + baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/authtest/forgotpasswords" }) export { authClient, authChangePswdClient}; diff --git a/src/app/config/constants.js b/src/app/config/constants.js index 5477467..bfa863b 100644 --- a/src/app/config/constants.js +++ b/src/app/config/constants.js @@ -1,10 +1,10 @@ export default { institutionMap: { - E: "Escola", - D: "Defesa Civil", - N: "Não governamental", - O: "Outra", - X: "Nenhuma", + SCHOOL: "Escola", + CIVIL_DEFENSE: "Defesa civil", + NGO: "Não governamental", + OTHER: "Outra", + X: "Nenhuma" }, roleMap: { ROLE_CLIENT: "Não responsável", diff --git a/src/app/screens/RegisterScreen.js b/src/app/screens/RegisterScreen.js index 99f74f6..89deee8 100644 --- a/src/app/screens/RegisterScreen.js +++ b/src/app/screens/RegisterScreen.js @@ -20,7 +20,6 @@ import FormDatePicker from "../components/forms/FormDatePicker"; import moment from "moment"; import SearchablePicker from "../components/SearchablePicker"; import { states, statesToCities } from "../assets/cities_states"; -import institutions from "../assets/institutions"; import { useFormikContext } from "formik"; import { signup, @@ -38,6 +37,7 @@ import CheckBox from "../components/forms/CheckBox"; import defaultStyles from "../config/styles"; import PhoneNumberFormField from "../components/forms/PhoneNumberFormField"; import { unMask, mask } from "react-native-mask-text"; +import { getInstitutions } from "../api/fetchInstutions"; const phoneRegex = RegExp( /^\(?[\(]?([0-9]{2})?\)?[)\b]?([0-9]{4,5})[-. ]?([0-9]{4})$/ @@ -130,10 +130,10 @@ function GenderPicker({ name }) { function InstitutionPicker({ name }) { const [items, setItems] = useState([ - { value: "E", label: "Escola" }, - { value: "D", label: "Defesa civil" }, - { value: "N", label: "Não governamental" }, - { value: "O", label: "Outra" }, + { value: "SCHOOL", label: "Escola" }, + { value: "CIVIL_DEFENSE", label: "Defesa civil" }, + { value: "NGO", label: "Não governamental" }, + { value: "OTHER", label: "Outra" }, { value: "X", label: "Nenhuma" }, ]); return ( @@ -190,20 +190,30 @@ function CityPicker({ name }) { function InstitutionNamePicker({ name }) { const { values } = useFormikContext(); const state = values["state"]; + const city = values["city"]; const instType = values["institution"]; const [items, setItems] = useState([]); + const [institutions, setInstitutions] = useState([]); + + useEffect(() => { + getInstitutions().then((data) => { + setInstitutions(data); + }) + .catch((error) => { + console.error(error); + }); + }, []); useEffect(() => { try { - if (state && instType) { - const insts = institutions[state] && institutions[state][instType]; - console.log(insts); + if (state && city && instType) { + const insts = institutions[state] && institutions[state][city] && institutions[state][city][instType]; insts ? setItems(insts) : setItems([]); } } catch (e) { console.log(e); } - }, [state, instType]); + }, [state,city, instType, institutions]); return ( @@ -375,7 +385,6 @@ export default function RegisterScreen(props) { }; const copnfirmations = (form, actions) => { - console.log(form); var current_mask = form.number.length >= 11 ? "(99) 99999-9999" : "(99) 9999-9999"; diff --git a/src/app/screens/UpdateUserInfoScreen.js b/src/app/screens/UpdateUserInfoScreen.js index 5794f75..c5b2d32 100644 --- a/src/app/screens/UpdateUserInfoScreen.js +++ b/src/app/screens/UpdateUserInfoScreen.js @@ -4,7 +4,6 @@ import moment from "moment"; import Screen from '../components/Screen'; import SearchablePicker from '../components/SearchablePicker'; import FormDatePicker from '../components/forms/FormDatePicker'; -import institutions from "../assets/institutions"; import defaultStyles from "../config/styles"; import constants from "../config/constants"; import ConfirmationModal from '../components/ConfirmationModal'; @@ -19,45 +18,57 @@ import { states, statesToCities } from "../assets/cities_states"; import PasswordFormField from "../components/forms/PasswordFormField"; import storage from "../auth/storage"; import { AuthContext } from "../auth/context"; +import { getInstitutions } from "../api/fetchInstutions"; //#region PICKERS function InstitutionNamePicker({ name }) { const { values } = useFormikContext(); const state = values["state"]; + const city = values["city"]; const instType = values["institution"]; const [items, setItems] = useState([]); - + const [institutions, setInstitutions] = useState([]); + useEffect(() => { - try { - if (state && instType) { - const insts = institutions[state] && institutions[state][instType]; - insts ? setItems(insts) : setItems([]); - } - } catch (e) { - console.log(e); + getInstitutions().then((data) => { + setInstitutions(data); + }) + .catch((error) => { + console.error(error); + }); + }, []); + + useEffect(() => { + try { + if (state && city && instType) { + const insts = institutions[state] && institutions[state][city] && institutions[state][city][instType]; + insts ? setItems(insts) : setItems([]); } - }, [state, instType]); - + } catch (e) { + console.log(e); + } + }, [state,city, instType, institutions]); + return ( - + ); -} - + } + function RolePicker({ name }) { const [items, setItems] = useState([ { value: "ROLE_INSTITUTION", label: "Responsável" }, @@ -123,11 +134,11 @@ function CityPicker({ name }) { function InstitutionPicker({ name }) { const [items, setItems] = useState([ - { value: "E", label: "Escola" }, - { value: "D", label: "Defesa civil" }, - { value: "N", label: "Não governamental" }, - { value: "O", label: "Outra" }, - { value: "X", label: "Nenhuma" }, + { value: "SCHOOL", label: "Escola" }, + { value: "CIVIL_DEFENSE", label: "Defesa civil" }, + { value: "NGO", label: "Não governamental" }, + { value: "OTHER", label: "Outra" }, + { value: "X", label: "Nenhuma" } ]); return (