|
8 | 8 | // no queryFn is provided (ideally we never provide a queryFn to reduce
|
9 | 9 | // duplication and bugs but always rely on defaultQueryFn to route to the correct API call)
|
10 | 10 |
|
11 |
| -import { QueryFunctionContext } from '@tanstack/react-query' |
| 11 | +import { QueryClient, QueryFunctionContext } from '@tanstack/react-query' |
12 | 12 | import { match, P } from 'ts-pattern'
|
| 13 | +import { |
| 14 | + ConfigService, |
| 15 | + ConnectorService, |
| 16 | + PipelineService, |
| 17 | + ProjectCodeResponse, |
| 18 | + ProjectDescr, |
| 19 | + ProjectService, |
| 20 | + UpdateProjectRequest |
| 21 | +} from './manager' |
13 | 22 |
|
14 |
| -import { ConfigService, ConnectorService, PipelineService, ProjectService } from './manager' |
| 23 | +// Updates the query cache for a `UpdateProjectRequest` response. |
| 24 | +export const projectQueryCacheUpdate = (queryClient: QueryClient, newData: UpdateProjectRequest) => { |
| 25 | + queryClient.setQueryData( |
| 26 | + ['projectCode', { project_id: newData.project_id }], |
| 27 | + (oldData: ProjectCodeResponse | undefined) => { |
| 28 | + if (oldData) { |
| 29 | + const newd = { |
| 30 | + ...oldData, |
| 31 | + ...{ |
| 32 | + project: { |
| 33 | + ...oldData.project, |
| 34 | + ...{ |
| 35 | + name: newData.name, |
| 36 | + description: newData.description ? newData.description : oldData.project.description |
| 37 | + }, |
| 38 | + }, |
| 39 | + code: newData.code ? newData.code : oldData.code |
| 40 | + } |
| 41 | + }; |
| 42 | + console.log('newdata is') |
| 43 | + console.log(newd); |
| 44 | + return newd |
| 45 | + } else { |
| 46 | + return oldData |
| 47 | + } |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + queryClient.setQueryData( |
| 52 | + ['projectStatus', { project_id: newData.project_id }], |
| 53 | + (oldData: ProjectDescr | undefined) => { |
| 54 | + return oldData |
| 55 | + ? { |
| 56 | + ...oldData, |
| 57 | + ...{ name: newData.name, description: newData.description ? newData.description : oldData.description } |
| 58 | + } |
| 59 | + : oldData |
| 60 | + } |
| 61 | + ) |
| 62 | + |
| 63 | + queryClient.setQueryData(['project'], (oldData: ProjectDescr[] | undefined) => |
| 64 | + oldData?.map((project: ProjectDescr) => { |
| 65 | + if (project.project_id === newData.project_id) { |
| 66 | + const projectDescUpdates = { |
| 67 | + name: newData.name, |
| 68 | + description: newData.description ? newData.description : project.description |
| 69 | + } |
| 70 | + return { ...project, ...projectDescUpdates } |
| 71 | + } else { |
| 72 | + return project |
| 73 | + } |
| 74 | + }) |
| 75 | + ) |
| 76 | +} |
15 | 77 |
|
16 | 78 | export const defaultQueryFn = async (context: QueryFunctionContext) => {
|
17 | 79 | return match(context.queryKey)
|
|
0 commit comments