-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
161 lines (100 loc) · 3.35 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import axios from 'axios';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import { ToastContainer, toast } from 'react-toastify';
import * as actions from '../actions/index.js';
var request_logs = {};
/**
* Custom ajax handler to make things easier.
*/
export function ajax(opts,callback, url = '/ajax', request = 'post') {
const config = { headers: { 'Content-Type': 'application/json' } };
var r = axios[request];
if( request === 'get' ) {
opts = { params:opts };
}
r(url,opts,config )
.catch(error => {
return;
if (url !== '/v1/api/analytics' && window.FANDANGO_RT!=="undefined" && ! toast.isActive(window.FANDANGO_RT)) {
request_logs[url+"-"+request] = {opts,callback,url,request};
toast.dismiss();
window.FANDANGO_RT = toast.info("Connection with server lost. Attempting to reconnecting...", {
position: toast.POSITION.TOP_CENTER,
autoClose : 6000
});
/**
* Attempt to reconnect again in every 6 seconds...
*/
setTimeout(function(){
Object.keys(request_logs).map(k => {
var a = request_logs[k];
ajax(a.opts,a.callback,a.url,a.request);
});
},6000)
}
})
.then(function (response) {
if(typeof response === "undefined") {
return;
}
if(typeof request_logs[url+"-"+request] !== "undefined")
delete request_logs[url+"-"+request];
callback(response.data,response.status);
});
};
/**
* Custom ajax handler to make things easier.
*/
export function ajaxMultiparts(url,form,callback, request = 'post') {
var tempFormData = new FormData(form);
const config = { headers: {'Content-Type': 'multipart/form-data' }};
var r = axios[request];
r(url,tempFormData,config )
.then(function (response) {
callback(response.data,response.status);
});
};
/**
* Get all inputs from form
*/
export function formHandler(form){
var data = {};
var inputs = form.querySelectorAll('input,textarea,select');
for(var i=0;i<inputs.length;i++) {
if(inputs[i].value!=='')
data[inputs[i].name] = inputs[i].value.trim();
}
return data;
}
/**
* React - Redux Bridge
*/
export function rBridge(component) {
function mapStateToProps(state) {
return { user : state.user , guest : state.guest , userLoggedIn : state.userLoggedIn, booking : state.booking };
}
function mapDispatchToProps(dispatch) {
return bindActionCreators(actions,dispatch);
}
return connect(mapStateToProps,mapDispatchToProps)(component);
}
/**
* Get movie from hall
*/
export function getMovie(moviehall,id) {
var movie = false;
// console.log(moviehall);
for(var i=0;i<moviehall.movies.length;i++) {
// console.log(moviehall.movies[i].movie_id._id,id);
if(moviehall.movies[i].movie_id._id === id) {
movie = moviehall.movies[i];
return movie; // this is a movie object with time and screen things
}
}
return movie;
}
export function logAnalytics() {
ajax({ ping:window.Fandango_Analytics},function(response){
},"/v1/api/analytics");
}