Commit fb81d2f6 authored by Jiawen Lyu's avatar Jiawen Lyu

find all days by week

parent bc118b45
...@@ -6,121 +6,174 @@ import MessageArea from "./components/MessageArea"; ...@@ -6,121 +6,174 @@ import MessageArea from "./components/MessageArea";
import "./App.css"; import "./App.css";
const data = [ const data = [
{ {
start_date: "2020-06-10 6:00", start_date: "2020-06-10 6:00",
end_date: "2020-06-10 8:00", end_date: "2020-06-10 8:00",
text: "C SR03", text: "C SR03",
id: 1, id: 1,
login: "jiawen" login: "jiawen"
}, },
{ {
start_date: "2020-06-10 6:00", start_date: "2020-06-10 6:00",
end_date: "2020-06-10 8:00", end_date: "2020-06-10 8:00",
text: "C SR03", text: "C SR03",
id: 4, id: 4,
login: "linh" login: "linh"
}, },
{ {
start_date: "2020-06-10 8:15", start_date: "2020-06-10 8:15",
end_date: "2020-06-10 10:15", end_date: "2020-06-10 10:15",
text: "TD SR03", text: "TD SR03",
id: 2, id: 2,
login: "jiawen" login: "jiawen"
}, },
{ {
start_date: "2020-06-13 10:00", start_date: "2020-06-13 10:00",
end_date: "2020-06-13 18:00", end_date: "2020-06-13 18:00",
text: "Event 2", text: "Event 2",
id: 3, id: 3,
login: "jiawen" login: "jiawen"
}, },
]; ];
const toDays = dayString => (
{
"LUNDI": 1,
"MARDI": 2,
"MERCREDI": 3,
"JEUDI": 4,
"VENDREDI": 5,
"SAMEDI": 6,
"DIMANCHE": 7,
}[dayString]
)
const semestreP20 = [
"2020-02-24",
"2020-06-27"
]
const semestreP20 = {
start_date: "2020-02-24", const getAllDays = (term, day) => {
end_date: "2020-06-27" let currentDay = term[0]
let days = []
while (currentDay !== term[1]) {
if (new Date(currentDay).getDay() === day) {
days = [...days, currentDay]
}
let dayArr = currentDay.split("-")
if ((dayArr[2] == 30 && dayArr[1] % 2 == 0) || (dayArr[2] == 31 && dayArr[1] % 2 == 1)) {
dayArr[2] = 1
dayArr[1]++
} else {
dayArr[2]++
}
currentDay = dayArr.map((elem, index) => {
if (index !== 0) {
return elem.toString().length === 1 ? "0" + elem : elem
} else return elem
}).join("-")
}
if (new Date(currentDay).getDay() === day) {
days = [...days, term[1]]
}
return days
} }
const extractUvDateInfo = uvs => uvs.map(uv => ({ const extractUvDateInfo = (uvs, login) => {
start_date: uv.begin, let uvInAllTerm = []
end_date: uv.end, uvs.map(uv => {
day: uv.day, const daysInTerm = getAllDays(semestreP20, toDays(uv.day))
text: uv.uv, daysInTerm.map(day => {
id: uvs.indexOf(uv) uvInAllTerm = [...uvInAllTerm,
})) {
start_date: day + " " + uv.begin,
end_date: day + " " + uv.end,
text: uv.uv,
id: uvInAllTerm.length + 1,
login: login
}
]
})
})
return uvInAllTerm
}
const getCurrentStudents = data => Array.from(new Set(data.map(elem => elem.login))) const getCurrentStudents = data => Array.from(new Set(data.map(elem => elem.login)))
class App extends Component { class App extends Component {
state = { state = {
currentTimeFormatState: true, currentTimeFormatState: true,
messages: [], messages: [],
error: null, error: null,
isLoaded: false, isLoaded: false,
uvs: [] uvs: []
}; };
addMessage(message) { addMessage(message) {
const maxLogLength = 5; const maxLogLength = 5;
const newMessage = { message }; const newMessage = { message };
const messages = [newMessage, ...this.state.messages]; const messages = [newMessage, ...this.state.messages];
if (messages.length > maxLogLength) { if (messages.length > maxLogLength) {
messages.length = maxLogLength; messages.length = maxLogLength;
} }
this.setState({ messages }); this.setState({ messages });
} }
logDataUpdate = (action, ev, id) => { logDataUpdate = (action, ev, id) => {
const text = ev && ev.text ? ` (${ev.text})` : ""; const text = ev && ev.text ? ` (${ev.text})` : "";
const message = `event ${action}: ${id} ${text}`; const message = `event ${action}: ${id} ${text}`;
this.addMessage(message); this.addMessage(message);
}; };
componentDidMount() { componentDidMount() {
fetch('https://cors-anywhere.herokuapp.com/https://webapplis.utc.fr/Edt_ent_rest/myedt/result/?login=jbarthel') fetch('https://cors-anywhere.herokuapp.com/https://webapplis.utc.fr/Edt_ent_rest/myedt/result/?login=jbarthel')
.then(res => res.json()) .then(res => res.json())
.then( .then(
(result) => { (result) => {
this.setState({ this.setState({
isLoaded: true, isLoaded: true,
uvs: result uvs: result,
}); login: "jbarthel"
}, });
// Remarque : il est important de traiter les erreurs ici },
// au lieu d'utiliser un bloc catch(), pour ne pas passer à la trappe // Remarque : il est important de traiter les erreurs ici
// des exceptions provenant de réels bugs du composant. // au lieu d'utiliser un bloc catch(), pour ne pas passer à la trappe
(error) => { // des exceptions provenant de réels bugs du composant.
this.setState({ (error) => {
isLoaded: true, this.setState({
error isLoaded: true,
}); error
} });
) }
} )
render() { }
const { currentTimeFormatState, messages, error, isLoaded, uvs } = this.state; render() {
// const data = extractUvDateInfo(uvs) const { currentTimeFormatState, messages, error, isLoaded, uvs, login } = this.state;
const currentStudents = getCurrentStudents(data) const data = extractUvDateInfo(uvs, login)
if (error) { const currentStudents = getCurrentStudents(data)
return <div>Erreur : {error.message}</div>; if (error) {
} else if (!isLoaded) { return <div>Erreur : {error.message}</div>;
return <div>Chargement</div>; } else if (!isLoaded) {
} else { return <div>Chargement</div>;
return ( } else {
<div> return (
<div className="scheduler-container"> <div>
<Scheduler <div className="scheduler-container">
events={data} <Scheduler
currentStudents={currentStudents} events={data}
timeFormatState={currentTimeFormatState} currentStudents={currentStudents}
onDataUpdated={this.logDataUpdate} timeFormatState={currentTimeFormatState}
/> onDataUpdated={this.logDataUpdate}
</div> />
<MessageArea messages={messages} /> </div>
</div > <MessageArea messages={messages} />
); </div >
} );
} }
}
} }
export default App; export default App;
Markdown is supported
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