132 lines
3.1 KiB
JavaScript
132 lines
3.1 KiB
JavaScript
import axios from 'axios';
|
|
import { Cookies } from 'react-cookie';
|
|
import Router from 'next/router';
|
|
import moment from 'moment';
|
|
import { nSQL } from "@nano-sql/core";
|
|
|
|
import * as actionTypes from '../constants/actionTypes';
|
|
|
|
if(process.browser)
|
|
{
|
|
FormData.prototype.appendObject = function(obj, namespace)
|
|
{
|
|
let keyName;
|
|
for (var key in obj)
|
|
{
|
|
if (obj.hasOwnProperty(key))
|
|
{
|
|
keyName = [namespace, '[', key, ']'].join('');
|
|
this.append(keyName, obj[key]);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
export const getEvents = ({ dispatch, type, contract }) =>
|
|
{
|
|
console.log("getEvents", `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/events`);
|
|
|
|
return new Promise((resolve, reject) =>
|
|
{
|
|
axios.post(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/events`, {}, {
|
|
withCredentials: true,
|
|
})
|
|
.then((response) =>
|
|
{
|
|
console.log("ACTION", "getEvents()", "response", response.data);
|
|
const events = response.data;
|
|
const filtered_events = [];
|
|
|
|
console.log("ACTION", "getEvents()", "events", events);
|
|
|
|
dispatch({ type: actionTypes.EVENTS, data: { list: events, loaded: true } });
|
|
resolve();
|
|
})
|
|
.catch((error) =>
|
|
{
|
|
console.log("error");
|
|
console.error(error);
|
|
|
|
reject();
|
|
});
|
|
});
|
|
}
|
|
|
|
export const getFilteredEvents = ({ dispatch, type, search, contract }) =>
|
|
{
|
|
return new Promise((resolve, reject) =>
|
|
{
|
|
console.log("getFilteredEvents");
|
|
|
|
const types = {
|
|
"restrictions": "restrictions",
|
|
"payments": [ "kasko_prolong", "fingap_prolong", "osago_prolong" ],
|
|
"additional": "graph_change",
|
|
"finance": "end_contract",
|
|
"fines": "fine_gibdd",
|
|
"pts": "return_pts",
|
|
};
|
|
|
|
if(type !== undefined || (search !== undefined || search !== null || search !== ""))
|
|
{
|
|
console.log(global.store.getState().events);
|
|
const events = global.store.getState().events.list;
|
|
|
|
let query = nSQL(events)
|
|
.query("select");
|
|
|
|
if(type !== undefined)
|
|
{
|
|
if(typeof types[ type ] === "string")
|
|
{
|
|
query.where([ [ "event_type", "=", types[ type ] ] ])
|
|
}
|
|
else
|
|
{
|
|
let array_of_queries = [];
|
|
for(let i in types[ type ])
|
|
{
|
|
array_of_queries.push([ "event_type", "=", types[ type ][ i ] ]);
|
|
if(parseInt(i, 10) < parseInt(types[ type ].length - 1))
|
|
{
|
|
console.log(parseInt(i, 10), "<", types[ type ].length - 1);
|
|
array_of_queries.push("OR");
|
|
}
|
|
}
|
|
console.log("array_of_queries", array_of_queries);
|
|
query.where(array_of_queries);
|
|
}
|
|
}
|
|
|
|
if(contract !== undefined && contract !== null && contract !== "")
|
|
{
|
|
query.where([ [ "contract_number", "=", contract ] ]);
|
|
}
|
|
|
|
if(search !== undefined && search !== null && search !== "")
|
|
{
|
|
query.where([ [ "contract_number", "LIKE", `%${ search }%` ], "OR", [ "add_info", "LIKE", `%${ search }%` ]]);
|
|
}
|
|
|
|
query.exec().then((rows) =>
|
|
{
|
|
console.log("rows");
|
|
console.log(rows);
|
|
dispatch({ type: actionTypes.EVENTS_FILTERED, data: { filtered: rows } });
|
|
resolve();
|
|
|
|
//callback(null, rows);
|
|
});
|
|
|
|
/*
|
|
.orderBy("number", "asc")
|
|
*/
|
|
}
|
|
else
|
|
{
|
|
dispatch({ type: actionTypes.EVENTS_FILTERED, data: { filtered: undefined } });
|
|
resolve();
|
|
}
|
|
});
|
|
}
|