-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Events v2: Update tRPC #8772
Merged
Merged
Events v2: Update tRPC #8772
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
a93a7d5
WIP
rikukissa d05521f
Merge branch 'develop' into update-trpc
rikukissa 3ba9120
Merge branch 'develop' of github.com:opencrvs/opencrvs-core into upda…
rikukissa 89cc67d
revert change in gateway
rikukissa 8af2e0b
Merge branch 'update-trpc' of github.com:opencrvs/opencrvs-core into …
rikukissa 138d7ba
fix linting errors
rikukissa 0f03b8c
cleanup
rikukissa 011cfe5
Merge branch 'develop' into update-trpc
rikukissa 4cc7c9e
Merge branch 'develop' of github.com:opencrvs/opencrvs-core into upda…
rikukissa 2462e68
Merge branch 'update-trpc' of github.com:opencrvs/opencrvs-core into …
rikukissa 01d37cd
fix linter errors
rikukissa 82d650e
Merge branch 'develop' into update-trpc
rikukissa 22739c9
cleanup
rikukissa 9a45fd1
revert bugfix for data stringifier
rikukissa ca830ba
Merge branch 'update-trpc' of github.com:opencrvs/opencrvs-core into …
rikukissa 094a1bb
move logic out of useEvents
rikukissa 318aad6
more cleanup
rikukissa 18a2916
add a comment for useEventAction
rikukissa 29b4209
remove unused dep
rikukissa 609e129
update readme
rikukissa deb0021
ensure all data is processed for action payload before submitting it …
rikukissa 9c53cac
Merge branch 'develop' into update-trpc
rikukissa 8a0f121
Merge branch 'develop' into update-trpc
rikukissa 07be5a1
Merge branch 'develop' of github.com:opencrvs/opencrvs-core into upda…
rikukissa e2bf6cf
Merge branch 'update-trpc' of github.com:opencrvs/opencrvs-core into …
rikukissa 8bb4e6a
Merge branch 'develop' into update-trpc
rikukissa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
101 changes: 101 additions & 0 deletions
101
packages/client/src/v2-events/features/events/useEvents/api.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* OpenCRVS is also distributed under the terms of the Civil Registration | ||
* & Healthcare Disclaimer located at http://opencrvs.org/license. | ||
* | ||
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. | ||
*/ | ||
import { | ||
MutationObserverOptions, | ||
OmitKeyof, | ||
QueryFunctionContext | ||
} from '@tanstack/react-query' | ||
import { TRPCClientError } from '@trpc/client' | ||
import { | ||
DecorateMutationProcedure, | ||
DecorateQueryProcedure, | ||
inferInput, | ||
inferOutput | ||
} from '@trpc/tanstack-react-query' | ||
import { EventDocument, EventIndex } from '@opencrvs/commons/client' | ||
import { AppRouter, queryClient, utils } from '@client/v2-events/trpc' | ||
|
||
export function getLocalEventData(eventId: string) { | ||
return queryClient.getQueryData( | ||
utils.event.get.queryKey(eventId) | ||
) as EventDocument | ||
} | ||
|
||
export function setEventData(id: string, data: EventDocument) { | ||
return queryClient.setQueryData(utils.event.get.queryKey(id), data) | ||
} | ||
|
||
export function setEventListData( | ||
updater: (eventIndices: EventIndex[] | undefined) => EventIndex[] | undefined | ||
) { | ||
return queryClient.setQueryData(utils.event.list.queryKey(), updater) | ||
} | ||
|
||
export async function invalidateEventsList() { | ||
return queryClient.invalidateQueries({ | ||
queryKey: utils.event.list.queryKey() | ||
}) | ||
} | ||
|
||
type TRPCError = TRPCClientError<AppRouter> | ||
type TRPCQueryKey<T> = [readonly string[], { input: T }] | ||
|
||
/** | ||
* Sets the default options for a mutation procedure. | ||
* | ||
* This function should be the primary method of changing settings for mutations | ||
* because it ensures that mutations stored in IndexedDB for later processing | ||
* after the application has reloaded can use the same settings without running | ||
* the same code paths again. | ||
* | ||
* i.e. if you want to override mutationFn, you must use setMutationDefault to do so. | ||
* | ||
* @template P - The type of the mutation procedure e.g. trpc.events.get. | ||
* @template Context - The type of the context, defaults to `any`. | ||
* @param {OmitKeyof<MutationObserverOptions<inferOutput<P>, TRPCError, inferInput<P>, Context>, 'mutationKey'>} options - The options to set as defaults, excluding the `mutationKey`. | ||
*/ | ||
export function setMutationDefaults< | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
P extends DecorateMutationProcedure<any>, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
Context = any | ||
>( | ||
mutation: P, | ||
options: OmitKeyof< | ||
MutationObserverOptions<inferOutput<P>, TRPCError, inferInput<P>, Context>, | ||
'mutationKey' | ||
> | ||
) { | ||
queryClient.setMutationDefaults(mutation.mutationKey(), options) | ||
} | ||
|
||
/** | ||
* Sets the default options for a mutation procedure. | ||
*/ | ||
export function setQueryDefaults< | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
P extends DecorateQueryProcedure<any> | ||
>( | ||
query: P, | ||
options: Omit< | ||
Parameters<typeof queryClient.setQueryDefaults>[1], | ||
'queryFn' | ||
> & { | ||
queryFn: ( | ||
input: QueryFunctionContext<TRPCQueryKey<inferInput<P>>> | ||
) => inferOutput<P> | Promise<inferOutput<P>> | ||
} | ||
) { | ||
queryClient.setQueryDefaults( | ||
query.queryKey(), | ||
options as Parameters<typeof queryClient.setQueryDefaults>[1] | ||
) | ||
} |
99 changes: 99 additions & 0 deletions
99
packages/client/src/v2-events/features/events/useEvents/outbox.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* OpenCRVS is also distributed under the terms of the Civil Registration | ||
* & Healthcare Disclaimer located at http://opencrvs.org/license. | ||
* | ||
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. | ||
*/ | ||
|
||
import { hashKey, Mutation, useQuery } from '@tanstack/react-query' | ||
|
||
import { | ||
DecorateMutationProcedure, | ||
inferInput, | ||
inferOutput | ||
} from '@trpc/tanstack-react-query' | ||
import { EventIndex } from '@opencrvs/commons/client' | ||
import { queryClient, useTRPC } from '@client/v2-events/trpc' | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function getPendingMutations<T extends DecorateMutationProcedure<any>>( | ||
procedure: T | ||
) { | ||
// type MutationFn = Exclude<T['mutationFn'], undefined> | ||
type Data = inferOutput<T> | ||
type Variables = inferInput<T> | ||
|
||
const mutationOptions = procedure.mutationOptions() | ||
const key = mutationOptions.mutationKey | ||
|
||
return queryClient | ||
.getMutationCache() | ||
.getAll() | ||
.filter((mutation) => mutation.state.status !== 'success') | ||
.filter( | ||
(mutation) => | ||
mutation.options.mutationKey && | ||
hashKey(mutation.options.mutationKey) === hashKey(key) | ||
) as Mutation<Data, Error, Variables>[] | ||
} | ||
|
||
function filterOutboxEventsWithMutation< | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
T extends DecorateMutationProcedure<any> | ||
>( | ||
events: EventIndex[], | ||
mutation: T, | ||
filter: (event: EventIndex, parameters: inferInput<T>) => boolean | ||
) { | ||
return getPendingMutations(mutation).flatMap((m) => { | ||
const variables = m.state.variables | ||
return events.filter((event) => filter(event, variables)) | ||
}) | ||
} | ||
|
||
export function useOutbox() { | ||
const trpc = useTRPC() | ||
const eventListQuery = useQuery({ | ||
...trpc.event.list.queryOptions(), | ||
queryKey: trpc.event.list.queryKey() | ||
}) | ||
|
||
const eventsList = eventListQuery.data ?? [] | ||
|
||
const eventFromDeclareActions = filterOutboxEventsWithMutation( | ||
eventsList, | ||
trpc.event.actions.declare, | ||
(event, parameters) => { | ||
return event.id === parameters.eventId && !parameters.draft | ||
} | ||
) | ||
|
||
const eventFromValidateActions = filterOutboxEventsWithMutation( | ||
eventsList, | ||
trpc.event.actions.validate, | ||
(event, parameters) => { | ||
return event.id === parameters.eventId && !parameters.draft | ||
} | ||
) | ||
|
||
const eventFromRegisterActions = filterOutboxEventsWithMutation( | ||
eventsList, | ||
trpc.event.actions.register, | ||
(event, parameters) => { | ||
return event.id === parameters.eventId && !parameters.draft | ||
} | ||
) | ||
|
||
return eventFromDeclareActions | ||
.concat(eventFromDeclareActions) | ||
.concat(eventFromValidateActions) | ||
.concat(eventFromRegisterActions) | ||
.filter( | ||
/* uniqueById */ | ||
(e, i, arr) => arr.findIndex((a) => a.id === e.id) === i | ||
) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commented code