ENEI2019-Public/App/app/store/actions/api.js

1099 lines
27 KiB
JavaScript
Raw Normal View History

2019-03-10 00:00:54 +00:00
import { AsyncStorage } from "react-native";
2019-03-12 03:34:40 +00:00
import { NetInfo, Alert } from "react-native";
2019-03-10 00:00:54 +00:00
var _ = require("lodash");
import {
TIMERWAIT_CHANGE,
SESSION_BLOCKS,
DATA_AVAILABLE,
API_LOGIN,
CHECK_USER,
LOGOUT_USER,
USER_INFO,
HOLD,
GET_EVENTS,
GET_CAREERS,
GET_SESSIONS,
CHANGE_GUEST,
WAIT_CHANGE,
SIGN_SESSION,
2019-03-11 16:15:51 +00:00
OPEN_MODAL,
2019-03-12 03:34:40 +00:00
LOADINGLOGIN,
2019-03-12 07:31:41 +00:00
REMOVE_SESSION,
2019-03-13 20:42:20 +00:00
UPDATE_USER
2019-03-10 00:00:54 +00:00
} from "./actionTypes"; //Import the actions types constant we defined in our actions
import moment from "moment";
import { compose } from "redux";
2019-03-08 18:24:01 +00:00
import tap from "lodash/fp/tap";
import flow from "lodash/fp/flow";
import groupBy from "lodash/fp/groupBy";
2019-03-10 18:28:14 +00:00
import { colors } from "react-native-elements";
2019-03-08 18:24:01 +00:00
2019-03-10 00:00:54 +00:00
const axios = require("axios");
2019-02-28 23:52:58 +00:00
2019-03-15 22:07:22 +00:00
axios.defaults.baseURL = "https://tickets.enei.pt/internal/api";
2019-03-04 23:57:08 +00:00
2019-03-10 00:00:54 +00:00
const map = require("lodash/fp/map").convert({ cap: false });
2019-03-04 23:57:08 +00:00
2019-03-12 03:34:40 +00:00
export function waitLogin() {
return dispatch => {
2019-03-11 16:15:51 +00:00
dispatch({
2019-03-12 03:34:40 +00:00
type: LOADINGLOGIN
2019-03-11 16:15:51 +00:00
});
2019-03-12 03:34:40 +00:00
};
2019-03-11 16:15:51 +00:00
}
2019-03-15 22:07:22 +00:00
var checkAndRefresh = function(token) {
return new Promise(function(resolve, reject) {
//verificar se já expirou a validade do token
if (token == undefined || token.access_token == undefined) {
reject("user logged out");
console.log("fds");
} else if (
Math.round(new Date().getTime() / 1000) >= token.expirationDateToken
) {
console.log("vai renovar");
var obj;
2019-03-12 07:31:41 +00:00
2019-03-15 22:07:22 +00:00
var details = {
refresh_token: token.refresh_token,
grant_type: "refresh_token"
};
var formBody = [];
for (var property in details) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(details[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
fetch("https://tickets.enei.pt/internal/api/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
},
body: formBody
})
.catch(err => {
console.log(err);
alert("Erro no login!!");
alert("error");
reject("ERRRO");
2019-03-12 07:31:41 +00:00
})
2019-03-15 22:07:22 +00:00
.then(res => res.json())
.then(parsed => {
if (parsed.error == "invalid_grant") {
reject("erro");
} else {
var obj = {
access_token: parsed.access_token,
refresh_token: parsed.refresh_token,
expirationDateToken:
Math.round(new Date().getTime() / 1000) + 3598
};
resolve(obj);
console.log(parsed);
}
2019-03-12 07:31:41 +00:00
});
2019-03-15 22:07:22 +00:00
} else {
console.log(
"Tempo restante token: " +
Math.round(
(token.expirationDateToken -
Math.round(new Date().getTime() / 1000)) /
60
) +
" Minutos"
);
resolve(token);
}
});
};
export function changePassword(token, old, new1, new2) {
return dispatch => {
axios.defaults.baseURL = "https://tickets.enei.pt/internal/api";
checkAndRefresh(token)
.then(newToken => {
axios.defaults.headers.common = {
Authorization: `bearer ${newToken.access_token}`
};
if (new1 != new2) {
Alert.alert("ERRO!", "As passords são diferentes...");
} else {
axios
.post("/User/ChangePassword", {
OldPassword: old,
NewPassword: new1
})
.then(a => {
Alert.alert("Sucesso!", "Password alterada com sucesso");
})
.catch(p => {
Alert.alert(
"ERRO!",
"Erro a alterar a password.\nA palavra original é inválida...\nCaso o erro persista verifica a tua conexão à internet e tenta novamente"
);
});
2019-03-12 07:31:41 +00:00
2019-03-15 22:07:22 +00:00
dispatch({
type: OPEN_MODAL
});
}
})
.catch(err => {
Alert.alert(
"Token ERROR!",
"Parace que houve um erro com o teu token... Reinicia a App. Caso o problema se mantenha, volta e instalar"
);
2019-03-12 07:31:41 +00:00
});
};
}
2019-03-12 03:34:40 +00:00
//faz autenticação com API interna
export function loginInternal(userDetails) {
2019-03-11 16:15:51 +00:00
axios.defaults.baseURL = "http://127.0.0.1:5000";
return dispatch => {
2019-03-12 03:34:40 +00:00
axios
.post("/api/login", {
username: "cena",
password: "password"
})
.then(a => {
console.log("sucesso!");
console.log(a);
})
.catch(p => {
console.log(p);
});
2019-03-11 16:15:51 +00:00
dispatch({
2019-03-12 03:34:40 +00:00
type: OPEN_MODAL
2019-03-11 16:15:51 +00:00
});
};
}
2019-03-10 00:00:54 +00:00
export function openModal(info, t) {
return dispatch => {
dispatch({
type: OPEN_MODAL,
modalInfo: info,
modalType: t
2019-03-04 23:57:08 +00:00
});
2019-03-10 00:00:54 +00:00
};
2019-03-04 23:57:08 +00:00
}
2019-03-10 00:00:54 +00:00
export function closeModal() {
return dispatch => {
dispatch({
type: OPEN_MODAL,
modalInfo: info
});
};
2019-03-04 23:57:08 +00:00
}
2019-03-10 00:00:54 +00:00
export function updateUser(token, user) {
2019-03-15 22:07:22 +00:00
axios.defaults.baseURL = "https://tickets.enei.pt/internal/api";
2019-02-28 23:52:58 +00:00
2019-03-10 00:00:54 +00:00
return dispatch => {
2019-03-15 22:07:22 +00:00
checkAndRefresh(token)
.then(newToken => {
axios.defaults.headers.common = {
Authorization: `bearer ${newToken.access_token}`
};
axios
.post("/Attendee/Edit", user)
.then(a => {
Alert.alert(
"Sucesso",
"As informações pessoais foram guardadas com sucesso."
);
console.log(a.data);
dispatch({
type: UPDATE_USER,
user: a.data
});
})
.catch(b => {
Alert.alert(
"ERRO!",
"Ocorreu um erro a guardar os dados pessoais."
);
alert(b);
});
2019-03-10 00:00:54 +00:00
})
2019-03-15 22:07:22 +00:00
.catch(err => {
Alert.alert(
"Token ERROR!",
"Parace que houve um erro com o teu token... Reinicia a App. Caso o problema se mantenha, volta e instalar"
);
2019-03-10 00:00:54 +00:00
});
};
2019-03-01 19:35:36 +00:00
}
2019-03-10 18:28:14 +00:00
function getCareerPath(sessions) {
careerPath = "Sem Career Path";
careerColor = "white";
for (let key in sessions) {
if (sessions[key].Name == "IA") {
careerPath = "Artificial Inteligence";
2019-03-12 03:34:40 +00:00
careerColor = "#CC1A17";
2019-03-10 18:28:14 +00:00
}
if (sessions[key].Name == "IOT") {
careerPath = "Internet of Things";
2019-03-12 03:34:40 +00:00
careerColor = "green";
2019-03-10 18:28:14 +00:00
}
if (sessions[key].Name == "WEB") {
careerPath = "Web Development";
2019-03-12 03:34:40 +00:00
careerColor = "purple";
2019-03-10 18:28:14 +00:00
}
if (sessions[key].Name == "NET") {
careerPath = "Networking and Security";
2019-03-12 03:34:40 +00:00
careerColor = "blue";
2019-03-10 18:28:14 +00:00
}
if (sessions[key].Name == "MOB") {
careerPath = "Mobile Development";
2019-03-12 03:34:40 +00:00
careerColor = "orange";
2019-03-10 18:28:14 +00:00
}
2019-03-12 03:34:40 +00:00
if (sessions[key].Name == "DS") {
careerPath = "Data Science";
careerColor = "yellow";
2019-03-10 18:28:14 +00:00
}
}
return { name: careerPath, color: careerColor };
}
2019-03-10 00:00:54 +00:00
export const waitChangeGuest = () => {
return dispatch => {
dispatch({
type: WAIT_CHANGE
});
2019-02-22 23:38:34 +00:00
};
2019-03-10 00:00:54 +00:00
};
export const timerChangeGuest = () => {
return dispatch => {
dispatch({
type: TIMERWAIT_CHANGE
});
};
};
2019-02-22 23:38:34 +00:00
2019-03-10 00:00:54 +00:00
export const connectionState = status => {
console.log(status);
return { type: "CHANGE_CONNECTION_STATUS", isConnected: status };
};
2019-02-22 23:38:34 +00:00
2019-03-12 07:31:41 +00:00
export function removeSession(user, token, idSession) {
2019-02-22 23:38:34 +00:00
2019-03-15 22:07:22 +00:00
var obj = {
2019-03-10 00:00:54 +00:00
IdSession: idSession,
Direction: 0
};
2019-02-28 23:52:58 +00:00
2019-03-10 00:00:54 +00:00
return dispatch => {
//adiciona participante a uma palestra
2019-03-15 22:07:22 +00:00
checkAndRefresh(token)
.then(newToken => {
axios.defaults.headers.common = {
Authorization: `bearer ${newToken.access_token}`
};
2019-03-10 00:00:54 +00:00
axios
2019-03-12 03:34:40 +00:00
.post("/Session/RemoveAttendee", obj)
2019-03-10 00:00:54 +00:00
//se não existir erro na chamada...
.then(a => {
2019-03-12 07:31:41 +00:00
console.log(a);
2019-03-10 18:28:14 +00:00
if (a.data.Success) {
2019-03-10 00:00:54 +00:00
axios
.get("/Attendee/AvailableSessions")
2019-02-28 23:52:58 +00:00
2019-03-10 00:00:54 +00:00
.then(function(response) {
console.log(response);
2019-02-28 23:52:58 +00:00
2019-03-10 00:00:54 +00:00
var sessions = response.data;
2019-03-10 18:28:14 +00:00
var careerPath = getCareerPath(sessions);
2019-03-10 00:00:54 +00:00
var cenas = [];
2019-02-28 23:52:58 +00:00
2019-03-10 00:00:54 +00:00
const result = flow(groupBy("SessionStart"))(response.data);
2019-03-08 18:24:01 +00:00
2019-03-10 00:00:54 +00:00
for (let key in result) {
result[key].option = 0;
2019-03-08 18:24:01 +00:00
cenas.push(result[key]);
}
2019-03-12 07:31:41 +00:00
Alert.alert(
"Sucesso",
"A inscrição na sessão foi removida com sucesso!"
);
2019-03-08 18:24:01 +00:00
2019-03-10 18:28:14 +00:00
//obter informações pessoais:
axios
.get("/Attendee/Detail")
.catch(error => {
alert(error);
})
.then(sucess => {
dispatch({
2019-03-12 03:34:40 +00:00
type: REMOVE_SESSION,
2019-03-10 18:28:14 +00:00
sessions: response.data,
Blocks: cenas,
changeGuestList: false,
careerPath: careerPath,
user: sucess.data
});
2019-03-12 03:34:40 +00:00
getEvents(user);
2019-03-10 18:28:14 +00:00
});
2019-03-10 00:00:54 +00:00
})
.catch(function(error) {
2019-03-10 18:28:14 +00:00
alert("Erro a obter sessões disponíveis!!");
2019-03-08 18:24:01 +00:00
2019-03-10 00:00:54 +00:00
console.log(error);
});
} else {
alert("Erro a inscrever na palestra");
2019-03-10 18:28:14 +00:00
dispatch({
2019-03-12 03:34:40 +00:00
type: REMOVE_SESSION,
2019-03-10 18:28:14 +00:00
waitChangeGuest: false
});
2019-03-10 00:00:54 +00:00
}
})
.catch(b => {
alert("Erro a inscrever na palestra");
});
2019-03-15 22:07:22 +00:00
})
.catch(err => {
Alert.alert(
"Token ERROR!",
"Parace que houve um erro com o teu token... Reinicia a App. Caso o problema se mantenha, volta e instalar"
);
});
2019-03-10 00:00:54 +00:00
};
}
2019-02-28 23:52:58 +00:00
2019-03-12 03:34:40 +00:00
//inscrição em palestra através de ID
2019-03-12 07:31:41 +00:00
export function signSession(user, token, idSession) {
2019-03-15 22:07:22 +00:00
2019-03-12 03:34:40 +00:00
var obj = {
IdSession: idSession,
Direction: 0
};
return dispatch => {
2019-03-15 22:07:22 +00:00
checkAndRefresh(token)
.then(newToken => {
axios.defaults.headers.common = {
Authorization: `bearer ${newToken.access_token}`
};
2019-03-12 03:34:40 +00:00
//adiciona participante a uma palestra
axios
.post("/Session/AddAttendee", obj)
//se não existir erro na chamada...
.then(a => {
if (a.data.Success) {
2019-03-12 22:52:12 +00:00
axios.defaults.headers.common = {
Authorization: `bearer ${token.access_token}`
};
2019-03-12 03:34:40 +00:00
axios
.get("/Attendee/AvailableSessions")
.then(function(response) {
console.log(response);
var sessions = response.data;
var careerPath = getCareerPath(sessions);
var cenas = [];
const result = flow(groupBy("SessionStart"))(response.data);
for (let key in result) {
result[key].option = 0;
cenas.push(result[key]);
}
2019-03-12 07:31:41 +00:00
Alert.alert(
"Sucesso",
"Inscrição na sessão efectuada com sucesso"
);
2019-03-15 22:07:22 +00:00
console.log("aqui1");
2019-03-12 03:34:40 +00:00
//obter informações pessoais:
axios
.get("/Attendee/Detail")
.catch(error => {
alert(error);
})
.then(sucess => {
2019-03-15 22:07:22 +00:00
console.log("aqui2");
2019-03-12 07:31:41 +00:00
var result = getE(user);
2019-03-12 03:34:40 +00:00
dispatch({
type: SIGN_SESSION,
sessions: response.data,
Blocks: cenas,
changeGuestList: false,
careerPath: careerPath,
2019-03-12 07:31:41 +00:00
user: sucess.data,
events: result.a,
day1: result.a,
day2: result.b,
day3: result.c,
day4: result.d
2019-03-12 03:34:40 +00:00
});
});
})
.catch(function(error) {
console.log(error);
});
} else {
2019-03-12 07:31:41 +00:00
Alert.alert("ERRO!!", a.data.Error);
2019-03-12 03:34:40 +00:00
dispatch({
type: SIGN_SESSION,
waitChangeGuest: false
});
}
})
.catch(b => {
2019-03-12 07:31:41 +00:00
// alert("Erro a inscrever na palestra");
2019-03-12 03:34:40 +00:00
});
2019-03-15 22:07:22 +00:00
})
.catch(err => {
Alert.alert(
"Token ERROR!",
"Parace que houve um erro com o teu token... Reinicia a App. Caso o problema se mantenha, volta e instalar"
);
});
2019-03-12 03:34:40 +00:00
};
}
2019-03-10 00:00:54 +00:00
export function getSessions(token) {
2019-03-15 22:07:22 +00:00
2019-03-08 18:24:01 +00:00
2019-03-10 00:00:54 +00:00
return dispatch => {
2019-03-15 22:07:22 +00:00
checkAndRefresh(token)
.then(newToken => {
axios.defaults.headers.common = {
Authorization: `bearer ${newToken.access_token}`
};
axios.defaults.baseURL = "https://tickets.enei.pt/internal/api";
2019-03-08 18:24:01 +00:00
2019-03-10 00:00:54 +00:00
axios.defaults.headers.common = {
Authorization: `bearer ${token.access_token}`
};
2019-03-08 18:24:01 +00:00
2019-03-10 00:00:54 +00:00
axios
.get("/Attendee/AvailableSessions")
.then(function(response) {
var sessions = response.data;
2019-03-10 18:28:14 +00:00
var careerPath = getCareerPath(sessions);
console.log(careerPath);
2019-03-10 00:00:54 +00:00
var cenas = [];
2019-03-10 18:28:14 +00:00
2019-03-10 00:00:54 +00:00
const result = flow(
groupBy("SessionStart")
// map((Id) => ({Id})),
//tap(console.log)
)(response.data);
for (let key in result) {
result[key].option = 0;
cenas.push(result[key]);
}
dispatch({
type: GET_SESSIONS,
sessions: response.data,
Blocks: cenas,
careerPath: careerPath
//guests: response.data
2019-02-28 23:52:58 +00:00
});
2019-03-10 00:00:54 +00:00
})
.catch(function(error) {
alert("Error a obter sessões disponíveis!!");
console.log(error);
});
2019-03-15 22:07:22 +00:00
})
.catch(err => {
Alert.alert(
"Token ERROR!",
"Parace que houve um erro com o teu token... Reinicia a App. Caso o problema se mantenha, volta e instalar"
);
});
2019-03-10 00:00:54 +00:00
};
2019-02-28 23:52:58 +00:00
}
2019-03-01 17:36:52 +00:00
2019-03-10 00:00:54 +00:00
export function getAvailableGuestlists(token) {
2019-03-15 22:07:22 +00:00
2019-03-01 17:36:52 +00:00
2019-03-10 00:00:54 +00:00
return dispatch => {
2019-03-15 22:07:22 +00:00
checkAndRefresh(token)
.then(newToken => {
axios.defaults.headers.common = {
Authorization: `bearer ${newToken.access_token}`
};
2019-03-10 00:00:54 +00:00
axios
.get("/Attendee/AvailableGuestlists")
.then(function(response) {
// handle success
console.log(response);
dispatch({
type: GET_CAREERS,
guests: response.data
});
})
.catch(function(error) {
// handle error
console.log(error);
})
.then(function() {
// always executed
2019-03-15 22:07:22 +00:00
});})
.catch(err => {
Alert.alert(
"Token ERROR!",
"Parace que houve um erro com o teu token... Reinicia a App. Caso o problema se mantenha, volta e instalar"
);
2019-03-10 00:00:54 +00:00
});
};
}
2019-03-01 17:36:52 +00:00
/*
8 - group
9 - IA
10 - NET
11 - MOB
12 - IOT
14 - WB
15 - DS
*/
2019-03-10 00:00:54 +00:00
export function changeGuestList(token, guestID) {
2019-03-15 22:07:22 +00:00
2019-03-08 18:24:01 +00:00
2019-03-15 22:07:22 +00:00
axios.defaults.baseURL = "https://tickets.enei.pt/internal/api";
2019-03-10 00:00:54 +00:00
return dispatch => {
2019-03-15 22:07:22 +00:00
checkAndRefresh(token)
.then(newToken => {
axios.defaults.headers.common = {
Authorization: `bearer ${newToken.access_token}`
};
2019-03-10 00:00:54 +00:00
var full = `/Attendee/ChangeGuestlist/${guestID}`;
2019-03-08 18:24:01 +00:00
2019-03-10 00:00:54 +00:00
axios
.get(full)
.then(function(response) {
2019-03-12 22:52:12 +00:00
2019-03-01 17:36:52 +00:00
2019-03-10 00:00:54 +00:00
axios
.get("/Attendee/AvailableSessions")
2019-03-01 17:36:52 +00:00
2019-03-10 00:00:54 +00:00
.then(function(response) {
2019-03-01 17:36:52 +00:00
// handle success
2019-03-15 22:07:22 +00:00
2019-03-10 00:00:54 +00:00
//console.log(response);
var cenas = [];
2019-03-12 03:34:40 +00:00
c = getCareerPath(response.data);
2019-03-08 18:24:01 +00:00
const result = flow(
2019-03-10 00:00:54 +00:00
groupBy("SessionStart")
// map((Id) => ({Id})),
//tap(console.log)
)(response.data);
2019-03-10 18:28:14 +00:00
2019-03-10 00:00:54 +00:00
for (let key in result) {
result[key].option = 0;
cenas.push(result[key]);
2019-03-08 18:24:01 +00:00
}
2019-03-15 22:07:22 +00:00
console.log("-.-.-..-.-.-.-.-.-.");
2019-03-10 00:00:54 +00:00
console.log(cenas);
2019-03-15 22:07:22 +00:00
console.log("-.-.-..-.-.-.-.-.-.");
2019-03-01 17:36:52 +00:00
dispatch({
2019-03-10 00:00:54 +00:00
type: CHANGE_GUEST,
sessions: response.data,
2019-03-10 18:28:14 +00:00
Blocks: cenas,
careerPath: c
2019-03-10 00:00:54 +00:00
//guests: response.data
});
})
.catch(function(error) {
2019-03-04 16:46:29 +00:00
alert("Error a obter sessões disponíveis!!");
2019-03-01 17:36:52 +00:00
console.log(error);
2019-03-10 00:00:54 +00:00
});
})
.catch(function(error) {
// handle error
console.log(response);
console.log(error);
alert("Erro a mudar de career path");
})
.then(function() {
// always executed
2019-03-15 22:07:22 +00:00
});}).catch(err => {
Alert.alert(
"Token ERROR!",
"Parace que houve um erro com o teu token... Reinicia a App. Caso o problema se mantenha, volta e instalar"
);
2019-03-10 00:00:54 +00:00
});
};
}
export function getSessionBlocks(sessions) {
var a;
var cenas = [];
const result = flow(
groupBy("SessionStart")
// map((Id) => ({Id})),
//tap(console.log)
)(sessions);
return dispatch => {
for (let key in result) {
cenas.push(result[key]);
console.log();
2019-03-01 17:36:52 +00:00
}
2019-03-10 00:00:54 +00:00
console.log(cenas);
2019-03-01 17:36:52 +00:00
2019-03-10 00:00:54 +00:00
dispatch({
type: SESSION_BLOCKS,
Blocks: cenas
});
};
2019-03-01 17:36:52 +00:00
}
2019-02-28 23:52:58 +00:00
2019-03-10 00:00:54 +00:00
export function getAvailableSessions(token) {
2019-02-28 23:52:58 +00:00
2019-03-15 22:07:22 +00:00
axios.defaults.baseURL = "https://tickets.enei.pt/internal/api";
2019-02-06 23:24:00 +00:00
2019-03-10 00:00:54 +00:00
return dispatch => {
2019-03-15 22:07:22 +00:00
checkAndRefresh(token)
.then(newToken => {
axios.defaults.headers.common = {
Authorization: `bearer ${newToken.access_token}`
};
2019-03-10 00:00:54 +00:00
axios
.get("/Attendee/AvailableSessions")
.then(function(response) {
// handle success
console.log(response);
var cenas = [];
const result = flow(
groupBy("SessionStart")
// map((Id) => ({Id})),
//tap(console.log)
)(response.data);
for (let key in result) {
cenas.push(result[key]);
console.log();
}
2019-02-06 23:24:00 +00:00
2019-03-10 00:00:54 +00:00
dispatch({
type: GET_SESSIONS,
sessions: response.data,
2019-02-08 18:56:38 +00:00
2019-03-10 00:00:54 +00:00
Blocks: cenas
});
2019-02-06 23:24:00 +00:00
})
2019-03-10 00:00:54 +00:00
.catch(function(error) {
alert("Error a obter sessões disponíveis!!");
console.log(error);
2019-03-15 22:07:22 +00:00
});})
.catch(err => {
Alert.alert(
"Token ERROR!",
"Parace que houve um erro com o teu token... Reinicia a App. Caso o problema se mantenha, volta e instalar"
);
2019-03-10 00:00:54 +00:00
});
};
}
2019-03-16 01:17:43 +00:00
//ESTA FUNÇÃO TEM MUITO CÓDIGO MAL FEITO...
2019-03-12 07:31:41 +00:00
function getE(user) {
2019-03-16 01:17:43 +00:00
2019-03-12 07:31:41 +00:00
var cenas = [];
let events = [];
2019-03-16 01:17:43 +00:00
var alimentacao=[];
var alojamento=[];
var acesso=[]
2019-03-12 07:31:41 +00:00
var i = 0;
2019-03-16 01:17:43 +00:00
console.log(user.Sessions)
2019-03-12 07:31:41 +00:00
for (let key in user.Sessions) {
2019-03-16 01:17:43 +00:00
//se forem sessões de bilhete, adiciona a outra lista
if(
user.Sessions[key].Id==1 || //dia 12 de abril
user.Sessions[key].Id==22 || //jantar 12 de abril
user.Sessions[key].Id==23 || //almoço e jantar 13 de abril
user.Sessions[key].Id==24 || //almoço e jantar 14 de abril
user.Sessions[key].Id==25 || //almoço 15 de abril
user.Sessions[key].Id==26 || //alojamento 12 de abril
user.Sessions[key].Id==29 || //alojamento 13 de abril
user.Sessions[key].Id==31 || //alojamento 14 de abril
user.Sessions[key].Id==32 || //dia 13 de abril
user.Sessions[key].Id==33 || //dia 14 de abril
user.Sessions[key].Id==34 || //dia 15 de abril
user.Sessions[key].Id==35 || //jantar dia 12 de abril
user.Sessions[key].Id==36 || //jantar dia 13 de abril
user.Sessions[key].Id==37 //jantar dia 14 de abril
){
// bilhete.push( user.Sessions[key])
if(user.Sessions[key].Id==1){
acesso.push("dia 12")
}
if(user.Sessions[key].Id==22)
alimentacao.push("dia 12")
if(user.Sessions[key].Id==23)
alimentacao.push("dia 13")
if(user.Sessions[key].Id==24)
alimentacao.push("dia 14")
if(user.Sessions[key].Id==25)
alimentacao.push("dia 15")
if(user.Sessions[key].Id==26)
alojamento.push("dia 12")
if(user.Sessions[key].Id==29)
alojamento.push("dia 13")
if(user.Sessions[key].Id==31)
alojamento.push("dia 14")
if(user.Sessions[key].Id==32)
acesso.push("dia 13")
if(user.Sessions[key].Id==33)
acesso.push("dia 14")
if(user.Sessions[key].Id==34)
acesso.push("dia 15")
}else{
2019-03-12 07:31:41 +00:00
events.push({
key: i++,
time: moment(user.Sessions[key].SessionStart).format("HH:mm"),
timeEnd: moment(user.Sessions[key].SessionEnd).format("HH:mm"),
//lineColor:'#009688',
imageUrl:
2019-03-16 01:17:43 +00:00
"https://tickets.enei.pt/adminpoint/Content/Images/Uploads/Sessions/"+user.Sessions[key].Image,
2019-03-12 07:31:41 +00:00
description: user.Sessions[key].Description,
name: user.Sessions[key].Name,
Enrolled: user.Sessions[key].Enrolled,
MaxAttendees: user.Sessions[key].MaxAttendees,
day: moment(user.Sessions[key].SessionStart).format("DD")
});
2019-03-16 01:17:43 +00:00
}}
2019-03-12 03:34:40 +00:00
2019-03-10 20:17:58 +00:00
2019-03-16 01:17:43 +00:00
const result = flow(groupBy("day"))(events);
2019-03-12 07:31:41 +00:00
var a = [],
2019-03-16 01:17:43 +00:00
b = [],
c = [],
d = [];
2019-03-12 07:31:41 +00:00
2019-03-16 01:17:43 +00:00
//MEU DEUS QUE É ISTO???
for (let key in result["12"]) {
2019-03-12 07:31:41 +00:00
a.push({
2019-03-16 01:17:43 +00:00
time: result["12"][key].time,
timeEnd: result["12"][key].timeEnd,
imageUrl:result["12"][key].imageUrl,
description: result["12"][key].description,
name:result["12"][key].name,
Enrolled: result["12"][key].Enrolled,
MaxAttendees: result["12"][key].MaxAttendees,
day: result["12"][key].day
2019-03-12 03:34:40 +00:00
});
2019-03-12 07:31:41 +00:00
}
2019-03-16 01:17:43 +00:00
for (let key in result["13"]) {
2019-03-12 07:31:41 +00:00
b.push({
2019-03-16 01:17:43 +00:00
time: result["13"][key].time,
timeEnd: result["13"][key].timeEnd,
imageUrl: result["13"][key].imageUrl,
description:result["13"][key].description,
name: result["13"][key].name,
Enrolled:result["13"][key].Enrolled,
MaxAttendees: result["13"][key].MaxAttendees,
day: result["13"][key].day
2019-03-12 03:34:40 +00:00
});
2019-03-12 07:31:41 +00:00
}
2019-03-16 01:17:43 +00:00
for (let key in result["14"]) {
2019-03-12 07:31:41 +00:00
c.push({
2019-03-16 01:17:43 +00:00
time: result["14"][key].time,
timeEnd: result["14"][key].timeEnd,
imageUrl: result["14"][key].imageUrl,
description: result["14"][key].description,
name:result["14"][key].name,
Enrolled: result["14"][key].Enrolled,
MaxAttendees: result["14"][key].MaxAttendees,
day: result["14"][key].day
2019-03-12 03:34:40 +00:00
});
2019-03-12 07:31:41 +00:00
}
2019-03-16 01:17:43 +00:00
for (let key in result["15"]) {
2019-03-12 07:31:41 +00:00
d.push({
2019-03-16 01:17:43 +00:00
time: result["15"][key].time,
timeEnd: result["15"][key].timeEnd,
imageUrl:result["15"][key].imageUrl,
description: result["15"][key].description,
name: result["15"][key].name,
Enrolled: result["15"][key].Enrolled,
MaxAttendees: result["15"][key].MaxAttendees,
day: result["15"][key].day
2019-03-12 03:34:40 +00:00
});
2019-03-12 07:31:41 +00:00
}
a = _.sortBy(a, function(o) {
return o.time;
});
b = _.sortBy(b, function(o) {
return o.time;
});
c = _.sortBy(c, function(o) {
return o.time;
});
d = _.sortBy(d, function(o) {
return o.time;
});
2019-03-16 01:17:43 +00:00
console.log(alimentacao)
return { a, b, c, d ,ab:alimentacao, acc: acesso, al:alojamento};
2019-03-12 07:31:41 +00:00
}
export function getEvents(user) {
var result = getE(user);
2019-03-16 01:17:43 +00:00
2019-03-12 07:31:41 +00:00
return dispatch => {
2019-02-28 23:52:58 +00:00
dispatch({
2019-03-10 00:00:54 +00:00
type: GET_EVENTS,
2019-03-12 07:31:41 +00:00
events: result.a,
day1: result.a,
day2: result.b,
day3: result.c,
2019-03-16 01:17:43 +00:00
day4: result.d,
alimentacao: result.ab,
acesso: result.acc,
alojamento: result.al
2019-02-28 23:52:58 +00:00
});
2019-03-10 00:00:54 +00:00
};
2019-02-06 23:24:00 +00:00
}
2019-03-10 00:00:54 +00:00
export function login(user, pass) {
return dispatch => {
console.log("user: " + user + " password: " + pass);
2019-02-03 20:54:27 +00:00
2019-03-10 00:00:54 +00:00
var details = {
username: user,
password: pass,
grant_type: "password"
};
2019-02-03 20:54:27 +00:00
2019-03-10 00:00:54 +00:00
var formBody = [];
2019-02-28 23:52:58 +00:00
2019-03-10 00:00:54 +00:00
for (var property in details) {
var encodedKey = encodeURIComponent(property);
2019-01-31 00:46:49 +00:00
2019-03-10 00:00:54 +00:00
var encodedValue = encodeURIComponent(details[property]);
2019-02-28 23:52:58 +00:00
2019-03-10 00:00:54 +00:00
formBody.push(encodedKey + "=" + encodedValue);
2019-01-31 00:46:49 +00:00
}
2019-03-10 00:00:54 +00:00
formBody = formBody.join("&");
2019-02-06 23:24:00 +00:00
2019-03-15 22:07:22 +00:00
fetch("https://tickets.enei.pt/internal/api/token", {
2019-03-10 00:00:54 +00:00
method: "POST",
2019-02-06 23:24:00 +00:00
2019-03-10 00:00:54 +00:00
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
},
2019-01-31 00:46:49 +00:00
2019-03-10 00:00:54 +00:00
body: formBody
})
.catch(err => {
console.log(err);
alert("Erro no login!!");
2019-01-31 00:46:49 +00:00
2019-03-10 00:00:54 +00:00
alert("error");
co;
dispatch({
type: API_LOGIN,
logged: false,
failedAttempt: true,
2019-03-11 16:15:51 +00:00
tokenData: "error",
2019-03-12 03:34:40 +00:00
user: { Name: "" }
2019-03-10 00:00:54 +00:00
});
})
2019-03-12 03:34:40 +00:00
.catch(err => {
console.log("error");
2019-03-11 16:15:51 +00:00
})
2019-03-10 00:00:54 +00:00
.then(res => res.json())
.then(parsed => {
2019-03-12 03:34:40 +00:00
console.log(parsed);
2019-03-10 00:00:54 +00:00
if (
parsed.error_description ==
"Provided username and password is incorrect"
) {
2019-03-12 03:34:40 +00:00
Alert.alert(
"Dados Inválidos",
"Podes sempre fazer reset da password para o email."
);
2019-03-10 00:00:54 +00:00
dispatch({
type: API_LOGIN,
logged: false,
failedAttempt: true,
token: obj,
2019-03-11 16:15:51 +00:00
user: { Name: "Henrique" },
2019-03-12 03:34:40 +00:00
userDetails: {},
waitLogin: false,
onHold: false
2019-03-10 00:00:54 +00:00
});
2019-03-11 16:15:51 +00:00
return;
2019-03-12 03:34:40 +00:00
} else {
var obj = {
access_token: parsed.access_token,
expirationDateToken: Math.round(new Date().getTime() / 1000) + 3598,
2019-03-15 22:07:22 +00:00
refresh_token: parsed.refresh_token,
2019-03-12 03:34:40 +00:00
valid: true
};
var details = {
username: user,
password: pass
};
dispatch({
type: API_LOGIN,
logged: true,
failedAttempt: false,
token: obj,
user: { Name: "Henrique" },
userDetails: details
});
2019-03-10 00:00:54 +00:00
}
});
};
}
export function hold() {
return dispatch => {
dispatch({
type: HOLD,
onHold: true
});
};
2019-02-03 20:54:27 +00:00
}
2019-03-10 00:00:54 +00:00
export function getUserInfo(token) {
return dispatch => {
//TODO: verificar validade do token
2019-03-15 22:07:22 +00:00
checkAndRefresh(token)
.then(newToken => {
console.log('Chamada "getUserInfo"');
2019-02-06 23:24:00 +00:00
2019-03-15 22:07:22 +00:00
var obj = {
method: "GET",
headers: {
Authorization: `Bearer ${newToken.access_token}`
}
};
2019-02-22 23:38:34 +00:00
2019-03-15 22:07:22 +00:00
fetch("https://tickets.enei.pt/internal/api/Attendee/Detail", obj)
.then(function(res) {
console.log(res);
let obj = JSON.parse(res._bodyText);
2019-02-03 20:54:27 +00:00
2019-03-15 22:07:22 +00:00
dispatch({
type: USER_INFO,
user: obj,
onHold: false,
logged: true,
token: newToken
});
})
.catch(function(res) {
console.log("erro");
// dispatch({ type: USER_INFO,onHold:false});
alert("Erro a obter a informação pessoal.");
});
2019-03-10 00:00:54 +00:00
})
2019-03-15 22:07:22 +00:00
.catch(err => {
console.log("cenas da vida");
2019-03-10 00:00:54 +00:00
});
};
2019-01-31 00:46:49 +00:00
}
2019-03-10 00:00:54 +00:00
export function logoutUser() {
return dispatch => {
2019-03-15 22:07:22 +00:00
console.log("token apagado");
dispatch({
type: LOGOUT_USER,
loggedIn: false,
tokenData: "error",
token: false
2019-03-10 00:00:54 +00:00
});
};
2019-03-15 22:07:22 +00:00
}
2019-02-27 11:22:43 +00:00
2019-03-15 22:07:22 +00:00
export function checkUser(token) {
2019-03-10 00:00:54 +00:00
return dispatch => {
2019-03-15 22:07:22 +00:00
checkAndRefresh(token)
.catch(err => {
Alert("ERRO a fazer login");
console.log(err);
dispatch({
type: CHECK_USER,
logged: true,
onHold: false,
userDetails: u,
token: obj
});
})
.then(newToken => {
2019-03-10 00:00:54 +00:00
dispatch({
type: CHECK_USER,
logged: true,
onHold: false,
user: { Name: "Henrique" },
2019-03-15 22:07:22 +00:00
token: newToken
2019-03-10 00:00:54 +00:00
});
});
};
2019-03-15 22:07:22 +00:00
}