Skip to content

Commit

Permalink
Merge branch 'dev' into Conexion-Informar-Pago
Browse files Browse the repository at this point in the history
Signed-off-by: Yoel Marain <[email protected]>
  • Loading branch information
yoelmarain authored Sep 25, 2024
2 parents 3260d8d + 8cdc35a commit 9fa8e5d
Show file tree
Hide file tree
Showing 16 changed files with 501 additions and 367 deletions.
194 changes: 109 additions & 85 deletions FrontAdmin/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion FrontAdmin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@
"eslint-plugin-react-refresh": "^0.4.6",
"prettier": "^3.3.3",
"typescript": "^5.5.4",
"vite": "^5.4.0"
"vite": "^5.4.7"
}
}
24 changes: 24 additions & 0 deletions FrontAdmin/src/API/EstadoCuentaAlumno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Cookies from 'js-cookie';

export const FetchEstadoCuenta = async (dni: number) => {
try {
const token = Cookies.get('tokennn');

const response = await fetch(`http://localhost:8000/api/cuotas/alumno/${dni}/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});

if (response.ok) {
const data = await response.json();
return data;
} else {
throw new Error('Error en la respuesta del servidor');
}
} catch (error) {
throw new Error('Network error: ' + error);
}
};
5 changes: 5 additions & 0 deletions FrontAdmin/src/API/Login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Cookies from 'js-cookie';
const URL = import.meta.env.VITE_URL_DEV;

export const FetchLogin = async (password: string, account: string) => {
console.log(account, password);
try {
const response = await fetch(`http://localhost:8000/api/auth/login/`, {
method: 'POST',
Expand All @@ -15,12 +16,16 @@ export const FetchLogin = async (password: string, account: string) => {
if (response.ok) {
const data = await response.json();
console.log(data);
const roles = data.user.roles;
localStorage.setItem('userRol', JSON.stringify(roles));

Cookies.set('tokennn', data.access);
Cookies.set('dni', data.user.dni);
Cookies.set('refresh_token', data.refresh);
Cookies.set('access_expiration', data.access_expiration);
Cookies.set('refresh_expiration', data.refresh_expiration);
Cookies.set('username', data.user.dni);

return data;
} else {
const errorResponse = await response.json();
Expand Down
31 changes: 27 additions & 4 deletions FrontAdmin/src/API/Materias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const FetchPostMateria = async (
try {
const token = Cookies.get('tokennn');

const response = await fetch(`http://localhost:8000/api/materias/`, {
const response = await fetch(`${URL}/materias/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -44,7 +44,7 @@ export const FetchMaterias = async () => {
try {
const token = Cookies.get('tokennn');

const response = await fetch(`http://localhost:8000/api/materias/`, {
const response = await fetch(`${URL}/materias/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -73,7 +73,7 @@ export const FetchPutMateria = async (
try {
const token = Cookies.get('tokennn');

const response = await fetch(`http://localhost:8000/api/materias/${codigo_materia}/`, {
const response = await fetch(`${URL}/materias/${codigo_materia}/`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -106,7 +106,7 @@ export const FetchDeleteMateria = async (codigo_materia: number) => {
try {
const token = Cookies.get('tokennn');

const response = await fetch(`http://localhost:8000/api/materias/${codigo_materia}/`, {
const response = await fetch(`${URL}/materias/${codigo_materia}/`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
Expand All @@ -126,3 +126,26 @@ export const FetchDeleteMateria = async (codigo_materia: number) => {
console.error('Network error:', error);
}
};

export const FetchAlumnosMaterias = async (codigo_materia: number) => {
try {
const token = Cookies.get('tokennn');

const response = await fetch(`http://localhost:8000/api/materias/${codigo_materia}/alumnos/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});

if (response.ok) {
const data = await response.json();
return data;
} else {
throw new Error('Error en la respuesta del servidor');
}
} catch (error) {
throw new Error('Network error: ' + error);
}
};
8 changes: 4 additions & 4 deletions FrontAdmin/src/API/Montos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const URL = import.meta.env.VITE_URL_DEV;

export const FetchMontos = async (offset: number, limit: number) => {
try {
const token = Cookies.get('access_token');
const token = Cookies.get('tokennn');

const response = await fetch(
`${URL}/pagos/compromisos/?offset=${offset}&limit=${limit}`,
Expand All @@ -30,9 +30,9 @@ export const FetchMontos = async (offset: number, limit: number) => {

export const createCompromiso = async (compromisoData: any, selectFile: any) => {
try {
const token = Cookies.get('access_token');
const token = Cookies.get('tokennn');

const response = await fetch(`${URL}/pagos/compromisos/`, {
const response = await fetch(`http://localhost:8000/api/compromisos/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -58,7 +58,7 @@ export const createCompromiso = async (compromisoData: any, selectFile: any) =>

export const loadPDF = async (id :string,file: File) => {
try {
const token = Cookies.get('access_token');
const token = Cookies.get('tokennn');

const formData = new FormData();
formData.append('archivo_pdf', file);
Expand Down
23 changes: 12 additions & 11 deletions FrontAdmin/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Auth from './layouts/Auth';
import Admin from './layouts/Admin';
import Alumnos from './layouts/Alumnos';
import theme from './theme/theme';
import Cookies from 'js-cookie';

function App() {
useEffect(() => {
Expand All @@ -15,29 +14,31 @@ function App() {

const { isAuthenticated, rolUser } = useAuth();
console.log(isAuthenticated, rolUser);
/*samu*/
const isAdmin = rolUser.includes('staff') || rolUser.includes('superuser');
// const isAlumno = rolUser.includes('alumno');


return (
<ChakraProvider theme={theme}>
<BrowserRouter>
<Routes>
{!isAuthenticated && <Route path="/auth/*" element={<Auth />} />}
{isAuthenticated && rolUser && (
<Route path="/admin/*" element={<Admin />} />
)}
{isAuthenticated && !rolUser && (
<Route path="/alumnos/*" element={<Alumnos />} />
)}
{isAuthenticated && isAdmin && (
<Route path="/admin/*" element={<Admin />} />
)}
{isAuthenticated && !isAdmin && (
<Route path="/alumnos/*" element={<Alumnos />} />
)}
<Route
path="/*"
element={
<Navigate
replace
to={
isAuthenticated
? rolUser
? '/admin/estadisticas'
: '/alumnos/pagos'
? isAdmin
? '/admin/alumnos'
: '/alumnos/cuenta'
: '/auth'
}
/>
Expand Down
16 changes: 8 additions & 8 deletions FrontAdmin/src/Context.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createContext, useState, ReactNode, useContext, useEffect } from 'react';
import Cookies from 'js-cookie';
import { FetchLogin } from './API/Login';
const URL= import.meta.env.VITE_URL_DEV;

interface AuthContextType {
isAuthenticated: boolean;
rolUser: boolean;
onLogin: () => void;
rolUser: String[];

Check failure on line 8 in FrontAdmin/src/Context.tsx

View workflow job for this annotation

GitHub Actions / Linter de Código

Don't use `String` as a type. Use string instead
onLogin: (password: string, account: string) => Promise<void>;
onLogout: () => void;
}

Expand All @@ -15,9 +16,7 @@ const AuthProvider = ({ children }: { children: ReactNode }) => {
const [isAuthenticated, setIsAuthenticated] = useState(
!Cookies.get('access_token') ? false : true
);
const [rolUser, setRolUser] = useState(
Cookies.get('username') === '12345678' ? true : false
);
const [rolUser, setRolUser] = useState<String[]>([]);

Check failure on line 19 in FrontAdmin/src/Context.tsx

View workflow job for this annotation

GitHub Actions / Linter de Código

Don't use `String` as a type. Use string instead
let refreshTimeout: NodeJS.Timeout;

const refreshToken = async () => {
Expand Down Expand Up @@ -64,14 +63,14 @@ const AuthProvider = ({ children }: { children: ReactNode }) => {
}
};

const onLogin = () => {
setRolUser(Cookies.get('username') === '12345678' ? true : false);
const onLogin = async (password: string, account: string) => {
await FetchLogin(password, account);
setRolUser(JSON.parse(localStorage.getItem('userRol') || '[]'));
setIsAuthenticated(true);
const accessExpiration = Cookies.get('access_expiration');
if (accessExpiration) {
TokenRefresh(accessExpiration);
}

};

const onLogout = () => {
Expand All @@ -81,6 +80,7 @@ const AuthProvider = ({ children }: { children: ReactNode }) => {
Cookies.remove('refresh_expiration');
Cookies.remove('username');
Cookies.remove('dni');
localStorage.removeItem('userRol');
console.log('logout');
setIsAuthenticated(false);
if (refreshTimeout) {
Expand Down
Loading

0 comments on commit 9fa8e5d

Please sign in to comment.