How does an object that contains two start and end dates - sanity

useEffect(() => {
client
.fetch(
`*[_type == "indicadore-chave_autonomia_da_tomada_decissao"]{
indicadores_chave,
genero,
icf,
data{
inicio,
fim
},
fonte,
total
}| order(indicadores_chave asc)`
)
.then(data => {
setDecisionData(data)
})
.catch(console.error)
}, [])
const [dicisionData, setDecisionData] = useState([])
const rowData = dicisionData.map(item => ({
IndicadoresChave: item.indicadores_chave,
Total: item.total,
Femenino: item.genero.femenino,
Masculino: item.genero.masculino,
ICF: item.icf,
Data: format(new Date([item.data?.inicio, item.date?.fim]), 'yyyy'),
Fonte: item.fonte
}))
I'm using sanity with back end and date-fn to format dates, I'm trying to format two dates start and end, but this just formatting one date and the other is not showing up
Translation
data{ inicio, fim }.Portuguese
data{ start, end }English

Related

React Native - How to deal with this

I'm a beginner and don't know how to go about it. I have a "state" collection and a sub-collection "city". The city has a people field with the amount of people. How to download the number of people in a city and display it in the country view.
enter image description here
enter image description here
const getData = () => {
firestore().collection('users').doc(user.uid).collection('country')
.onSnapshot(
 querySnapshot => {
const countryData = [];
let total = 0;
 querySnapshot.forEach(doc => {
countryData.push({...doc.data(), id: doc.id});
firestore()
.collection('users')
.doc(user.uid)
.collection('country')
.doc(doc.id)
.collection('city')
.onSnapshot(
 querySnapshot => {
const itemPeople = [];
let totalPeople = 0;
 querySnapshot.forEach(doc2 => {
totalPeople += doc2.data().people;
itemPeople.push({totalPeople});
});
setItemPeople(totalPeople);
    },
)
});
setCountryData(countryData);
    },
      error => {
       console.log(error)
    }
)
};
useEffect(() => {
getData();
}, []);
Here is the example how you can get the people value
const result = firebase
.firestore()
.collection("Country")
.doc("your user's UID")
.collection("City")
.get()
.then(querySnapshot => {
querySnapshot.forEach(queryDocumentSnapshot => {
console.log(queryDocumentSnapshot.get("People"));
});
})
.catch(err => {
alert(err);
});

Cypress: save result, then perform web actions, then compare initial result with end result after actions

