Skip to content

Commit

Permalink
adjust login flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Janderson Souza Matias authored and Janderson Souza Matias committed Mar 11, 2024
1 parent 5a6a332 commit 38529e4
Show file tree
Hide file tree
Showing 19 changed files with 277 additions and 249 deletions.
3 changes: 1 addition & 2 deletions src/database/migrations/00.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ const createSchoolTable = (db: SQLiteDatabase) => {
_status TEXT,
name TEXT,
key TEXT null,
region TEXT null,
schoolKey TEXT null,
emis_number TEXT null,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Expand All @@ -67,7 +67,6 @@ const createCoacherTable = (db: SQLiteDatabase) => {
phone TEXT null,
surname TEXT null,
birthdate TEXT null,
birthday TIMESTAMP null,
image_id TEXT REFERENCES image(id),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Expand Down
44 changes: 41 additions & 3 deletions src/providers/coach.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import {School} from '../types/school';
import {Coach} from '../types/coach';
import {useNavigate} from 'react-router-native';
import PathRoutes from '../routers/paths';
import {CoachService} from '../services/coach.service';
import {CoachSchool} from '../types/coach_school';
import {SessionService} from '../services/session.service';

type CoachContextType = {
currentCoach: Coach | null;
currentSchool: School | null;
logout: () => Promise<void>;
loginOTP: (coach: Coach) => Promise<void>;
selectCoach: (coach: Coach | null) => Promise<void>;
selectSchool: (school: School | null) => Promise<void>;
};
Expand All @@ -30,12 +34,45 @@ const CoachProvider: React.FC<{children: ReactNode}> = ({children}) => {
await StorageService.setCurrentCoach(coach);
};

const loginOTP = async (coach: Coach) => {
setCurrentCoach(coach);

await CoachService.insertOrUpdateCoach(coach);

if (coach.coachSchools) {
await Promise.all(
coach.coachSchools.map(async ({school}: CoachSchool) => {
if (school) {
await SchoolService.insertASchoolFromServer(school);
}
}),
);

await Promise.all(
coach.coachSchools.map(coachSchool =>
CoachService.insertOrUpdateCoachSchool(coachSchool),
),
);
}

if (coach.sessions) {
await SessionService.insertSessionsFromServer(coach.sessions);
}

await StorageService.setCurrentCoach(coach);
};

const selectSchool = async (school: School | null) => {
setCurrentSchool(school);
await StorageService.setCurrentSchool(school);
if (school) {
await SchoolService.insertSchool(school);
await SchoolService.insertASchoolFromServer(school);

if (currentCoach) {
await CoachService.assignCoachToSchool(currentCoach, school);
}
}

setCurrentSchool(school);
await StorageService.setCurrentSchool(school);
};

const logout = async () => {
Expand All @@ -59,6 +96,7 @@ const CoachProvider: React.FC<{children: ReactNode}> = ({children}) => {
<CoachContext.Provider
value={{
logout,
loginOTP,
selectCoach,
selectSchool,
currentCoach,
Expand Down
3 changes: 0 additions & 3 deletions src/routers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import LoginScreen from '../screens/Login/Main';
import OTPScreen from '../screens/Login/OTP';
import SignupScreen from '../screens/SignUp/Main';
import SignupSuccessScreen from '../screens/SignUp/Success';
import FastLoginScreen from '../screens/Login/FastLogin';

const RouterProvider: React.FC = () => {
return (
Expand All @@ -63,8 +62,6 @@ const RouterProvider: React.FC = () => {

<Route path={PathRoutes.login.otp} Component={OTPScreen} />

<Route path={PathRoutes.login.fastLogin} Component={FastLoginScreen} />

<Route path={PathRoutes.signup.main} Component={SignupScreen} />

<Route
Expand Down
2 changes: 1 addition & 1 deletion src/screens/ClassObservation/Confirmation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const ClassObservationConfirmation: React.FC = () => {
question_id => ({question_id, value: answers[question_id]}),
);

const sessionId = await SessionService.create(
const sessionId = await SessionService.createLocalSession(
{
students_count: session.students_count,
subject: session.subject,
Expand Down
3 changes: 1 addition & 2 deletions src/screens/CoachForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import DatePicker from 'react-native-date-picker';
import {TouchableOpacity} from 'react-native';
import moment from 'moment';
import i18n from '../../i18n';
import {Coach} from '../../types/coach';

export type FormValuesType = {
name?: string;
Expand Down Expand Up @@ -60,7 +59,7 @@ const CoachFormScreen: React.FC = () => {
}

if (currentSchool) {
const coach = await CoachService.create(currentSchool, {
const coach = await CoachService.createNewLocalCoach({
...values,
image_id,
});
Expand Down
2 changes: 1 addition & 1 deletion src/screens/FeedbackSession/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const FeedbackSessionForm: React.FC = () => {
const finishCoachSession = async () => {
setLoading(true);
if (actions) {
const feedbackId = await SessionService.createFeedback(
const feedbackId = await SessionService.createLocalFeedback(
{
answer_id: answerId,
value: actions,
Expand Down
32 changes: 29 additions & 3 deletions src/screens/Home/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import React from 'react';
import React, {useEffect, useState} from 'react';
import {TeacherItemType} from '../../types/teacher';
import {useNavigate} from 'react-router-native';
import HorizontalMenu from './HorizontalMenu';
import PathRoutes from '../../routers/paths';
import TeachersList from './TeachersList';
import {VStack} from 'native-base';
import {Spinner, VStack} from 'native-base';
import Page from '../../components/Page';
import HomeHeader from './HomeHeader';
import {isTablet} from 'react-native-device-info';
import BottomNavigator from './BottomNavigator';
import {StorageService} from '../../services/storage.service';
import SyncService from '../../services/sync.service';

const HomeScreen: React.FC = () => {
const navigate = useNavigate();
const [isLoading, setIsLoading] = useState(false);

useEffect(() => {
const asyncFunction = async () => {
const lastSync = await StorageService.getLastSync();
const ONE_HOUR = 60 * 60 * 1000;

if (
!lastSync ||
new Date().getTime() - new Date(lastSync).getTime() > ONE_HOUR
) {
console.log('START SYNC');
setIsLoading(true);
await SyncService.trySyncData();
setIsLoading(false);
}
};

asyncFunction();
}, []);

const onSelectTeacher = (teacher: TeacherItemType) => {
navigate(PathRoutes.teacher.details.replace(':id', teacher.id));
Expand All @@ -25,7 +47,11 @@ const HomeScreen: React.FC = () => {
<HorizontalMenu />

<VStack flex={1} pb={4}>
<TeachersList onSelectTeacher={onSelectTeacher} />
{isLoading ? (
<Spinner size="lg" />
) : (
<TeachersList onSelectTeacher={onSelectTeacher} />
)}
</VStack>
</VStack>

Expand Down
86 changes: 0 additions & 86 deletions src/screens/Login/FastLogin/index.tsx

This file was deleted.

30 changes: 22 additions & 8 deletions src/screens/Login/OTP/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,39 @@ import Icon from '../../../components/Icon';
import {useTranslation} from 'react-i18next';
import {CoachService} from '../../../services/coach.service';
import {useCoachContext} from '../../../providers/coach.provider';
import CompetenceService from '../../../services/competence.service';
import {QuestionService} from '../../../services/question.service';
import {StorageService} from '../../../services/storage.service';

const OTPScreen: React.FC = () => {
const [OTPCode, setOTPCode] = useState<{hasError: boolean; value: string}>();
const {loginOTP} = useCoachContext();
const [isLoading, setIsLoading] = useState(false);
const navigate = useNavigate();
const {t} = useTranslation();
const theme = useTheme();
const {id} = useParams();
const {selectCoach} = useCoachContext();

const handleVerifyCode = async () => {
setIsLoading(true);
try {
if (id && OTPCode?.value) {
const {data} = await CoachService.verifyOTP(
id.toLowerCase().trim(),
OTPCode.value,
);
selectCoach(data.coach);

await loginOTP(data.coach);
await CompetenceService.sync(data.competencies);
await QuestionService.sync(data.questions);
await StorageService.setLastSync(new Date());

navigate(PathRoutes.selectSchool, {replace: true});
}
} catch (err) {
console.log(err);
setOTPCode(otp => ({value: otp?.value || '', hasError: true}));
}
setIsLoading(false);
};

const handleResendCode = async () => {
Expand All @@ -53,12 +63,12 @@ const OTPScreen: React.FC = () => {
</Text>

<Text
fontSize={'TXL'}
fontWeight={700}
color={'primary.200'}
mt={1}
mb={8}
alignSelf={'center'}>
fontSize={'TXL'}
fontWeight={700}
alignSelf={'center'}
color={'primary.200'}>
{id}
</Text>

Expand All @@ -81,7 +91,11 @@ const OTPScreen: React.FC = () => {
</HStack>
)}

<Button mt={8} onPress={handleVerifyCode} disabled={!OTPCode?.value}>
<Button
mt={8}
isLoading={isLoading}
onPress={handleVerifyCode}
disabled={!OTPCode?.value}>
<Text
color={!OTPCode?.value ? 'gray.600' : 'gray.0'}
fontWeight={500}>
Expand Down
Loading

0 comments on commit 38529e4

Please sign in to comment.