Commit af2e5a7f authored by Jiawen Lyu's avatar Jiawen Lyu

combine promises

parent 71fdecb0
......@@ -9,18 +9,28 @@ import withFetchData from "./HOC/withFetchData";
import withUVs from "./HOC/withUVs";
import { LoginInput } from "./components/LoginInput";
import { StudentsList } from "./components/StudentsList";
import LocalStorageManager from "./utils/LocalStorageManager"
const SchedulerWithData = withFetchData(withUVs(Scheduler))
class App extends Component {
state = {
currentTimeFormatState: true,
messages: [],
error: null,
isLoaded: false,
logins: ["lyujiawe", "jbarthel"],
uvs: []
};
constructor(props) {
super(props)
const storage = new LocalStorageManager()
storage.set("logins", ["lyujiawe", "jbarthel"])
let logins = storage.logins()
this.state = {
currentTimeFormatState: true,
messages: [],
error: null,
isLoaded: false,
logins: logins,
uvs: []
};
}
addMessage(message) {
const maxLogLength = 5;
const newMessage = { message };
......
......@@ -11,34 +11,34 @@ export default WrappedComponent => {
};
}
componentDidMount() {
this.props.logins.map(login => {
// alert(login)
fetch(`https://cors-anywhere.herokuapp.com/https://webapplis.utc.fr/Edt_ent_rest/myedt/result/?login=${login}`)
const fetchPromises = this.props.logins.map(login => {
return fetch(`https://cors-anywhere.herokuapp.com/https://webapplis.utc.fr/Edt_ent_rest/myedt/result/?login=${login}`)
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
data: this.state.data.concat(result.map(elem => ({
login: login,
...elem
}))),
});
},
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
})
Promise.all(fetchPromises).then(
(results) => {
// this.setState({
// isLoaded: true,
// data: this.state.data.concat(result.map(elem => ({
// login: login,
// ...elem
// }))),
// });
console.log("promise: ", results)
},
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
}
render() {
const { error, isLoaded, data, login } = this.state
console.log("withFetch: ", data)
if (error) {
return <div>Erreur : {error.message}</div>;
} else if (!isLoaded) {
......
......@@ -61,7 +61,7 @@ const getAllDays = (term, day) => {
days = [...days, currentDay]
}
let dayArr = currentDay.split("-")
if ((dayArr[2] == 30 && dayArr[1] % 2 == 0) || (dayArr[2] == 31 && dayArr[1] % 2 == 1)) {
if ((dayArr[2] === 30 && dayArr[1] % 2 === 0) || (dayArr[2] === 31 && dayArr[1] % 2 === 1)) {
dayArr[2] = 1
dayArr[1]++
} else {
......
......@@ -20,18 +20,10 @@ class LocalStorageManager {
return this.storage.removeItem(key)
}
loggedUser() {
return this.get('loggedUser')
logins() {
return this.get('logins')
}
userToken() {
return this.get('userToken')
}
registerDate() {
return this.get('registerDate')
}
}
export default LocalStorageManager
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