// BEFORE, without the function
useMutation(api.docs.setContent).withOptimisticUpdate((ctx, mutArgs) => {
const docContent = ctx.getQuery(api.docs.getContent, { doc: mutArgs.doc });
if (docContent) {
ctx.setQuery(
api.docs.getContent,
{ doc: mutArgs.doc },
{
...docContent,
content: mutArgs.content,
},
);
}
const newTitle = getTitle(mutArgs.content);
// Alright, let's iterate all document lists
for (const entry of ctx.getAllQueries(api.docs.get)) {
if (!entry.value) {
continue;
}
for (const doc of entry.value.page) {
if (doc._id == mutArgs.doc) {
// now, the fun begins - creating an update :)
const update = {
...entry.value,
page: entry.value.page.with(entry.value.page.indexOf(doc), {
...doc,
title: newTitle,
}),
} satisfies (typeof entry)["value"];
ctx.setQuery(api.docs.get, entry.args, update);
}
}
}
// BEFORE, without the function
useMutation(api.docs.setContent).withOptimisticUpdate((ctx, mutArgs) => {
const docContent = ctx.getQuery(api.docs.getContent, { doc: mutArgs.doc });
if (docContent) {
ctx.setQuery(
api.docs.getContent,
{ doc: mutArgs.doc },
{
...docContent,
content: mutArgs.content,
},
);
}
const newTitle = getTitle(mutArgs.content);
// Alright, let's iterate all document lists
for (const entry of ctx.getAllQueries(api.docs.get)) {
if (!entry.value) {
continue;
}
for (const doc of entry.value.page) {
if (doc._id == mutArgs.doc) {
// now, the fun begins - creating an update :)
const update = {
...entry.value,
page: entry.value.page.with(entry.value.page.indexOf(doc), {
...doc,
title: newTitle,
}),
} satisfies (typeof entry)["value"];
ctx.setQuery(api.docs.get, entry.args, update);
}
}
}