Structure mocks in different apis
This commit is contained in:
parent
38284ec7bf
commit
2b1302ad3a
4 changed files with 136 additions and 136 deletions
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -11,81 +11,8 @@ import {
|
|||
allPeople,
|
||||
} from './responses';
|
||||
|
||||
const getClassrooms = userId =>
|
||||
sleep(300).then(() => {
|
||||
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) =>
|
||||
const CommonApi = {
|
||||
getUser: (email, password) =>
|
||||
sleep(300).then(() => {
|
||||
let user;
|
||||
if (email === 'p@test.com' && password === 'p123') {
|
||||
|
@ -97,9 +24,9 @@ const getUser = (email, password) =>
|
|||
}
|
||||
window.localStorage.setItem('$USER', JSON.stringify(user));
|
||||
return user;
|
||||
});
|
||||
}),
|
||||
|
||||
const registerUser = data =>
|
||||
registerUser: data =>
|
||||
sleep(300).then(() => {
|
||||
let userData;
|
||||
if (data.email === 'p@test.com') {
|
||||
|
@ -111,18 +38,95 @@ const registerUser = data =>
|
|||
}
|
||||
window.localStorage.setItem('$USER', JSON.stringify(data));
|
||||
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 };
|
||||
|
|
Loading…
Reference in a new issue