From 7ecf286c3e4a62e82ee62d027224b7a65fb18e8f Mon Sep 17 00:00:00 2001 From: Pedro Gomes Date: Wed, 7 Aug 2024 22:32:12 -0300 Subject: [PATCH] :sparkles: feat: Typescript Errors --- src/components/ContainerCards/index.tsx | 43 +++++++++++++------ .../CompleteMentorship/index.tsx | 4 +- .../EvaluateMentoring/ModuloAsk/index.tsx | 4 +- src/screens/EvaluateMentoring/index.tsx | 29 +++++++++++-- .../EvaluateVisitManager/index.tsx | 3 ++ src/screens/EvaluateVisit/index.tsx | 3 +- src/screens/Home/index.tsx | 7 +-- src/screens/Login/index.tsx | 2 +- src/screens/MyTeam/index.tsx | 26 ++++++----- src/screens/PlainAction/index.tsx | 19 +++++++- src/screens/SalesInspector/index.tsx | 4 +- src/screens/SellerAdded/index.tsx | 2 +- src/services/CompanyService.ts | 4 ++ src/services/ModuleServices.ts | 4 +- src/services/SellerServices.ts | 2 +- src/services/SupervisorServices.ts | 2 +- src/services/UserService.ts | 6 +-- 17 files changed, 110 insertions(+), 54 deletions(-) diff --git a/src/components/ContainerCards/index.tsx b/src/components/ContainerCards/index.tsx index 0fd0df2..cebd803 100644 --- a/src/components/ContainerCards/index.tsx +++ b/src/components/ContainerCards/index.tsx @@ -7,7 +7,7 @@ import ISupervisor from '@interfaces/Supervisor'; const Container: React.FC<{ search?: string; - data: Array; + data: Array | null; loading: boolean; title: string; media?: { [key: string]: number }; @@ -21,7 +21,7 @@ const Container: React.FC<{ {loading ? ( - ) : filteredData.length > 0 ? ( + ) : (filteredData ?? []).length > 0 ? ( userType === 'Supervisor' ? ( <> @@ -34,9 +34,12 @@ const Container: React.FC<{ ) : ( - filteredData.map((item, index) => ( - - )) + filteredData?.map( + ( + item: ISeller | ISupervisor, + index: string | number | null | undefined + ) => + ) ) ) : ( @@ -46,31 +49,43 @@ const Container: React.FC<{ ); }; -const filterData = (data, search, userType) => { +const filterData = ( + data: any[] | null, + search: string | undefined, + userType: string | undefined +) => { if (userType === 'Supervisor') return data; const removeAccents = (str: string) => str.normalize('NFD').replace(/[\u0300-\u036f]/g, ''); const searchTerm = search ? removeAccents(search.toLowerCase()) : ''; - return data.filter((item) => { + return data?.filter((item) => { const itemName = removeAccents(item.name.toLowerCase()); return !search || itemName.includes(searchTerm); }); }; -const renderFilteredData = (data, stage, media) => { - const filtered = data.filter( - (item): item is ISeller => 'stage' in item && item.stage === stage +const renderFilteredData = ( + data: any[] | null | undefined, + stage: string, + media: { [x: string]: number } +) => { + const filtered = data?.filter( + (item: { stage: any }): item is ISeller => + 'stage' in item && item.stage === stage ); - if (filtered.length === 0) { + if (filtered?.length === 0) { return ; } - return filtered.map((item, index) => ( - - )); + return filtered?.map( + ( + item: ISeller | ISupervisor, + index: string | number | null | undefined + ) => + ); }; const LoadingIndicator: React.FC = () => ( diff --git a/src/screens/EvaluateMentoring/CompleteMentorship/index.tsx b/src/screens/EvaluateMentoring/CompleteMentorship/index.tsx index 54c7aca..e6c6abd 100644 --- a/src/screens/EvaluateMentoring/CompleteMentorship/index.tsx +++ b/src/screens/EvaluateMentoring/CompleteMentorship/index.tsx @@ -133,7 +133,7 @@ const CompleteMentorship: React.FC = () => { return ( <> - + @@ -188,7 +188,7 @@ const ModuleEvaluation: React.FC = ({ moduleData }) => { const fetchData = async () => { try { const fetchedModule = await ModulesServices.getModuleById( - moduleData?.idModule + moduleData?.idModule ?? '' ); setModule(fetchedModule); } catch (error) { diff --git a/src/screens/EvaluateMentoring/ModuloAsk/index.tsx b/src/screens/EvaluateMentoring/ModuloAsk/index.tsx index f14a7f0..e0b43ea 100644 --- a/src/screens/EvaluateMentoring/ModuloAsk/index.tsx +++ b/src/screens/EvaluateMentoring/ModuloAsk/index.tsx @@ -95,12 +95,12 @@ const ModuloAsk: React.FC = ({ route }) => { navigation.navigate('CompleteMentoring', { Seller: seller, ModulesEvaluate: editedModules, - }); + } as never); }; return ( <> - + diff --git a/src/screens/EvaluateMentoring/index.tsx b/src/screens/EvaluateMentoring/index.tsx index 7dd2a30..b687060 100644 --- a/src/screens/EvaluateMentoring/index.tsx +++ b/src/screens/EvaluateMentoring/index.tsx @@ -63,7 +63,7 @@ const EvaluateMentoring = () => { return ( <> - + @@ -80,14 +80,27 @@ const EvaluateMentoring = () => { ); }; -const SellerSelection = ({ sellers, onSelectSeller }) => ( +interface SellerSelectionProps { + sellers: ISeller[]; + onSelectSeller: (seller: ISeller) => void; +} + +const SellerSelection: React.FC = ({ + sellers, + onSelectSeller, +}) => ( Nome do Vendedor ); -const ModuleList = ({ loading, modules }) => ( +interface ModuleListProps { + loading: boolean; + modules: IModule[]; +} + +const ModuleList: React.FC = ({ loading, modules }) => ( Veja quais módulos estão disponíveis @@ -106,7 +119,15 @@ const ModuleList = ({ loading, modules }) => ( ); -const EvaluationButton = ({ onPress, disabled }) => ( +interface EvaluationButtonProps { + onPress: () => void; + disabled: boolean; +} + +const EvaluationButton: React.FC = ({ + onPress, + disabled, +}) => ( Avaliar diff --git a/src/screens/EvaluateVisit/EvaluateVisitManager/index.tsx b/src/screens/EvaluateVisit/EvaluateVisitManager/index.tsx index 1ebb664..63f493b 100644 --- a/src/screens/EvaluateVisit/EvaluateVisitManager/index.tsx +++ b/src/screens/EvaluateVisit/EvaluateVisitManager/index.tsx @@ -8,6 +8,7 @@ import VisitService from '@services/VisitService'; import ICategories from '@interfaces/Visit/Categories'; import ITemplateVisit from '@interfaces/Visit/TemplateVisit'; import Select from '@components/Select'; +import { StatusBar } from 'react-native'; interface VisitGrade { questionId: string; @@ -186,6 +187,8 @@ const EvaluateVisitManager = () => { return ( + + diff --git a/src/screens/EvaluateVisit/index.tsx b/src/screens/EvaluateVisit/index.tsx index b0d33cb..8b108fb 100644 --- a/src/screens/EvaluateVisit/index.tsx +++ b/src/screens/EvaluateVisit/index.tsx @@ -1,6 +1,6 @@ import React, { useEffect, useState } from 'react'; import * as S from './styles'; -import { ActivityIndicator, Dimensions } from 'react-native'; +import { ActivityIndicator, Dimensions, StatusBar } from 'react-native'; import Breadcrumb from '@components/Breadcrumb'; import Dropdown from '@components/Dropdown'; import HeaderPages from '@components/HeaderPages'; @@ -261,6 +261,7 @@ const EvaluateVisit = () => { return ( <> + { const { user } = useAuth(); const { data } = useDataContext(); const [loading, setLoading] = useState(true); - const [sellers, setSellers] = useState([]); - const [supervisors, setSupervisors] = useState([]); + const [sellers, setSellers] = useState([]); + const [supervisors, setSupervisors] = useState([]); const [media, setMedia] = useState({}); const fetchMediaData = async (sellersData: Seller[]) => { - const mediaData = {}; + const mediaData: { [key: string]: number } = {}; for (const seller of sellersData) { const moduleGrades = await ModulesServices.getModuleGradesByIdSeller( seller.id diff --git a/src/screens/Login/index.tsx b/src/screens/Login/index.tsx index 55682c3..cdd66a4 100644 --- a/src/screens/Login/index.tsx +++ b/src/screens/Login/index.tsx @@ -6,7 +6,7 @@ import React from 'react'; const Login = () => { return ( - + Faça seu login diff --git a/src/screens/MyTeam/index.tsx b/src/screens/MyTeam/index.tsx index fdc9461..10c65c1 100644 --- a/src/screens/MyTeam/index.tsx +++ b/src/screens/MyTeam/index.tsx @@ -3,7 +3,6 @@ import { StatusBar } from 'expo-status-bar'; import DivGradient from '@components/DivGradient'; import * as S from './styles'; import { useNavigation } from '@react-navigation/native'; -import Seller from '@interfaces/Seller'; import Container from '@components/ContainerCards'; import useAuth from '@hooks/useAuth'; import SellerServices from '@services/SellerServices'; @@ -18,10 +17,10 @@ const MyTeam = () => { const { user } = useAuth(); const { data } = useDataContext(); const [loading, setLoading] = useState(true); - const [sellers, setSellers] = useState([]); + const [sellers, setSellers] = useState(null); const [supervisors, setSupervisors] = useState([]); const [search, setSearch] = useState(''); - const [media, setMedia] = useState({}); + const [media, setMedia] = useState<{ [key: string]: number }>({}); const navigation = useNavigation(); const handlePressAddedSeller = () => { @@ -33,18 +32,18 @@ const MyTeam = () => { setLoading(true); try { if (user.job === 'Supervisor') { - const [sellersData] = await Promise.all([ - SellerServices.getAllSellerFromSupervisor(user.id), - ]); - const mediaData = await fetchMediaData(sellersData); - setSellers(sellersData); + const sellersData = await SellerServices.getAllSellerFromSupervisor( + user.id + ); + const mediaData = await fetchMediaData(sellersData as ISeller[]); + setSellers(sellersData || []); setMedia(mediaData); } else if (user.job === 'Gerente') { const [sellersData, supervisorsData] = await Promise.all([ SellerServices.getAllSellerFromManager(user.id), SupervisorServices.getAllSupervisorsFromManager(user.id), ]); - const mediaData = await fetchMediaData(sellersData); + const mediaData = await fetchMediaData(sellersData as ISeller[]); setSellers(sellersData); setSupervisors(supervisorsData); setMedia(mediaData); @@ -58,8 +57,8 @@ const MyTeam = () => { fetchData(); }, [user.id, user.job, data]); - const fetchMediaData = async (sellersData: Seller[]) => { - const mediaData = {}; + const fetchMediaData = async (sellersData: ISeller[]) => { + const mediaData: { [key: string]: number } = {}; for (const seller of sellersData) { const moduleGrades = await ModulesServices.getModuleGradesByIdSeller( seller.id @@ -82,8 +81,8 @@ const MyTeam = () => { setSearch(text)} /> @@ -100,7 +99,6 @@ const MyTeam = () => { userType={user.job} /> )} - { ); }; -const PlanList = ({ +interface PlanListProps { + title: string; + plains: IPlains[]; + handleToggleVisibility: (idPlain: string) => void; + handleMarkDone: (idPlain: string) => void; +} + +const PlanList: React.FC = ({ title, plains, handleToggleVisibility, @@ -156,7 +163,15 @@ const PlanList = ({ ); }; -const CompletedPlanList = ({ +interface CompletedPlanListProps { + title: string; + completedPlains: IPlains[]; + handleToggleVisibility: (idPlain: string) => void; + handleMarkDone: (idPlain: string) => void; + complete: boolean; +} + +const CompletedPlanList: React.FC = ({ title, completedPlains, handleToggleVisibility, diff --git a/src/screens/SalesInspector/index.tsx b/src/screens/SalesInspector/index.tsx index 90042ef..9b00a02 100644 --- a/src/screens/SalesInspector/index.tsx +++ b/src/screens/SalesInspector/index.tsx @@ -55,9 +55,9 @@ const SalesInspector = ({ route }) => { const handleDelete = async () => { try { setLoading(true); - if (cargo === 'Supervisor') { + if (cargo === 'Supervisor' && supervisors) { await SupervisorServices.delete(supervisors.id); - } else if (cargo === 'Vendedor') { + } else if (cargo === 'Vendedor' && seller) { await SellerServices.delete(seller.id); } setData({ diff --git a/src/screens/SellerAdded/index.tsx b/src/screens/SellerAdded/index.tsx index b68bce2..7daa65e 100644 --- a/src/screens/SellerAdded/index.tsx +++ b/src/screens/SellerAdded/index.tsx @@ -81,7 +81,7 @@ const SellerAdded: React.FC = () => { const handleCreate = async () => { try { setLoading(true); - const supervisorId = selectedSupervisor?.id || null; + const supervisorId = selectedSupervisor?.id || undefined; const companyId = user.companyId; const seller: ISeller = await SellerService.createSeller({ diff --git a/src/services/CompanyService.ts b/src/services/CompanyService.ts index 5d8f07f..4a44dfb 100644 --- a/src/services/CompanyService.ts +++ b/src/services/CompanyService.ts @@ -18,6 +18,10 @@ export default class CompanyServices { (company) => company.id === idCompany ); + if (company === undefined) { + throw new Error('Company not found'); + } + return company; } } diff --git a/src/services/ModuleServices.ts b/src/services/ModuleServices.ts index d2de0c8..d8a9b3e 100644 --- a/src/services/ModuleServices.ts +++ b/src/services/ModuleServices.ts @@ -15,7 +15,9 @@ export default class ModulesServices { static async getModuleById(idModule: string): Promise { const moduleResponse: AxiosResponse = await api.get('/module/getAll'); - const module = moduleResponse.data.find((module) => module.id === idModule); + const module = moduleResponse.data.find( + (module) => module.id === idModule + ) as IModule; return module; } diff --git a/src/services/SellerServices.ts b/src/services/SellerServices.ts index 3cd080a..a8f8560 100644 --- a/src/services/SellerServices.ts +++ b/src/services/SellerServices.ts @@ -28,7 +28,7 @@ export default class SellerService { static async getSupervisorByIdCompany( companyId: string, sellerId: string - ): Promise { + ): Promise { const responseCompany = await this.getAllSellerFromCompany(companyId); const seller = responseCompany.find((seller) => seller.id === sellerId); return seller; diff --git a/src/services/SupervisorServices.ts b/src/services/SupervisorServices.ts index b32dee3..251ed7e 100644 --- a/src/services/SupervisorServices.ts +++ b/src/services/SupervisorServices.ts @@ -36,7 +36,7 @@ export default class SupervisorServices { static async getSupervisorByIdCompany( companyId: string, supervisorId: string - ): Promise { + ): Promise { const responseCompany = await this.getAllSupervisorFromCompany(companyId); const supervisor = responseCompany.find( (supervisor) => supervisor.id === supervisorId diff --git a/src/services/UserService.ts b/src/services/UserService.ts index 5f9020d..5a0e2fd 100644 --- a/src/services/UserService.ts +++ b/src/services/UserService.ts @@ -17,27 +17,23 @@ interface ILoginRequest { } export default class UserService { - static async login(data: ILoginRequest): Promise { + static async login(data: ILoginRequest): Promise { try { - // Tentar fazer login como supervisor const supervisorResponse: AxiosResponse = await api.post( '/supervisor/login', data ); - // Se o login como supervisor for bem-sucedido, armazenar tokens e retornar resposta await AsyncStorage.setItem('@app:token', supervisorResponse.data.token); await AsyncStorage.setItem('@app:useId', supervisorResponse.data.user.id); return supervisorResponse.data; } catch (error) { try { - // Tentar fazer login como gerente const managerResponse: AxiosResponse = await api.post( '/manager/login', data ); - // Se o login como gerente for bem-sucedido, armazenar tokens e retornar resposta await AsyncStorage.setItem('@app:token', managerResponse.data.token); await AsyncStorage.setItem('@app:useId', managerResponse.data.user.id); return managerResponse.data;