Commit 4a8a1a6c authored by Djordje's avatar Djordje

Add action handlers

parent f9fc9f48
......@@ -43,5 +43,6 @@
"last 1 firefox version",
"last 1 safari version"
]
}
},
"proxy": "http://localhost:3001"
}
This diff is collapsed.
export const ADD_BUILDING = "ADD_BUILDING";
export const CHANGE_NEW_BUILDING = "CHANGE_NEW_BUILDING"
export const CHANGE_NEW_UNIT_COUNT = "CHANGE_NEW_UNIT_COUNT"
export const CHANGE_NEW_UNIT = "CHANGE_NEW_UNIT"
export const CHANGE_OWNER = "CHANGE_OWNER"
export const CREATE_DOCUMENTS = "CREATE_DOCUMENTS";
export const CHANGE_SELECTED_BUILDING = "CHANGE_SELECTED_BUILDING"
export const GENERIC_STATE_CHANGE = "GENERIC_STATE_CHANGE"
export const FETCH_ALL_BUILDING_SUCCESS = "FETCH_ALL_BUILDING_SUCCESS"
export const TOKEN_RECEIVED = "TOKEN_RECEIVED"
export const TOKEN_RECEIVED_MANAGER = "TOKEN_RECEIVED_MANAGER"
export const TENANT_BUILDING_RECEIVED = "TENANT_BUILDING_RECEIVED"
export const TENANT_VOTING_RECEIVED = "TENANT_VOTING_RECEIVED"
export const UNAUTHENTICATED = "UNAUTHENTICATED"
//TBD:
// ChangeTenant/Owner
// SubmitIssue
// AddProposal
// Vote
export function genericAction(type, payload){
return {type, payload}
}
// export function tokenReceived(jwt, role){
// return genericAction(TOKEN_RECEIVED, {jwt, role})
// }
// export function
// export function tenantBuildingReceived(building){
// return genericAction(TENANT_BUILDING_RECEIVED, building)
// }
//TODO: this should be called when is already added
export function addBuilding(building) {
return {
type: ADD_BUILDING,
payload: building
}
}
export function changeNewBuilding(updateData) {
return {
type: CHANGE_NEW_BUILDING,
payload: updateData
}
}
export function changeNewUnitCount(count) {
return {
type: CHANGE_NEW_UNIT_COUNT,
payload: count
}
}
export function changeNewUnit(updateData) {
return {
type: CHANGE_NEW_UNIT,
payload: updateData
}
}
export function changeNewUnitOwner(updateData) {
return {
type: CHANGE_OWNER,
payload: updateData
}
}
export const fetchBuildingsSuccess = buildings => ({
type: FETCH_ALL_BUILDING_SUCCESS,
payload: { buildings }
});
//TODO: this should not be called by reducer
export function createDocuments(building) {
// console.log(building)
return {
type: CREATE_DOCUMENTS,
payload: building
}
}
export function changeSelectedBuilding(buildingId, selectedIndex) {
return {
type: CHANGE_SELECTED_BUILDING,
payload: { selectedBuilding: buildingId, selectedBuildingIndex: selectedIndex - 1 }
}
}
export function genericStateChange(key, value) {
return {
type: GENERIC_STATE_CHANGE,
payload: { key, value }
}
}
// console.log("process.env.REACT_APP_USE_DEV")
// console.log(process.env.REACT_APP_USE_DEV)
// let API_ADDRESS
// if (process.env.REACT_APP_USE_DEV) {
// // API_ADDRESS = API_ADDRESS = "http://localhost:3001"
// } else {
// API_ADDRESS = "/api"
// }
const API_ADDRESS = "/api"
export const JWT_TENANT = "jwtt"
export const JWT_MANAGER = "jwtm"
function fetchRequestBase(jwt, api_prefix, address, callback, dispatch, method = "GET", object = undefined, isDownload = false) {
let requestInit = {
method,
headers: {
// 'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': localStorage.getItem(jwt)
}
}
if (!isDownload && object) {
requestInit["body"] = JSON.stringify(object)
}
return fetch(API_ADDRESS + api_prefix + address, requestInit)
.then(res => {
//NOTE: cascade promise is used in order to get both res.body and res status in same processing phase.
let promise;
if (!isDownload) {
promise = res.json()
}
else {
promise = res.blob()
}
promise.then(json => {
//TODO: unify error handling
//TODO: verify status
if (!res.ok) {
if (typeof (json) === "string") {
json = { errorMessage: json }
}
console.log(json)
json.statusText = res.statusText
json.status = res.status
console.error(json)
// if (json.status === "error") {}
if (callback) { callback(json, null, dispatch) }
return
}
if (!isDownload) {
if (callback) { callback(null, json, dispatch) };
}
else {
// 1. Create blob link to download
const url = window.URL.createObjectURL(new Blob([json]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', object);
// 2. Append to html page
document.body.appendChild(link);
// 3. Force download
link.click();
// 4. Clean up and remove the link
link.parentNode.removeChild(link);
if (callback) { callback(null, json, dispatch) };
}
})
.catch(error => {
// dispatch(fetchProductsFailure(error))
// console.error("ERR fetch")
console.error(error)
if (callback) { callback(error, null, dispatch) }
});
})
.catch(error => {
// dispatch(fetchProductsFailure(error))
// console.error("ERR fetch")
console.error(error)
if (callback) { callback(error, null, dispatch) }
});
}
export function fetchRequestManager(address, callback, dispatch, method = "GET", object = undefined, isDownload = false) {
return fetchRequestBase(JWT_MANAGER, "/manager", address, callback, dispatch, method, object, isDownload)
}
export function fetchRequestTenant(...args) { return fetchRequestBase(JWT_TENANT, "/tenant", ...args) }
\ No newline at end of file
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