Browse Source

Merge pull request 'feat/institutions_data_from_api' (#4) from feat/institutions_data_from_api into master

Reviewed-on: https://git.quijaua.com.br/cemaden-educacao/WPD-MobileApp/pulls/4
master
rbantu 7 months ago
parent
commit
8a0361ee56
  1. 8
      src/app/api/auth.js
  2. 51
      src/app/api/fetchInstutions.js
  3. 4
      src/app/auth/authClient.js
  4. 10
      src/app/config/constants.js
  5. 37
      src/app/screens/RegisterScreen.js
  6. 37
      src/app/screens/UpdateUserInfoScreen.js

8
src/app/api/auth.js

@ -47,7 +47,7 @@ function signup({
async function userPersonalData() { async function userPersonalData() {
const authToken = await authStorage.getToken(); const authToken = await authStorage.getToken();
const localClient = create({ 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}`); localClient.setHeader("Authorization", `Bearer ${authToken}`);
@ -61,7 +61,7 @@ async function userActivation(code) {
// console.log("USER NAME: " + username); // console.log("USER NAME: " + username);
const localClient = create({ 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}`); localClient.setHeader("Authorization", `Bearer ${authToken}`);
@ -97,7 +97,7 @@ function loginByUsernamAnswers(username, secQuestionId, secQuestionAnswer) {
function updatePassword(authToken, username, password) { function updatePassword(authToken, username, password) {
const localClient = create({ 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}`); localClient.setHeader("Authorization", `Bearer ${authToken}`);
@ -110,7 +110,7 @@ function updatePassword(authToken, username, password) {
async function UpdateUserInfo(authToken,defaultValue,formValues) { async function UpdateUserInfo(authToken,defaultValue,formValues) {
const localClient = create({ 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}`); localClient.setHeader("Authorization", `Bearer ${authToken}`);

51
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;
});
}

4
src/app/auth/authClient.js

@ -1,11 +1,11 @@
import { create } from "apisauce"; import { create } from "apisauce";
const authClient = create({ const authClient = create({
baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/auth/users",
baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/authtest/users",
}); });
const authChangePswdClient = create({ const authChangePswdClient = create({
baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/auth/forgotpasswords"
baseURL: "https://wpd.brazilsouth.cloudapp.azure.com/authtest/forgotpasswords"
}) })
export { authClient, authChangePswdClient}; export { authClient, authChangePswdClient};

10
src/app/config/constants.js

@ -1,10 +1,10 @@
export default { export default {
institutionMap: { 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: { roleMap: {
ROLE_CLIENT: "Não responsável", ROLE_CLIENT: "Não responsável",

37
src/app/screens/RegisterScreen.js

@ -20,7 +20,6 @@ import FormDatePicker from "../components/forms/FormDatePicker";
import moment from "moment"; import moment from "moment";
import SearchablePicker from "../components/SearchablePicker"; import SearchablePicker from "../components/SearchablePicker";
import { states, statesToCities } from "../assets/cities_states"; import { states, statesToCities } from "../assets/cities_states";
import institutions from "../assets/institutions";
import { useFormikContext } from "formik"; import { useFormikContext } from "formik";
import { import {
signup, signup,
@ -38,6 +37,7 @@ import CheckBox from "../components/forms/CheckBox";
import defaultStyles from "../config/styles"; import defaultStyles from "../config/styles";
import PhoneNumberFormField from "../components/forms/PhoneNumberFormField"; import PhoneNumberFormField from "../components/forms/PhoneNumberFormField";
import { unMask, mask } from "react-native-mask-text"; import { unMask, mask } from "react-native-mask-text";
import { getInstitutions } from "../api/fetchInstutions";
const phoneRegex = RegExp( const phoneRegex = RegExp(
/^\(?[\(]?([0-9]{2})?\)?[)\b]?([0-9]{4,5})[-. ]?([0-9]{4})$/ /^\(?[\(]?([0-9]{2})?\)?[)\b]?([0-9]{4,5})[-. ]?([0-9]{4})$/
@ -130,10 +130,10 @@ function GenderPicker({ name }) {
function InstitutionPicker({ name }) { function InstitutionPicker({ name }) {
const [items, setItems] = useState([ 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" }, { value: "X", label: "Nenhuma" },
]); ]);
return ( return (
@ -190,20 +190,30 @@ function CityPicker({ name }) {
function InstitutionNamePicker({ name }) { function InstitutionNamePicker({ name }) {
const { values } = useFormikContext(); const { values } = useFormikContext();
const state = values["state"]; const state = values["state"];
const city = values["city"];
const instType = values["institution"]; const instType = values["institution"];
const [items, setItems] = useState([]); const [items, setItems] = useState([]);
const [institutions, setInstitutions] = useState([]);
useEffect(() => {
getInstitutions().then((data) => {
setInstitutions(data);
})
.catch((error) => {
console.error(error);
});
}, []);
useEffect(() => { useEffect(() => {
try { 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([]); insts ? setItems(insts) : setItems([]);
} }
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
}, [state, instType]);
}, [state,city, instType, institutions]);
return ( return (
<SearchablePicker <SearchablePicker
@ -213,11 +223,11 @@ function InstitutionNamePicker({ name }) {
formPlaceholder={"Selecione o nome da instituição"} formPlaceholder={"Selecione o nome da instituição"}
doubleItemLine={true} doubleItemLine={true}
nothingToShow={ nothingToShow={
institutions?.state?.instType
institutions?.state?.city?.instType
? "Não encontramos nada com esse termo" ? "Não encontramos nada com esse termo"
: state && instType
? `Nenhuma instituição do tipo ${constants.institutionMap[instType]} no ${constants.statesMap[state]}`
: "Selecione o Estado e o tipo da instituição primeiro"
: (state && instType) || state || instType
? "Selecione uma cidade"
: state && instType && city ? `Nenhuma instituição do tipo ${constants.institutionMap[instType]} em ${city} (${constants.statesMap[state]})` : "Selecione o Estado, Cidade e o tipo da instituição primeiro"
} }
searchPlaceholder={"Busca..."} searchPlaceholder={"Busca..."}
/> />
@ -375,7 +385,6 @@ export default function RegisterScreen(props) {
}; };
const copnfirmations = (form, actions) => { const copnfirmations = (form, actions) => {
console.log(form);
var current_mask = var current_mask =
form.number.length >= 11 ? "(99) 99999-9999" : "(99) 9999-9999"; form.number.length >= 11 ? "(99) 99999-9999" : "(99) 9999-9999";

37
src/app/screens/UpdateUserInfoScreen.js

@ -4,7 +4,6 @@ import moment from "moment";
import Screen from '../components/Screen'; import Screen from '../components/Screen';
import SearchablePicker from '../components/SearchablePicker'; import SearchablePicker from '../components/SearchablePicker';
import FormDatePicker from '../components/forms/FormDatePicker'; import FormDatePicker from '../components/forms/FormDatePicker';
import institutions from "../assets/institutions";
import defaultStyles from "../config/styles"; import defaultStyles from "../config/styles";
import constants from "../config/constants"; import constants from "../config/constants";
import ConfirmationModal from '../components/ConfirmationModal'; import ConfirmationModal from '../components/ConfirmationModal';
@ -19,25 +18,37 @@ import { states, statesToCities } from "../assets/cities_states";
import PasswordFormField from "../components/forms/PasswordFormField"; import PasswordFormField from "../components/forms/PasswordFormField";
import storage from "../auth/storage"; import storage from "../auth/storage";
import { AuthContext } from "../auth/context"; import { AuthContext } from "../auth/context";
import { getInstitutions } from "../api/fetchInstutions";
//#region PICKERS //#region PICKERS
function InstitutionNamePicker({ name }) { function InstitutionNamePicker({ name }) {
const { values } = useFormikContext(); const { values } = useFormikContext();
const state = values["state"]; const state = values["state"];
const city = values["city"];
const instType = values["institution"]; const instType = values["institution"];
const [items, setItems] = useState([]); const [items, setItems] = useState([]);
const [institutions, setInstitutions] = useState([]);
useEffect(() => {
getInstitutions().then((data) => {
setInstitutions(data);
})
.catch((error) => {
console.error(error);
});
}, []);
useEffect(() => { useEffect(() => {
try { try {
if (state && instType) {
const insts = institutions[state] && institutions[state][instType];
if (state && city && instType) {
const insts = institutions[state] && institutions[state][city] && institutions[state][city][instType];
insts ? setItems(insts) : setItems([]); insts ? setItems(insts) : setItems([]);
} }
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
}, [state, instType]);
}, [state,city, instType, institutions]);
return ( return (
<SearchablePicker <SearchablePicker
@ -47,11 +58,11 @@ function InstitutionNamePicker({ name }) {
formPlaceholder={"Selecione o nome da instituição"} formPlaceholder={"Selecione o nome da instituição"}
doubleItemLine={true} doubleItemLine={true}
nothingToShow={ nothingToShow={
institutions?.state?.instType
institutions?.state?.city?.instType
? "Não encontramos nada com esse termo" ? "Não encontramos nada com esse termo"
: state && instType
? `Nenhuma instituição do tipo ${constants.institutionMap[instType]} no ${constants.statesMap[state]}`
: "Selecione o Estado e o tipo da instituição primeiro"
: (state && instType) || state || instType
? "Selecione uma cidade"
: state && instType && city ? `Nenhuma instituição do tipo ${constants.institutionMap[instType]} em ${city} (${constants.statesMap[state]})` : "Selecione o Estado, Cidade e o tipo da instituição primeiro"
} }
searchPlaceholder={"Busca..."} searchPlaceholder={"Busca..."}
/> />
@ -123,11 +134,11 @@ function CityPicker({ name }) {
function InstitutionPicker({ name }) { function InstitutionPicker({ name }) {
const [items, setItems] = useState([ 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 ( return (
<SearchablePicker <SearchablePicker

Loading…
Cancel
Save