I need to update all formulas in cache that use the same raw ingredient after it has been changed using tanstack query. In Tanstack Query I newbie
My implementation but this code does not always work correctly. It updates once, it doesn't update the next time.
const { data: formulas } = useGetFormulas({
queryParams: getParamsString(),
});
updateRelation(
{
id,
ingredientId: `${subIngredient.id}`,
relationType: "subIngredients",
body: { value: weight_percent },
},
{
onSuccess() {
queryClient.refetchQueries({
queryKey: [queryKeys.rawIngredient, id],
});
queryClient.refetchQueries({
queryKey: [queryKeys.rawIngredients],
})
queryClient.refetchQueries({
queryKey: [queryKeys.formulas],
})
queryClient.refetchQueries({
queryKey: [queryKeys.lastUpdateFormules],
})
if (formulas) {
const formulaIds = formulas.rows
.filter((formula: IFormulaItem) => formula.ri_ids.includes(+id))
.map((formula: IFormulaItem) => String(formula.id));
console.log({ formulaIds });
formulaIds.forEach((formulaId) => {
queryClient.refetchQueries({
queryKey: [queryKeys.formula, formulaId],
});
});
}
},
onError() {
toast.error(toastTexts.error, defaultErrorToastOptions);
},
},
);
This part of the code does not want to work correctly
if (formulas) {
const formulaIds = formulas.rows
.filter((formula: IFormulaItem) => formula.ri_ids.includes(+id))
.map((formula: IFormulaItem) => String(formula.id));
formulaIds.forEach((formulaId) => {
queryClient.refetchQueries({
queryKey: [queryKeys.formula, formulaId],
});
})
}
Tell me how to solve this problem pleas