Commit 9e8df20a authored by Anael Lacour's avatar Anael Lacour
Browse files

parse data of UTC api

parent 8dc90bb1
/* eslint-disable array-callback-return */
const axios = require('axios');
const url = "https://webapplis.utc.fr/Edt_ent_rest/myedt/result/?login=";
export async function loadByLogin(login) {
const getUV = await axios.get(`${url + login}`)
.then(({ data }) => data);
return getUV;
}
export async function loadUVlogin(uv, login) {
const edt = await loadByLogin(login);
const tabofUV = edt.filter(element => {
if (element.uv === uv.toUpperCase()){
return element;
}
});
return tabofUV;
}
export async function loadUserUVs(login) {
const edt = await loadByLogin(login);
let res = [];
for (let i = 0; i < edt.length; i++) {
if (!res.includes(edt[i].uv)) res.push(edt[i].uv);
}
return res;
}
const dayMapper = {
'LUNDI' : 1,
'MARDI' : 2,
'MERCREDI' : 3,
'JEUDI' : 4,
'VENDREDI' : 5,
'SAMEDI' : 6,
'DIMANCHE' : 7,
}
function parseDate(begin, end, day) {
let sep1 = begin.split(':');
let sep2 = end.split(':');
let today = new Date();
let d1 = new Date(today.getFullYear(), today.getMonth(), dayMapper[day], parseInt(sep1[0]), parseInt(sep1[1]));
let d2 = new Date(today.getFullYear(), today.getMonth(), dayMapper[day], parseInt(sep2[0]), parseInt(sep2[1]));
return {
begin: d1,
end: d2,
}
}
function FrequencyOfEvent(type){
if (type === 'TP') return 'FREQ=WEEKLY;INTERVAL=2';
else return 'FREQ=WEEKLY'
}
function UTCtoSchedItem({ uv, begin, end, type , day , room , group } = {}, id) {
let dates = parseDate(begin, end, day);
return {
Id: id,
Subject: uv,
StartTime: dates.begin,
EndTime: dates.end,
RecurrenceRule: FrequencyOfEvent(type),
Location: room + ' - ' + type,
Description: group,
}
}
export function Mapper(data){
let res = []
data.forEach((utcObj, index) => {
res.push(UTCtoSchedItem(utcObj, index));
})
return res;
}
import React, { Component } from 'react';
import logo from './logo.svg';
/* eslint-disable react-hooks/exhaustive-deps */
import React, { useState, useEffect } from 'react';
import './App.css';
import axios from 'axios';
import Header from './components/Header';
import { Inject, ScheduleComponent, Day, Week, ViewsDirective, ViewDirective } from '@syncfusion/ej2-react-schedule';
import { loadByLogin, loadUserUVs, Mapper } from './api/edtutc';
class App extends Component {
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
function App() {
const [edt, setEDT] = useState([]);
const [uvs, setUvs] = useState([]);
const [searchLogins, setSearchLogins] = useState(['lacouran']);
useEffect(() => {
console.log('In use effect');
searchLogins.forEach((login) => {
console.log('treating login : ' + login);
loadByLogin(login).then((data) => setEDT(edt.concat(Mapper(data)))).catch(err => console.log(err));
loadUserUVs(login).then((data) => setUvs(uvs.concat(data))).catch(err => console.log(err));
})
}, [searchLogins])
function handleSearch(input) {
if (input === '') return;
setSearchLogins([...searchLogins, input]);
}
function removeFromArray(arrOriginal, elementToRemove) {
return arrOriginal.filter((el) => {return el !== elementToRemove})
}
function handleDelete(input){
console.log('login to delete : ' + input);
let newArray = removeFromArray(searchLogins, input);
setSearchLogins(newArray);
setEDT([]);
setUvs([]);
}
return (
<div className="App">
<Header onSearch={handleSearch} onDelete={handleDelete} title='Emploi du temps' />
<p style={{ fontWeight: 'bold' }}>Vos UVs : {uvs.join(", ")}</p>
<p style={{ fontWeight: 'bold' }}>Vos Logins : {searchLogins.join(", ")}</p>
<ScheduleComponent
eventSettings={{ dataSource: edt }}
timeScale={{ interval: 60 }}
showWeekNumber={true}
startHour='7:00'
endHour='21:00'
height='600px'
width='900px'
>
<ViewsDirective>
<ViewDirective option='Day' />
<ViewDirective option='Week' />
</ViewsDirective>
<Inject services={[Day, Week]} />
</ScheduleComponent>
</div>
);
}
export default App;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment