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 { createContext, useContext, useEffect, useState } from 'react';
|
||||||
import { useLocation } from 'react-router-dom';
|
import { useLocation } from 'react-router-dom';
|
||||||
|
import { UserServiceProvider } from '../services/user-service-provider';
|
||||||
import { useAuthState } from './auth';
|
import { useAuthState } from './auth';
|
||||||
import {
|
|
||||||
getAllAssignments,
|
|
||||||
getAssignmentById,
|
|
||||||
getAssignmentsByClassId,
|
|
||||||
getClassroomAnnouncementsById,
|
|
||||||
getClassroomById,
|
|
||||||
getClassrooms,
|
|
||||||
getFaq,
|
|
||||||
getPeopleByClassId,
|
|
||||||
getUpcomingAssignmentsByClassId,
|
|
||||||
} from '../services/user-service';
|
|
||||||
|
|
||||||
const UserContext = createContext();
|
const UserContext = createContext();
|
||||||
|
|
||||||
function UserProvider(props) {
|
function UserProvider(props) {
|
||||||
const { user } = useAuthState();
|
const { user } = useAuthState();
|
||||||
const { pathname } = useLocation();
|
const { pathname } = useLocation();
|
||||||
|
const [userService, setUserService] = useState(null);
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
user: null,
|
user: null,
|
||||||
error: null,
|
error: null,
|
||||||
|
@ -26,73 +17,23 @@ function UserProvider(props) {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setState({ user, pathname });
|
setState({ user, pathname });
|
||||||
|
|
||||||
|
async function initUserService() {
|
||||||
|
if (user) {
|
||||||
|
const instance = await UserServiceProvider.getInstance(user);
|
||||||
|
setUserService(instance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
initUserService();
|
||||||
}, [user, pathname]);
|
}, [user, pathname]);
|
||||||
|
|
||||||
const fetchClassrooms = () => getClassrooms(user.id);
|
return <UserContext.Provider value={{ state, userService }} {...props} />;
|
||||||
|
|
||||||
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}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function useUser() {
|
function useUser() {
|
||||||
const {
|
const { state, userService } = useContext(UserContext);
|
||||||
state,
|
|
||||||
fetchClassrooms,
|
|
||||||
fetchAssignmentById,
|
|
||||||
fetchAllAssignments,
|
|
||||||
fetchAssignmentsByClassId,
|
|
||||||
fetchClassroomById,
|
|
||||||
fetchFAQ,
|
|
||||||
fetchClassroomAnnouncements,
|
|
||||||
fetchUpcomingAssignmentsByClassId,
|
|
||||||
fetchPeopleByClassId,
|
|
||||||
} = useContext(UserContext);
|
|
||||||
|
|
||||||
return {
|
return { state, userService };
|
||||||
state,
|
|
||||||
fetchClassrooms,
|
|
||||||
fetchAllAssignments,
|
|
||||||
fetchAssignmentById,
|
|
||||||
fetchAssignmentsByClassId,
|
|
||||||
fetchClassroomById,
|
|
||||||
fetchFAQ,
|
|
||||||
fetchClassroomAnnouncements,
|
|
||||||
fetchUpcomingAssignmentsByClassId,
|
|
||||||
fetchPeopleByClassId,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { UserProvider, useUser };
|
export { UserProvider, useUser };
|
||||||
|
|
|
@ -8,14 +8,14 @@ import View from './View';
|
||||||
function Assignment() {
|
function Assignment() {
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
const layoutType = useLayoutType();
|
const layoutType = useLayoutType();
|
||||||
const { fetchAssignmentById } = useUser();
|
const { userService } = useUser();
|
||||||
const [assignment, setAssignment] = useState(null);
|
const [assignment, setAssignment] = useState(null);
|
||||||
const dropzone = useDropzone({ maxFiles: 5 });
|
const dropzone = useDropzone({ maxFiles: 5 });
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function getAssignmentById(assignmentId) {
|
async function getAssignmentById(assignmentId) {
|
||||||
document.title = 'Carregando...';
|
document.title = 'Carregando...';
|
||||||
const result = await fetchAssignmentById(assignmentId);
|
const result = await userService.fetchAssignmentById(assignmentId);
|
||||||
setAssignment(result.data);
|
setAssignment(result.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ function Assignment() {
|
||||||
|
|
||||||
getAssignmentById(params.id);
|
getAssignmentById(params.id);
|
||||||
updateDocumentTitle();
|
updateDocumentTitle();
|
||||||
}, [params, fetchAssignmentById, assignment]);
|
}, [params, userService, userService.fetchAssignmentById, assignment]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View assignment={assignment} dropzone={dropzone} layoutType={layoutType} />
|
<View assignment={assignment} dropzone={dropzone} layoutType={layoutType} />
|
||||||
|
|
|
@ -8,13 +8,7 @@ import View from './View';
|
||||||
function Classroom() {
|
function Classroom() {
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
const layoutType = useLayoutType();
|
const layoutType = useLayoutType();
|
||||||
const {
|
const { userService } = useUser();
|
||||||
fetchClassroomById,
|
|
||||||
fetchClassroomAnnouncements,
|
|
||||||
fetchUpcomingAssignmentsByClassId,
|
|
||||||
fetchAssignmentsByClassId,
|
|
||||||
fetchPeopleByClassId,
|
|
||||||
} = useUser();
|
|
||||||
const [classroom, setClassroom] = useState(null);
|
const [classroom, setClassroom] = useState(null);
|
||||||
const [tabData, setTabData] = useState(null);
|
const [tabData, setTabData] = useState(null);
|
||||||
const [selectedTabOption, setSelectedTabOption] = useState(
|
const [selectedTabOption, setSelectedTabOption] = useState(
|
||||||
|
@ -23,10 +17,11 @@ function Classroom() {
|
||||||
|
|
||||||
const fetchAndPopulateAnnouncementsTabData = useCallback(async () => {
|
const fetchAndPopulateAnnouncementsTabData = useCallback(async () => {
|
||||||
setTabData({ tab: 'announcements', state: 'loading' });
|
setTabData({ tab: 'announcements', state: 'loading' });
|
||||||
const announcements = await fetchClassroomAnnouncements(params.id);
|
const announcements = await userService.fetchClassroomAnnouncements(
|
||||||
const upcomingAssignments = await fetchUpcomingAssignmentsByClassId(
|
|
||||||
params.id
|
params.id
|
||||||
);
|
);
|
||||||
|
const upcomingAssignments =
|
||||||
|
await userService.fetchUpcomingAssignmentsByClassId(params.id);
|
||||||
|
|
||||||
setTabData({
|
setTabData({
|
||||||
tab: 'announcements',
|
tab: 'announcements',
|
||||||
|
@ -34,33 +29,29 @@ function Classroom() {
|
||||||
announcements: [...announcements.data],
|
announcements: [...announcements.data],
|
||||||
upcomingAssignments: [...upcomingAssignments.data],
|
upcomingAssignments: [...upcomingAssignments.data],
|
||||||
});
|
});
|
||||||
}, [
|
}, [userService, params.id]);
|
||||||
fetchClassroomAnnouncements,
|
|
||||||
fetchUpcomingAssignmentsByClassId,
|
|
||||||
params.id,
|
|
||||||
]);
|
|
||||||
|
|
||||||
const fetchAndPopulateAssignmentsTabData = useCallback(async () => {
|
const fetchAndPopulateAssignmentsTabData = useCallback(async () => {
|
||||||
setTabData({ tab: 'assignments', state: 'loading' });
|
setTabData({ tab: 'assignments', state: 'loading' });
|
||||||
const assignments = await fetchAssignmentsByClassId(params.id);
|
const assignments = await userService.fetchAssignmentsByClassId(params.id);
|
||||||
|
|
||||||
setTabData({
|
setTabData({
|
||||||
tab: 'assignments',
|
tab: 'assignments',
|
||||||
state: 'idle',
|
state: 'idle',
|
||||||
assignments: [...assignments.data],
|
assignments: [...assignments.data],
|
||||||
});
|
});
|
||||||
}, [fetchAssignmentsByClassId, params.id]);
|
}, [userService, params.id]);
|
||||||
|
|
||||||
const fetchAndPopulatePoepleTabData = useCallback(async () => {
|
const fetchAndPopulatePoepleTabData = useCallback(async () => {
|
||||||
setTabData({ tab: 'people', state: 'loading' });
|
setTabData({ tab: 'people', state: 'loading' });
|
||||||
const people = await fetchPeopleByClassId(params.id);
|
const people = await userService.fetchPeopleByClassId(params.id);
|
||||||
|
|
||||||
setTabData({
|
setTabData({
|
||||||
tab: 'people',
|
tab: 'people',
|
||||||
state: 'idle',
|
state: 'idle',
|
||||||
people: [...people.data],
|
people: [...people.data],
|
||||||
});
|
});
|
||||||
}, [fetchPeopleByClassId, params.id]);
|
}, [userService, params.id]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function getSelectedTabData() {
|
async function getSelectedTabData() {
|
||||||
|
@ -90,7 +81,7 @@ function Classroom() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function getClassroomById(classId) {
|
async function getClassroomById(classId) {
|
||||||
document.title = 'Carregando...';
|
document.title = 'Carregando...';
|
||||||
const result = await fetchClassroomById(classId);
|
const result = await userService.fetchClassroomById(classId);
|
||||||
setClassroom(result.data);
|
setClassroom(result.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +93,7 @@ function Classroom() {
|
||||||
|
|
||||||
getClassroomById(params.id);
|
getClassroomById(params.id);
|
||||||
updateDocumentTitle();
|
updateDocumentTitle();
|
||||||
}, [fetchClassroomById, params, classroom]);
|
}, [userService, userService.fetchClassroomById, params, classroom]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View
|
<View
|
||||||
|
|
|
@ -9,25 +9,25 @@ function Home() {
|
||||||
useDocumentTitle('Página Inicial');
|
useDocumentTitle('Página Inicial');
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const layoutType = useLayoutType();
|
const layoutType = useLayoutType();
|
||||||
const { fetchClassrooms, fetchAllAssignments } = useUser();
|
const { userService } = useUser();
|
||||||
const [classrooms, setClassrooms] = useState(null);
|
const [classrooms, setClassrooms] = useState(null);
|
||||||
const [assignments, setAssignments] = useState(null);
|
const [assignments, setAssignments] = useState(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function getClassrooms() {
|
async function getClassrooms() {
|
||||||
const result = await fetchClassrooms();
|
const result = await userService.fetchClassrooms();
|
||||||
setClassrooms(result.data);
|
setClassrooms(result.data);
|
||||||
}
|
}
|
||||||
getClassrooms();
|
getClassrooms();
|
||||||
}, [fetchClassrooms]);
|
}, [userService, userService.fetchClassrooms]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function getAssignments() {
|
async function getAssignments() {
|
||||||
const result = await fetchAllAssignments();
|
const result = await userService.fetchAllAssignments();
|
||||||
setAssignments(result.data);
|
setAssignments(result.data);
|
||||||
}
|
}
|
||||||
getAssignments();
|
getAssignments();
|
||||||
}, [fetchAllAssignments]);
|
}, [userService, userService.fetchAllAssignments]);
|
||||||
|
|
||||||
const onClickClassCard = id => {
|
const onClickClassCard = id => {
|
||||||
navigate(`/class/${id}`);
|
navigate(`/class/${id}`);
|
||||||
|
|
|
@ -9,16 +9,16 @@ import { sectors } from './data';
|
||||||
function Information() {
|
function Information() {
|
||||||
useDocumentTitle('Informações');
|
useDocumentTitle('Informações');
|
||||||
const layoutType = useLayoutType();
|
const layoutType = useLayoutType();
|
||||||
const { fetchFAQ } = useUser();
|
const { userService } = useUser();
|
||||||
const [faq, setFaq] = useState(null);
|
const [faq, setFaq] = useState(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function getClassrooms() {
|
async function getClassrooms() {
|
||||||
const result = await fetchFAQ();
|
const result = await userService.fetchFAQ();
|
||||||
setFaq(result.data);
|
setFaq(result.data);
|
||||||
}
|
}
|
||||||
getClassrooms();
|
getClassrooms();
|
||||||
}, [fetchFAQ]);
|
}, [userService, userService.fetchFAQ]);
|
||||||
|
|
||||||
return <View faq={faq} sectors={sectors} layoutType={layoutType} />;
|
return <View faq={faq} sectors={sectors} layoutType={layoutType} />;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,16 +9,16 @@ function Home() {
|
||||||
useDocumentTitle('Página Inicial');
|
useDocumentTitle('Página Inicial');
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const layoutType = useLayoutType();
|
const layoutType = useLayoutType();
|
||||||
const { fetchClassrooms } = useUser();
|
const { userService } = useUser();
|
||||||
const [classrooms, setClassrooms] = useState(null);
|
const [classrooms, setClassrooms] = useState(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function getClassrooms() {
|
async function getClassrooms() {
|
||||||
const result = await fetchClassrooms();
|
const result = await userService.fetchClassrooms();
|
||||||
setClassrooms(result.data);
|
setClassrooms(result.data);
|
||||||
}
|
}
|
||||||
getClassrooms();
|
getClassrooms();
|
||||||
}, [fetchClassrooms]);
|
}, [userService, userService.fetchClassrooms]);
|
||||||
|
|
||||||
const onClickClassCard = id => {
|
const onClickClassCard = id => {
|
||||||
navigate(`/class/${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