import moment from "moment"; import React from "react"; import { View, Text } from "react-native"; import getFieldsAnswers from "../api/RequestFieldsAnswers/getFieldsAnswers"; import assets from "../config/assets"; //---1/3: melhorar código const custom_assets = { pluviometer: assets.PluviometerIcon, 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") { if (situation == "TRANSITÁVEL") { return custom_assets.floodZones.passable; } else { return custom_assets.floodZones.notPassable; } } } function verifymeasureDates(labels, measureDate, day) { // console.log(measureDate); if (labels == null || !labels.find((f) => f == measureDate)) { labels.push(measureDate); day.label = measureDate; return true; } return false; } const getCurrentPluviometer = (pluvForm, userId, data, datapluv) => { //console.log("\nuserid: "+ userId + "\n\n"); //console.log(pluvForm); var day = { label: null, values: [] }; var measureDate = null; if (pluvForm.formsanswersuserinformer == userId) { pluvForm.array_to_json.find((field) => { if (field.fieldname == "date") { measureDate = moment(field.fieldsanswersvalue, "DD-MM-YYYY").format( "DD/MM" ); //data.labels.push(moment(f.fieldsanswersdtfilling).format("DD/MM/YYYY")); } }); pluvForm.array_to_json.find((field) => { if (field.fieldname == "rainamount") { if (verifymeasureDates(data.labels, measureDate, day)) { day.values.push( field.fieldsanswersvalue ? parseFloat(field.fieldsanswersvalue) : null ); datapluv.push(day); } else { datapluv.forEach((d) => { if (d.label === measureDate) { d.values.push( field.fieldsanswersvalue ? parseFloat(field.fieldsanswersvalue) : null ); } }); } } }); return pluvForm; } }; async function getPluviometerStatio_informations(id) { const result = await getFieldsAnswers.fieldsAnswers(null, id); if (result.data) { return result.data.responseData.array_to_json[0].array_to_json; } return null; } const AssembleModalObject = async (response, name, userId, formId) => { 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 = { labels: [], }; var datapluv = []; if (JSON.parse(response).array_to_json) { const r = JSON.parse(response).array_to_json; //(waiting for issue #16) user = r[0].formsanswersuserinformer; if (name == "automaticPluviometer") { (situation = "PLUVIÔMETRO"), r[0].array_to_json.forEach(function (f) { var day = { label: null, values: [] }; if (f.fieldname == "medicao") { var measureDate = moment( f.fieldsanswersdtfilling, "YYYY-MM-DDTHH:mm:ss" ).format("DD/MM"); if (verifymeasureDates(data.labels, measureDate, day)) { day.values.push( f.fieldsanswersvalue ? parseFloat(f.fieldsanswersvalue) : null ); day.label = measureDate; datapluv.push(day); } else { datapluv.forEach((d) => { if (d.label == measureDate) { d.values.push( f.fieldsanswersvalue ? parseFloat(f.fieldsanswersvalue) : null ); } }); } } if (f.fieldname == "siglarede") { institution = f.fieldsanswersvalue; } if (f.fieldname == "nome") { address = f.fieldsanswersvalue; } }); if (datapluv) { datapluv.sort(function (dataPluv, dataPluvB) { return dataPluv.label.localeCompare(dataPluvB.label); }); } // console.log(datapluv); } else if (name == "pluviometer") { situation = "PLUVIÔMETRO"; r[r.length - 1].array_to_json.forEach(function (f) { if (f.fieldname == "comments") { comments = f.fieldsanswersvalue; } }); const pluv_sation = await getPluviometerStatio_informations(formId); if (pluv_sation) { pluv_sation.forEach((field) => { if (field.fieldname == "institutename") { institution = field.fieldsanswersvalue != "None" ? field.fieldsanswersvalue : "Não há vínculo institucional"; } else if (field.fieldname == "eventaddress") { address = field.fieldsanswersvalue; } }); } const currentPluv = JSON.parse(response).array_to_json.filter( (pluvForm) => getCurrentPluviometer(pluvForm, userId, data, datapluv) ); if (datapluv) { datapluv.sort(function (dataPluv, dataPluvB) { return dataPluv.label.localeCompare(dataPluvB.label); }); } } 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: null, institution: institution, address: address, date: date + " | " + time, description: comments, logo: getLogo(name, situation), pictures: pictures, data: datapluv, }; }; export default AssembleModalObject;