summaryrefslogtreecommitdiff
path: root/src/context
diff options
context:
space:
mode:
Diffstat (limited to 'src/context')
-rw-r--r--src/context/auth.js12
-rw-r--r--src/context/user.js85
2 files changed, 18 insertions, 79 deletions
diff --git a/src/context/auth.js b/src/context/auth.js
index 655b8d1..59ccd82 100644
--- a/src/context/auth.js
+++ b/src/context/auth.js
@@ -1,5 +1,5 @@
import { createContext, useContext, useEffect, useState } from 'react';
-import { getUser, registerUser } from '../services/user-service';
+import { CommonApi } from '../utils/mocks/api';
const AuthContext = createContext();
@@ -22,10 +22,9 @@ function AuthProvider(props) {
const register = data => {
setState({ ...state, status: 'pending' });
- let shouldFail = false;
- return registerUser(data, shouldFail).then(data => {
- if (shouldFail) {
+ return CommonApi.registerUser(data).then(data => {
+ if (data.message) {
return setState({ status: 'error', user: null, error: data });
} else {
return setState({ status: 'success', user: data, error: null });
@@ -35,10 +34,9 @@ function AuthProvider(props) {
const login = (email, password) => {
setState({ ...state, status: 'pending' });
- let shouldFail = email !== 'teste@teste.com' || password !== '#teste1234';
- return getUser(shouldFail).then(data => {
- if (shouldFail) {
+ return CommonApi.getUser(email, password).then(data => {
+ if (data.message) {
return setState({ status: 'error', user: null, error: data });
} else {
return setState({ status: 'success', user: data, error: null });
diff --git a/src/context/user.js b/src/context/user.js
index 23a3df4..4225201 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/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 });
- }, [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);
+ async function initUserService() {
+ if (user) {
+ const instance = await UserServiceProvider.getInstance(user);
+ setUserService(instance);
+ }
+ }
+ initUserService();
+ }, [user, pathname]);
- 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 };