141 lines
3.8 KiB

import moment from "moment";
import React from "react";
import { View, Text } from "react-native";
import assets from "../config/assets";
const custom_assets = {
pluviometer: assets.pluviometer,
officialPluviometer: assets.officialPluviometer,
floodZones: assets.floodZones,
riverLevel: ["low", "normal", "high", "flooding"].map((key) => {
return assets.riverLevel[key];
}),
rainLevel: ["rain_0_5", "rain_1_5", "rain_2_5", "rain_3_5"].map((key) => {
return assets.rainLevel[key];
}),
};
function getLogo(name, situation) {
if (name == "automaticPluviometer") {
return custom_assets.officialPluviometer;
}
if (name == "pluviometer") {
return custom_assets.pluviometer;
}
if (name == "rain") {
if (situation == "SEM CHUVA") {
return custom_assets.rainLevel[0];
} else if (situation == "CHUVA FRACA") {
return custom_assets.rainLevel[1];
} else if (situation == "CHUVA MODERADA") {
return custom_assets.rainLevel[2];
} else if (situation == "CHUVA FORTE") {
return custom_assets.rainLevel[3];
} else {
return custom_assets.rainLevel[3];
}
}
if (name == "riverFlood") {
if (situation == "BAIXO") {
return custom_assets.riverLevel[0];
} else if (situation == "NORMAL") {
return custom_assets.riverLevel[1];
} else if (situation == "ALTO") {
return custom_assets.riverLevel[2];
} else if (situation == "INUNDAR") {
return custom_assets.riverLevel[3];
}
return custom_assets.riverLevel[0];
}
if (name == "floodZones") {
return custom_assets.floodZones;
}
}
function verifyMeasureDates(labels, measureDate) {
if (labels == null || !labels.find((f) => f == measureDate)) {
labels.push(measureDate);
}
}
const AssembleModalObject = (response, name) => {
var situation = null;
var user = null;
var institution = null;
var address = null;
var date = null;
var time = null;
var comments = null;
var pictures = null;
var data = {
values: [],
labels: [],
};
if (JSON.parse(response).array_to_json) {
const r = JSON.parse(response).array_to_json;
console.log(response);
//(waiting for issue #16) user = r[0].formsanswersuserinformer;
if (name == "automaticPluviometer") {
(situation = "PLUVIÔMETRO"),
r[0].array_to_json.forEach(function (f) {
if (f.fieldname == "medicao") {
verifyMeasureDates(
data.labels,
moment(f.fieldsanswersdtfilling).format("DD/MM")
); //data.labels.push(moment(f.fieldsanswersdtfilling).format("DD/MM/YYYY"));
data.values.push(
f.fieldsanswersvalue ? parseFloat(f.fieldsanswersvalue) : null
);
}
if (f.fieldname == "siglarede") {
institution = f.fieldsanswersvalue;
}
if (f.fieldname == "nome") {
address = f.fieldsanswersvalue;
}
});
} else {
if (r[0].array_to_json) {
user = r[0].formsanswersuserinformer;
r[0].array_to_json.forEach(function (f) {
if (f.fieldname == "situation") {
situation = f.fieldsanswersvalue;
}
if (f.fieldname == "eventaddress") {
address = f.fieldsanswersvalue;
}
if (f.fieldname == "eventdate") {
date = f.fieldsanswersvalue;
}
if (f.fieldname == "eventtime") {
time = f.fieldsanswersvalue;
}
if (f.fieldname == "comments") {
comments = f.fieldsanswersvalue;
}
});
}
}
}
return {
name: name,
title: situation,
user: user,
institution: institution,
address: address,
date: date + " | " + time,
description: comments,
logo: getLogo(name, situation),
pictures: pictures,
data: data,
};
};
export default AssembleModalObject;