Split services between student and professor roles
This commit is contained in:
parent
a580f2c199
commit
bbea9b2f0f
9 changed files with 108 additions and 106 deletions
|
@ -1,23 +1,14 @@
|
|||
import { createContext, useContext, useEffect, useState } from 'react';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import { UserServiceProvider } from '../services/user-service-provider';
|
||||
import { useAuthState } from './auth';
|
||||
import {
|
||||
getAllAssignments,
|
||||
getAssignmentById,
|
||||
getAssignmentsByClassId,
|
||||
getClassroomAnnouncementsById,
|
||||
getClassroomById,
|
||||
getClassrooms,
|
||||
getFaq,
|
||||
getPeopleByClassId,
|
||||
getUpcomingAssignmentsByClassId,
|
||||
} from '../services/user-service';
|
||||
|
||||
const UserContext = createContext();
|
||||
|
||||
function UserProvider(props) {
|
||||
const { user } = useAuthState();
|
||||
const { pathname } = useLocation();
|
||||
const [userService, setUserService] = useState(null);
|
||||
const [state, setState] = useState({
|
||||
user: null,
|
||||
error: null,
|
||||
|
@ -26,73 +17,23 @@ function UserProvider(props) {
|
|||
|
||||
useEffect(() => {
|
||||
setState({ user, pathname });
|
||||
|
||||
async function initUserService() {
|
||||
if (user) {
|
||||
const instance = await UserServiceProvider.getInstance(user);
|
||||
setUserService(instance);
|
||||
}
|
||||
}
|
||||
initUserService();
|
||||
}, [user, pathname]);
|
||||
|
||||
const fetchClassrooms = () => getClassrooms(user.id);
|
||||
|
||||
const fetchAllAssignments = () => getAllAssignments(user.id);
|
||||
|
||||
const fetchAssignmentById = assignmentId => getAssignmentById(assignmentId);
|
||||
|
||||
const fetchAssignmentsByClassId = classId => getAssignmentsByClassId(classId);
|
||||
|
||||
const fetchClassroomById = classId => getClassroomById(classId);
|
||||
|
||||
const fetchFAQ = () => getFaq();
|
||||
|
||||
const fetchClassroomAnnouncements = classId =>
|
||||
getClassroomAnnouncementsById(classId);
|
||||
|
||||
const fetchUpcomingAssignmentsByClassId = classId =>
|
||||
getUpcomingAssignmentsByClassId(classId);
|
||||
|
||||
const fetchPeopleByClassId = classId => getPeopleByClassId(classId);
|
||||
|
||||
return (
|
||||
<UserContext.Provider
|
||||
value={{
|
||||
state,
|
||||
fetchClassrooms,
|
||||
fetchAllAssignments,
|
||||
fetchAssignmentById,
|
||||
fetchAssignmentsByClassId,
|
||||
fetchClassroomById,
|
||||
fetchFAQ,
|
||||
fetchClassroomAnnouncements,
|
||||
fetchUpcomingAssignmentsByClassId,
|
||||
fetchPeopleByClassId,
|
||||
}}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
return <UserContext.Provider value={{ state, userService }} {...props} />;
|
||||
}
|
||||
|
||||
function useUser() {
|
||||
const {
|
||||
state,
|
||||
fetchClassrooms,
|
||||
fetchAssignmentById,
|
||||
fetchAllAssignments,
|
||||
fetchAssignmentsByClassId,
|
||||
fetchClassroomById,
|
||||
fetchFAQ,
|
||||
fetchClassroomAnnouncements,
|
||||
fetchUpcomingAssignmentsByClassId,
|
||||
fetchPeopleByClassId,
|
||||
} = useContext(UserContext);
|
||||
const { state, userService } = useContext(UserContext);
|
||||
|
||||
return {
|
||||
state,
|
||||
fetchClassrooms,
|
||||
fetchAllAssignments,
|
||||
fetchAssignmentById,
|
||||
fetchAssignmentsByClassId,
|
||||
fetchClassroomById,
|
||||
fetchFAQ,
|
||||
fetchClassroomAnnouncements,
|
||||
fetchUpcomingAssignmentsByClassId,
|
||||
fetchPeopleByClassId,
|
||||
};
|
||||
return { state, userService };
|
||||
}
|
||||
|
||||
export { UserProvider, useUser };
|
||||
|
|
|
@ -8,14 +8,14 @@ import View from './View';
|
|||
function Assignment() {
|
||||
const params = useParams();
|
||||
const layoutType = useLayoutType();
|
||||
const { fetchAssignmentById } = useUser();
|
||||
const { userService } = useUser();
|
||||
const [assignment, setAssignment] = useState(null);
|
||||
const dropzone = useDropzone({ maxFiles: 5 });
|
||||
|
||||
useEffect(() => {
|
||||
async function getAssignmentById(assignmentId) {
|
||||
document.title = 'Carregando...';
|
||||
const result = await fetchAssignmentById(assignmentId);
|
||||
const result = await userService.fetchAssignmentById(assignmentId);
|
||||
setAssignment(result.data);
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ function Assignment() {
|
|||
|
||||
getAssignmentById(params.id);
|
||||
updateDocumentTitle();
|
||||
}, [params, fetchAssignmentById, assignment]);
|
||||
}, [params, userService, userService.fetchAssignmentById, assignment]);
|
||||
|
||||
return (
|
||||
<View assignment={assignment} dropzone={dropzone} layoutType={layoutType} />
|
||||
|
|
|
@ -8,13 +8,7 @@ import View from './View';
|
|||
function Classroom() {
|
||||
const params = useParams();
|
||||
const layoutType = useLayoutType();
|
||||
const {
|
||||
fetchClassroomById,
|
||||
fetchClassroomAnnouncements,
|
||||
fetchUpcomingAssignmentsByClassId,
|
||||
fetchAssignmentsByClassId,
|
||||
fetchPeopleByClassId,
|
||||
} = useUser();
|
||||
const { userService } = useUser();
|
||||
const [classroom, setClassroom] = useState(null);
|
||||
const [tabData, setTabData] = useState(null);
|
||||
const [selectedTabOption, setSelectedTabOption] = useState(
|
||||
|
@ -23,10 +17,11 @@ function Classroom() {
|
|||
|
||||
const fetchAndPopulateAnnouncementsTabData = useCallback(async () => {
|
||||
setTabData({ tab: 'announcements', state: 'loading' });
|
||||
const announcements = await fetchClassroomAnnouncements(params.id);
|
||||
const upcomingAssignments = await fetchUpcomingAssignmentsByClassId(
|
||||
const announcements = await userService.fetchClassroomAnnouncements(
|
||||
params.id
|
||||
);
|
||||
const upcomingAssignments =
|
||||
await userService.fetchUpcomingAssignmentsByClassId(params.id);
|
||||
|
||||
setTabData({
|
||||
tab: 'announcements',
|
||||
|
@ -34,33 +29,29 @@ function Classroom() {
|
|||
announcements: [...announcements.data],
|
||||
upcomingAssignments: [...upcomingAssignments.data],
|
||||
});
|
||||
}, [
|
||||
fetchClassroomAnnouncements,
|
||||
fetchUpcomingAssignmentsByClassId,
|
||||
params.id,
|
||||
]);
|
||||
}, [userService, params.id]);
|
||||
|
||||
const fetchAndPopulateAssignmentsTabData = useCallback(async () => {
|
||||
setTabData({ tab: 'assignments', state: 'loading' });
|
||||
const assignments = await fetchAssignmentsByClassId(params.id);
|
||||
const assignments = await userService.fetchAssignmentsByClassId(params.id);
|
||||
|
||||
setTabData({
|
||||
tab: 'assignments',
|
||||
state: 'idle',
|
||||
assignments: [...assignments.data],
|
||||
});
|
||||
}, [fetchAssignmentsByClassId, params.id]);
|
||||
}, [userService, params.id]);
|
||||
|
||||
const fetchAndPopulatePoepleTabData = useCallback(async () => {
|
||||
setTabData({ tab: 'people', state: 'loading' });
|
||||
const people = await fetchPeopleByClassId(params.id);
|
||||
const people = await userService.fetchPeopleByClassId(params.id);
|
||||
|
||||
setTabData({
|
||||
tab: 'people',
|
||||
state: 'idle',
|
||||
people: [...people.data],
|
||||
});
|
||||
}, [fetchPeopleByClassId, params.id]);
|
||||
}, [userService, params.id]);
|
||||
|
||||
useEffect(() => {
|
||||
async function getSelectedTabData() {
|
||||
|
@ -90,7 +81,7 @@ function Classroom() {
|
|||
useEffect(() => {
|
||||
async function getClassroomById(classId) {
|
||||
document.title = 'Carregando...';
|
||||
const result = await fetchClassroomById(classId);
|
||||
const result = await userService.fetchClassroomById(classId);
|
||||
setClassroom(result.data);
|
||||
}
|
||||
|
||||
|
@ -102,7 +93,7 @@ function Classroom() {
|
|||
|
||||
getClassroomById(params.id);
|
||||
updateDocumentTitle();
|
||||
}, [fetchClassroomById, params, classroom]);
|
||||
}, [userService, userService.fetchClassroomById, params, classroom]);
|
||||
|
||||
return (
|
||||
<View
|
||||
|
|
|
@ -9,25 +9,25 @@ function Home() {
|
|||
useDocumentTitle('Página Inicial');
|
||||
const navigate = useNavigate();
|
||||
const layoutType = useLayoutType();
|
||||
const { fetchClassrooms, fetchAllAssignments } = useUser();
|
||||
const { userService } = useUser();
|
||||
const [classrooms, setClassrooms] = useState(null);
|
||||
const [assignments, setAssignments] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
async function getClassrooms() {
|
||||
const result = await fetchClassrooms();
|
||||
const result = await userService.fetchClassrooms();
|
||||
setClassrooms(result.data);
|
||||
}
|
||||
getClassrooms();
|
||||
}, [fetchClassrooms]);
|
||||
}, [userService, userService.fetchClassrooms]);
|
||||
|
||||
useEffect(() => {
|
||||
async function getAssignments() {
|
||||
const result = await fetchAllAssignments();
|
||||
const result = await userService.fetchAllAssignments();
|
||||
setAssignments(result.data);
|
||||
}
|
||||
getAssignments();
|
||||
}, [fetchAllAssignments]);
|
||||
}, [userService, userService.fetchAllAssignments]);
|
||||
|
||||
const onClickClassCard = id => {
|
||||
navigate(`/class/${id}`);
|
||||
|
|
|
@ -9,16 +9,16 @@ import { sectors } from './data';
|
|||
function Information() {
|
||||
useDocumentTitle('Informações');
|
||||
const layoutType = useLayoutType();
|
||||
const { fetchFAQ } = useUser();
|
||||
const { userService } = useUser();
|
||||
const [faq, setFaq] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
async function getClassrooms() {
|
||||
const result = await fetchFAQ();
|
||||
const result = await userService.fetchFAQ();
|
||||
setFaq(result.data);
|
||||
}
|
||||
getClassrooms();
|
||||
}, [fetchFAQ]);
|
||||
}, [userService, userService.fetchFAQ]);
|
||||
|
||||
return <View faq={faq} sectors={sectors} layoutType={layoutType} />;
|
||||
}
|
||||
|
|
|
@ -9,16 +9,16 @@ function Home() {
|
|||
useDocumentTitle('Página Inicial');
|
||||
const navigate = useNavigate();
|
||||
const layoutType = useLayoutType();
|
||||
const { fetchClassrooms } = useUser();
|
||||
const { userService } = useUser();
|
||||
const [classrooms, setClassrooms] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
async function getClassrooms() {
|
||||
const result = await fetchClassrooms();
|
||||
const result = await userService.fetchClassrooms();
|
||||
setClassrooms(result.data);
|
||||
}
|
||||
getClassrooms();
|
||||
}, [fetchClassrooms]);
|
||||
}, [userService, userService.fetchClassrooms]);
|
||||
|
||||
const onClickClassCard = id => {
|
||||
navigate(`/class/${id}`);
|
||||
|
|
5
src/services/professor-service.js
Normal file
5
src/services/professor-service.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
export default class ProfessorService {
|
||||
constructor(user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
37
src/services/student-service.js
Normal file
37
src/services/student-service.js
Normal file
|
@ -0,0 +1,37 @@
|
|||
import {
|
||||
getAllAssignments,
|
||||
getAssignmentById,
|
||||
getAssignmentsByClassId,
|
||||
getClassroomAnnouncementsById,
|
||||
getClassroomById,
|
||||
getClassrooms,
|
||||
getFaq,
|
||||
getPeopleByClassId,
|
||||
getUpcomingAssignmentsByClassId,
|
||||
} from './user-service';
|
||||
|
||||
export default class StudentService {
|
||||
constructor(user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
fetchClassrooms = () => getClassrooms(this.user.id);
|
||||
|
||||
fetchAllAssignments = () => getAllAssignments(this.user.id);
|
||||
|
||||
fetchAssignmentById = assignmentId => getAssignmentById(assignmentId);
|
||||
|
||||
fetchAssignmentsByClassId = classId => getAssignmentsByClassId(classId);
|
||||
|
||||
fetchClassroomById = classId => getClassroomById(classId);
|
||||
|
||||
fetchFAQ = () => getFaq();
|
||||
|
||||
fetchClassroomAnnouncements = classId =>
|
||||
getClassroomAnnouncementsById(classId);
|
||||
|
||||
fetchUpcomingAssignmentsByClassId = classId =>
|
||||
getUpcomingAssignmentsByClassId(classId);
|
||||
|
||||
fetchPeopleByClassId = classId => getPeopleByClassId(classId);
|
||||
}
|
28
src/services/user-service-provider.js
Normal file
28
src/services/user-service-provider.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
export const UserServiceProvider = (function () {
|
||||
let instance;
|
||||
|
||||
async function createInstance(user) {
|
||||
if (user.role === 'STUDENT') {
|
||||
const service = await import('../services/student-service');
|
||||
if (service) {
|
||||
return new service.default(user);
|
||||
}
|
||||
} else if (user.role === 'PROFESSOR') {
|
||||
const service = await import('../services/professor-service');
|
||||
if (service) {
|
||||
return new service.default(user);
|
||||
}
|
||||
} else {
|
||||
throw new Error('Invalid Role!');
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
getInstance: function (user) {
|
||||
if (!instance) {
|
||||
instance = createInstance(user);
|
||||
}
|
||||
return instance;
|
||||
},
|
||||
};
|
||||
})();
|
Loading…
Reference in a new issue