diff --git a/src/context/user.js b/src/context/user.js
index 23a3df4..afb4527 100644
--- a/src/context/user.js
+++ b/src/context/user.js
@@ -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 (
-
- );
+ return ;
}
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 };
diff --git a/src/screens/Assignment/index.js b/src/screens/Assignment/index.js
index 3559d3b..3357c2b 100644
--- a/src/screens/Assignment/index.js
+++ b/src/screens/Assignment/index.js
@@ -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 (
diff --git a/src/screens/Classroom/index.js b/src/screens/Classroom/index.js
index 7410515..10fc044 100644
--- a/src/screens/Classroom/index.js
+++ b/src/screens/Classroom/index.js
@@ -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 (
{
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}`);
diff --git a/src/screens/Information/index.js b/src/screens/Information/index.js
index b1f7965..1042060 100644
--- a/src/screens/Information/index.js
+++ b/src/screens/Information/index.js
@@ -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 ;
}
diff --git a/src/screens/professor/Home/index.js b/src/screens/professor/Home/index.js
index 91eddc3..32580b6 100644
--- a/src/screens/professor/Home/index.js
+++ b/src/screens/professor/Home/index.js
@@ -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}`);
diff --git a/src/services/professor-service.js b/src/services/professor-service.js
new file mode 100644
index 0000000..5589b51
--- /dev/null
+++ b/src/services/professor-service.js
@@ -0,0 +1,5 @@
+export default class ProfessorService {
+ constructor(user) {
+ this.user = user;
+ }
+}
diff --git a/src/services/student-service.js b/src/services/student-service.js
new file mode 100644
index 0000000..5b9f297
--- /dev/null
+++ b/src/services/student-service.js
@@ -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);
+}
diff --git a/src/services/user-service-provider.js b/src/services/user-service-provider.js
new file mode 100644
index 0000000..98224fc
--- /dev/null
+++ b/src/services/user-service-provider.js
@@ -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;
+ },
+ };
+})();