I would appreciate some help: 1) I am obtaining the total number of items in a list, 2) I perform an action in the website that should remove one item in the list and 3) I need to check that the item is discounted from the list (total items - 1)
static Counter() {
cy.get('.page-number > span')
.invoke('text')
.then((s) => {
return s.match(/([\d\.]*)$/gmi)[0]
}).then(parseInt).should('be.a', 'number')
}
describe('Compare totals before/after performing web actions', () => {
it('Store & compare', () => {
const before = Counter()
//Perform actions in the web
const after = Counter()
expect(after == before - 1).to.equal(true) //?!
})
})
Thank you very much in advance!
I got it if useful:
describe('Compare totals before/after performing web actions', () => {
it.only('Store & compare', () => {
cy.get('.page-number > span')
.invoke('text')
.then((s) => {
//Trim the text: number is mixed with text=> | 1-25 / 10000 +
const u = s.match(/([\d\.]*)$/gmi)[0]
cy.log('u =', u) //√
//perform action that discounts one item from the list [...]
cy.get('.page-number > span')
.invoke('text')
.then((s) => {
//Trim the text: number is mixed with text=> | 1-25 / 10000 +
const p = s.match(/([\d\.]*)$/gmi)[0]
cy.log('p =', p)
expect(p, 'Total before').equal(u + 1, 'Total after')
})
}) //.then(cy.log)

Best way pass array as query params for pg PoolClient with postgres using typescript

I am fetching some data from the postgres database with typescript using pg PoolClient, currently, I am fetching using the below way please let me know if there is any better way to achieve the same
const SPECIAL_LIST = [
"ABC",
"XYZ",
"PQR",
"LMN",
"EFG",
"IJK",
];
var params = SPECIAL_LIST.reduce(function (a, b, idx) {
if (idx === 0) return '$' + (idx + 2);
else return a + ', $' + (idx + 2);
}, '');
const sql = "Select sg.code,sg.dep,sg.vo,sg.arr from table1 sg where sg.is_omitted is false and v_code = $1 and sg.r_code not in (" +params+ ") order by sg.est_datetime asc"
const values = [vesselCode].concat(SPECIAL_LIST);
const client = await pool.connect();
await client
.query(sql, values)
.then((res) => {
const data = res.rows;
console.log(res.rowCount)
data.forEach((row) => {
// function logic
});
})
.catch((e) => console.error(e))
.then(() => client.release());
Found the solution using ANY below is the solution
const sql =
`SELECT v1, v2, v3, v4, v5, v6, v7
FROM ${TABLE_NAME}
WHERE omitted IS FALSE
AND v1 = $1
AND NOT rcode = ANY ($2)
ORDER BY datetime_dt ASC`;
const values = [vCode, SPECIAL_SITES];
const client = await pool.connect();
await client
.query({ text: sql, values: values })
.then((res) => {
res.rows.forEach((row) => {
//......
});
})
.catch((e) => console.error(e))
.then(() => client.release());

Vue js - not all the data showing after store dispatch

The select box not showing sometimes the first color and sometimes not showing the first color.
How can i make it to show all the item in the select box?
I'm not getting for some reason all the promises
You can see the issue in the picture
Please help me to fix this issue i'm new to vue js
My code:
data() {
return {
propertiesTree: []
}
}
getPropertyGroups(objectId: number): void {
if (this.$data.currentObjectId === objectId)
return;
let component: any = this;
this.$data.currentObjectId = objectId;
component.showLoader();
this.$store.dispatch('properties/getPropertyGroups', objectId)
.then(({ data, status }: { data: string | Array<propertiesInterfaces.PropertyGroupDto>, status: number }) => {
// console.log(data, "data");
// console.log(status, "status")
if (status === 500) {
this.$message({
message: data as string,
type: "error"
});
}
else {
let anyData = data as any;
anyData.map(item => {
item.properties.map(prop => {
if(prop.type.toString() === 'dictionary'){
prop.dictionaryList = [];
prop.color = '';
this.getWholeDictionaryList(prop.dictionaryId.value, prop)
}
});
});
}
component.hideLoader();
});
},
getWholeDictionaryList(dictionaryId: number, prop: any){
this.$store.dispatch('dictionaries/getWholeDictionaryList', dictionaryId).then(
({ data, status }: { data: Array<any> |string , status: number }) => {
if (status === 500) {
this.$message({
message: data as string,
type: "error"
});
} else {
const arrData = data as Array<any>;
arrData.map((item,index) => {
prop.dictionaryList = [];
prop.dictionaryList = data;
this.getDictionaryItemColor(item.id.value, data, index, prop);
});
}
});
},
getDictionaryItemColor(dictionaryItemId:number, dictionaryList: Array<any>, index:number, current){
this.$store.dispatch('patterns/getDictionaryItemColor', dictionaryItemId).then((data: any, status: number) => {
if (status === 500) {
this.$message({
message: data as string,
type: "error"
});
} else{
debugger
if(current.dictionaryItemId.value === data.data.sceneObjectId)
current.color = data.data.colorString;
dictionaryList[index].color = data.data.colorString ? data.data.colorString: '#FFFFFF';
}
});
},
Html code of the select box
<el-select v-model="data.color" placeholder="Select">
<el-option
v-for="item in data.dictionaryList"
:key="item.name"
:label="item.color"
:value="item.color">
</el-option>
</el-select>
I did return to dispatch
let dispatch = this.getWholeDictionaryList(prop.dictionaryId.value, prop)
let promiseArr = [];
promiseArr.push(dispatch);
after the map closing tag i did
Promise.all(promisArr).then( () => {
debugger
this.$data.propertiesTree = anyData;
});
And I've got it solved

setinterval function is running infinitely

const interval = setInterval(() => {
fire.database().ref().child(getpath())
.once("value",
(snapshot) => {
let item = snapshot.val()
console.log(item)
if (item !== null) {
let array = [];
Object.
keys(item)
.forEach(i => array.push(item[i]));
setCard1(array);
}
console.log(item, "item")
if (item !== null) {
itemlen = 7 //length of object I get from valid result
//stop polling for results
console.log(itemlen, "should clear")
}
else {
console.log("polling")
}
})
}, 1000)
console.log("comingout")
if (itemlen !== 0) {
console.log("goingIn")
clearInterval(interval);
}
}, [prefVar]);
expected clearinterval to stop the setinterval function but it is running continuosly and not stopping
itemlen is getting non zero values.Is there a better way of implementing this ?
I want stop useEffect once I get valid value from db.My code inside the for setinterval selects a random path and retrieve that path only problem is that sometimes the path is empty,thus using setInterval
I would create two state items, one which keeps the interval and the other which stores itemlen, and would use another useEffect to listen on changes to itemlen, and when it is not 0, the interval should clear. Also, I would check if there is another interval running before you start another one.
const [itemlen, setItemlen] = useState(0);
const [pollingInterval, setPollingInterval] = useState(null);
useEffect(() => {
if (pollingInterval === null) {
setPollingInterval(setInterval(() => {
fire.database().ref().child(getpath())
.once("value",
(snapshot) => {
let item = snapshot.val()
console.log(item)
if (item !== null) {
let array = [];
Object.
keys(item)
.forEach(i => array.push(item[i]));
setCard1(array);
}
console.log(item, "item")
if (item !== null) {
setItemlen(7);
console.log("should clear")
} else {
console.log("polling")
}
})
}, 1000));
}
}, [prefVar]);
useEffect(() => {
if (itemlen !== 0 && pollingInterval !== null) {
clearInterval(pollingInterval);
setPollingInterval(null);
}
}, [itemlen])