Extract get user mock function to user service

This commit is contained in:
Leonardo Murça 2022-07-07 15:33:40 -03:00
parent e6cbb15ce3
commit 2c768058aa
3 changed files with 26 additions and 20 deletions

View file

@ -1,20 +1,5 @@
import { createContext, useContext, useEffect, useState } from 'react'; import { createContext, useContext, useEffect, useState } from 'react';
import { sleep } from '../utils/sleep'; import { getUser } from '../services/user-service';
const getUser = shouldFail =>
sleep(3000).then(() => {
if (shouldFail) {
return { message: 'Falha na autenticação' };
} else {
return {
id: '0021564',
username: 'leonardomurca',
name: 'Leonardo',
lastName: 'Murça',
token: 'skdfb9458hnsdfsif4w38r9234ry98423',
};
}
});
const AuthContext = createContext(); const AuthContext = createContext();
@ -43,7 +28,6 @@ function AuthProvider(props) {
if (shouldFail) { if (shouldFail) {
return setState({ status: 'error', user: null, error: data }); return setState({ status: 'error', user: null, error: data });
} else { } else {
window.localStorage.setItem('$USER', JSON.stringify(data));
return setState({ status: 'success', user: data, error: null }); return setState({ status: 'success', user: data, error: null });
} }
}); });

View file

@ -166,4 +166,16 @@ const faq = [
}, },
]; ];
export { allClassrooms, allAssignments, faq }; const user = {
id: '0021564',
username: 'leonardomurca',
name: 'Leonardo',
lastName: 'Murça',
token: 'skdfb9458hnsdfsif4w38r9234ry98423',
};
const authFailure = {
message: 'Falha na autenticação',
};
export { allClassrooms, allAssignments, faq, user, authFailure };

View file

@ -1,5 +1,5 @@
import { sleep } from '../utils/sleep'; import { sleep } from '../utils/sleep';
import { allClassrooms, allAssignments, faq } from './mocks'; import { allClassrooms, allAssignments, faq, user, authFailure } from './mocks';
const getClassrooms = userId => const getClassrooms = userId =>
sleep(3000).then(() => { sleep(3000).then(() => {
@ -33,4 +33,14 @@ const getFaq = () =>
}; };
}); });
export { getClassrooms, getClassroomById, getAssignments, getFaq }; const getUser = shouldFail =>
sleep(3000).then(() => {
if (shouldFail) {
return authFailure;
} else {
window.localStorage.setItem('$USER', JSON.stringify(user));
return user;
}
});
export { getClassrooms, getClassroomById, getAssignments, getFaq, getUser };