From 2b1302ad3a6b90e9b3387339330b6ea6e6249a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Mur=C3=A7a?= Date: Tue, 29 Nov 2022 18:24:13 -0300 Subject: [PATCH] Structure mocks in different apis --- src/context/auth.js | 6 +- src/services/professor.js | 4 + src/services/student.js | 32 ++---- src/utils/mocks/api.js | 230 +++++++++++++++++++------------------- 4 files changed, 136 insertions(+), 136 deletions(-) diff --git a/src/context/auth.js b/src/context/auth.js index 3cf7d40..59ccd82 100644 --- a/src/context/auth.js +++ b/src/context/auth.js @@ -1,5 +1,5 @@ import { createContext, useContext, useEffect, useState } from 'react'; -import { getUser, registerUser } from '../utils/mocks/api'; +import { CommonApi } from '../utils/mocks/api'; const AuthContext = createContext(); @@ -23,7 +23,7 @@ function AuthProvider(props) { const register = data => { setState({ ...state, status: 'pending' }); - return registerUser(data).then(data => { + return CommonApi.registerUser(data).then(data => { if (data.message) { return setState({ status: 'error', user: null, error: data }); } else { @@ -35,7 +35,7 @@ function AuthProvider(props) { const login = (email, password) => { setState({ ...state, status: 'pending' }); - return getUser(email, password).then(data => { + return CommonApi.getUser(email, password).then(data => { if (data.message) { return setState({ status: 'error', user: null, error: data }); } else { diff --git a/src/services/professor.js b/src/services/professor.js index 5589b51..9820227 100644 --- a/src/services/professor.js +++ b/src/services/professor.js @@ -1,5 +1,9 @@ +import { ProfessorApi } from '../utils/mocks/api'; + export default class ProfessorService { constructor(user) { this.user = user; } + + fetchClassrooms = () => ProfessorApi.getClassrooms(this.user.id); } diff --git a/src/services/student.js b/src/services/student.js index 1fcd107..294edd3 100644 --- a/src/services/student.js +++ b/src/services/student.js @@ -1,37 +1,29 @@ -import { - getAllAssignments, - getAssignmentById, - getAssignmentsByClassId, - getClassroomAnnouncementsById, - getClassroomById, - getClassrooms, - getFaq, - getPeopleByClassId, - getUpcomingAssignmentsByClassId, -} from '../utils/mocks/api'; +import { StudentApi } from '../utils/mocks/api'; export default class StudentService { constructor(user) { this.user = user; } - fetchClassrooms = () => getClassrooms(this.user.id); + fetchClassrooms = () => StudentApi.getClassrooms(this.user.id); - fetchAllAssignments = () => getAllAssignments(this.user.id); + fetchAllAssignments = () => StudentApi.getAllAssignments(this.user.id); - fetchAssignmentById = assignmentId => getAssignmentById(assignmentId); + fetchAssignmentById = assignmentId => + StudentApi.getAssignmentById(assignmentId); - fetchAssignmentsByClassId = classId => getAssignmentsByClassId(classId); + fetchAssignmentsByClassId = classId => + StudentApi.getAssignmentsByClassId(classId); - fetchClassroomById = classId => getClassroomById(classId); + fetchClassroomById = classId => StudentApi.getClassroomById(classId); - fetchFAQ = () => getFaq(); + fetchFAQ = () => StudentApi.getFaq(); fetchClassroomAnnouncements = classId => - getClassroomAnnouncementsById(classId); + StudentApi.getClassroomAnnouncementsById(classId); fetchUpcomingAssignmentsByClassId = classId => - getUpcomingAssignmentsByClassId(classId); + StudentApi.getUpcomingAssignmentsByClassId(classId); - fetchPeopleByClassId = classId => getPeopleByClassId(classId); + fetchPeopleByClassId = classId => StudentApi.getPeopleByClassId(classId); } diff --git a/src/utils/mocks/api.js b/src/utils/mocks/api.js index 8735207..9f31001 100644 --- a/src/utils/mocks/api.js +++ b/src/utils/mocks/api.js @@ -11,118 +11,122 @@ import { allPeople, } from './responses'; -const getClassrooms = userId => - sleep(300).then(() => { - console.log('Get classrooms ' + userId); - return { - data: allClassrooms, - }; - }); +const CommonApi = { + getUser: (email, password) => + sleep(300).then(() => { + let user; + if (email === 'p@test.com' && password === 'p123') { + user = professorUser; + } else if (email === 's@test.com' && password === 's123') { + user = studentUser; + } else { + return authFailure; + } + window.localStorage.setItem('$USER', JSON.stringify(user)); + return user; + }), -const getClassroomById = classId => - sleep(300).then(() => { - console.log('Get classroom by id ' + classId); - return { - data: allClassrooms.filter(c => c.id === classId)[0], - }; - }); - -const getClassroomAnnouncementsById = classId => - sleep(300).then(() => { - console.log('Get classroon announcements by id ' + classId); - return { - data: allClassroomAnnouncements.filter(c => c.classroom.id === classId), - }; - }); - -const getUpcomingAssignmentsByClassId = classId => - sleep(300).then(() => { - console.log('Getting upcoming assignments by class id ' + classId); - return { - data: allUpcomingAssignments.filter( - a => a.classrooms.filter(c => c.id === classId)[0] - ), - }; - }); - -const getAllAssignments = userId => - sleep(400).then(() => { - console.log('Getting all assignments ' + userId); - return { - data: allAssignments, - }; - }); - -const getAssignmentById = assignmentId => - sleep(400).then(() => { - console.log('Getting assignment by id ' + assignmentId); - return { - data: allAssignments.filter(a => a.id === assignmentId)[0], - }; - }); - -const getAssignmentsByClassId = classId => - sleep(300).then(() => { - console.log('Getting assignments by class id ' + classId); - return { - data: allAssignments.filter(a => a.classrooms[0].id === classId), - }; - }); - -const getPeopleByClassId = classId => - sleep(400).then(() => { - console.log('Getting people by class id ' + classId); - return { - data: allPeople.filter(p => p.classes[0].id === classId), - }; - }); - -const getFaq = () => - sleep(300).then(() => { - console.log('Fetching FAQ...'); - return { - data: faq, - }; - }); - -const getUser = (email, password) => - sleep(300).then(() => { - let user; - if (email === 'p@test.com' && password === 'p123') { - user = professorUser; - } else if (email === 's@test.com' && password === 's123') { - user = studentUser; - } else { - return authFailure; - } - window.localStorage.setItem('$USER', JSON.stringify(user)); - return user; - }); - -const registerUser = data => - sleep(300).then(() => { - let userData; - if (data.email === 'p@test.com') { - userData = { ...data, role: 'PROFESSOR' }; - } else if (data.email === 's@test.com') { - userData = { ...data, role: 'STUDENT' }; - } else { - return authFailure; - } - window.localStorage.setItem('$USER', JSON.stringify(data)); - return userData; - }); - -export { - getClassrooms, - getClassroomById, - getAllAssignments, - getAssignmentById, - getAssignmentsByClassId, - getClassroomAnnouncementsById, - getUpcomingAssignmentsByClassId, - getPeopleByClassId, - getFaq, - getUser, - registerUser, + registerUser: data => + sleep(300).then(() => { + let userData; + if (data.email === 'p@test.com') { + userData = { ...data, role: 'PROFESSOR' }; + } else if (data.email === 's@test.com') { + userData = { ...data, role: 'STUDENT' }; + } else { + return authFailure; + } + window.localStorage.setItem('$USER', JSON.stringify(data)); + return userData; + }), }; + +const StudentApi = { + ...CommonApi, + getClassrooms: userId => + sleep(300).then(() => { + console.log('Get classrooms ' + userId); + return { + data: allClassrooms, + }; + }), + + getClassroomById: classId => + sleep(300).then(() => { + console.log('Get classroom by id ' + classId); + return { + data: allClassrooms.filter(c => c.id === classId)[0], + }; + }), + + getClassroomAnnouncementsById: classId => + sleep(300).then(() => { + console.log('Get classroon announcements by id ' + classId); + return { + data: allClassroomAnnouncements.filter(c => c.classroom.id === classId), + }; + }), + + getUpcomingAssignmentsByClassId: classId => + sleep(300).then(() => { + console.log('Getting upcoming assignments by class id ' + classId); + return { + data: allUpcomingAssignments.filter( + a => a.classrooms.filter(c => c.id === classId)[0] + ), + }; + }), + + getAllAssignments: userId => + sleep(400).then(() => { + console.log('Getting all assignments ' + userId); + return { + data: allAssignments, + }; + }), + + getAssignmentById: assignmentId => + sleep(400).then(() => { + console.log('Getting assignment by id ' + assignmentId); + return { + data: allAssignments.filter(a => a.id === assignmentId)[0], + }; + }), + + getAssignmentsByClassId: classId => + sleep(300).then(() => { + console.log('Getting assignments by class id ' + classId); + return { + data: allAssignments.filter(a => a.classrooms[0].id === classId), + }; + }), + + getPeopleByClassId: classId => + sleep(400).then(() => { + console.log('Getting people by class id ' + classId); + return { + data: allPeople.filter(p => p.classes[0].id === classId), + }; + }), + + getFaq: () => + sleep(300).then(() => { + console.log('Fetching FAQ...'); + return { + data: faq, + }; + }), +}; + +const ProfessorApi = { + ...CommonApi, + getClassrooms: userId => + sleep(300).then(() => { + console.log('Get classrooms ' + userId); + return { + data: allClassrooms, + }; + }), +}; + +export { StudentApi, ProfessorApi, CommonApi };