Structure mocks in different apis

This commit is contained in:
Leonardo Murça 2022-11-29 18:24:13 -03:00
parent 38284ec7bf
commit 2b1302ad3a
4 changed files with 136 additions and 136 deletions

View file

@ -1,5 +1,5 @@
import { createContext, useContext, useEffect, useState } from 'react'; import { createContext, useContext, useEffect, useState } from 'react';
import { getUser, registerUser } from '../utils/mocks/api'; import { CommonApi } from '../utils/mocks/api';
const AuthContext = createContext(); const AuthContext = createContext();
@ -23,7 +23,7 @@ function AuthProvider(props) {
const register = data => { const register = data => {
setState({ ...state, status: 'pending' }); setState({ ...state, status: 'pending' });
return registerUser(data).then(data => { return CommonApi.registerUser(data).then(data => {
if (data.message) { if (data.message) {
return setState({ status: 'error', user: null, error: data }); return setState({ status: 'error', user: null, error: data });
} else { } else {
@ -35,7 +35,7 @@ function AuthProvider(props) {
const login = (email, password) => { const login = (email, password) => {
setState({ ...state, status: 'pending' }); setState({ ...state, status: 'pending' });
return getUser(email, password).then(data => { return CommonApi.getUser(email, password).then(data => {
if (data.message) { if (data.message) {
return setState({ status: 'error', user: null, error: data }); return setState({ status: 'error', user: null, error: data });
} else { } else {

View file

@ -1,5 +1,9 @@
import { ProfessorApi } from '../utils/mocks/api';
export default class ProfessorService { export default class ProfessorService {
constructor(user) { constructor(user) {
this.user = user; this.user = user;
} }
fetchClassrooms = () => ProfessorApi.getClassrooms(this.user.id);
} }

View file

@ -1,37 +1,29 @@
import { import { StudentApi } from '../utils/mocks/api';
getAllAssignments,
getAssignmentById,
getAssignmentsByClassId,
getClassroomAnnouncementsById,
getClassroomById,
getClassrooms,
getFaq,
getPeopleByClassId,
getUpcomingAssignmentsByClassId,
} from '../utils/mocks/api';
export default class StudentService { export default class StudentService {
constructor(user) { constructor(user) {
this.user = 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 => fetchClassroomAnnouncements = classId =>
getClassroomAnnouncementsById(classId); StudentApi.getClassroomAnnouncementsById(classId);
fetchUpcomingAssignmentsByClassId = classId => fetchUpcomingAssignmentsByClassId = classId =>
getUpcomingAssignmentsByClassId(classId); StudentApi.getUpcomingAssignmentsByClassId(classId);
fetchPeopleByClassId = classId => getPeopleByClassId(classId); fetchPeopleByClassId = classId => StudentApi.getPeopleByClassId(classId);
} }

View file

@ -11,81 +11,8 @@ import {
allPeople, allPeople,
} from './responses'; } from './responses';
const getClassrooms = userId => const CommonApi = {
sleep(300).then(() => { getUser: (email, password) =>
console.log('Get classrooms ' + userId);
return {
data: allClassrooms,
};
});
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(() => { sleep(300).then(() => {
let user; let user;
if (email === 'p@test.com' && password === 'p123') { if (email === 'p@test.com' && password === 'p123') {
@ -97,9 +24,9 @@ const getUser = (email, password) =>
} }
window.localStorage.setItem('$USER', JSON.stringify(user)); window.localStorage.setItem('$USER', JSON.stringify(user));
return user; return user;
}); }),
const registerUser = data => registerUser: data =>
sleep(300).then(() => { sleep(300).then(() => {
let userData; let userData;
if (data.email === 'p@test.com') { if (data.email === 'p@test.com') {
@ -111,18 +38,95 @@ const registerUser = data =>
} }
window.localStorage.setItem('$USER', JSON.stringify(data)); window.localStorage.setItem('$USER', JSON.stringify(data));
return userData; return userData;
}); }),
export {
getClassrooms,
getClassroomById,
getAllAssignments,
getAssignmentById,
getAssignmentsByClassId,
getClassroomAnnouncementsById,
getUpcomingAssignmentsByClassId,
getPeopleByClassId,
getFaq,
getUser,
registerUser,
}; };
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 };