Created
October 25, 2024 09:34
-
-
Save jepser/cf8cdb067cfdefb1f5be13beb26f45a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// endpoints | |
query, | |
transformResponse, | |
async onQueryStarted({ answerId, themeIds, blockUuid, themes = [] }, { dispatch, queryFulfilled, getState }) { | |
// this is the way we can get the serialized cache query | |
const [selectedCacheName] = unmoderatedResultsApi.util.selectInvalidatedBy(getState(), [ | |
{ | |
type: 'BlockAnswers', | |
id: blockUuid, | |
}, | |
]); | |
if (!selectedCacheName) { | |
return; | |
} | |
const { originalArgs } = selectedCacheName; | |
const patchResult = dispatch( | |
unmoderatedResultsApi.util.updateQueryData( | |
'getBlockAnswers', | |
{ | |
blockUuid: originalArgs.blockUuid, | |
blockType: originalArgs.blockType, | |
filters: originalArgs.filters, | |
offset: originalArgs.offset, | |
limit: originalArgs.limit, | |
}, | |
draft => { | |
const selectedAnswer = draft.find( | |
(answer: any) => answer.uuid === answerId, | |
) as unknown as AiInputBlockAnswerDto; | |
if (!selectedAnswer || !selectedAnswer?.aiInputAnswer) { | |
return; | |
} | |
const themesForOptimisticUpdate: Pick<AiInputThemeDto, 'color' | 'id' | 'title'>[] = themes.reduce( | |
(formmatedThemes, theme) => { | |
const currentTheme = themeIds.find(id => id === theme.id); | |
if (!currentTheme) { | |
return formmatedThemes; | |
} | |
return [ | |
...formmatedThemes, | |
{ | |
id: theme.id, | |
title: theme.title, | |
color: theme.color, | |
}, | |
]; | |
}, | |
[] as Pick<AiInputThemeDto, 'color' | 'id' | 'title'>[], | |
); | |
selectedAnswer.aiInputAnswer.themes = themesForOptimisticUpdate; | |
}, | |
), | |
); | |
try { | |
await queryFulfilled; | |
} catch { | |
patchResult.undo(); | |
} | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment