-
Notifications
You must be signed in to change notification settings - Fork 38
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
feat(react-hooks): extend credential providers with format data #146
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ import type { | |
RecordUpdatedEvent, | ||
Agent, | ||
BaseEvent, | ||
GetFormatDataReturn, | ||
IndyCredentialFormat, | ||
} from '@aries-framework/core' | ||
import type { Constructor } from '@aries-framework/core/build/utils/mixins' | ||
|
||
|
@@ -14,9 +16,20 @@ import { map, filter, pipe } from 'rxjs' | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type BaseRecordAny = BaseRecord<any, any, any> | ||
type RecordClass<R extends BaseRecordAny> = Constructor<R> & { type: string } | ||
|
||
interface CombinedRecord<R extends BaseRecord> { | ||
record: R | ||
formatData: GetFormatDataReturn<[IndyCredentialFormat]> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to only specifically do I also implemented this locally for some projects and just made the formatData a separate hook.
or
Which could then optionally take a generic for the format data, i.e. |
||
} | ||
|
||
export interface RecordsState<R extends BaseRecordAny> { | ||
loading: boolean | ||
records: R[] | ||
records: Array<R> | ||
} | ||
|
||
export interface CombinedRecordsState<R extends BaseRecordAny> { | ||
loading: boolean | ||
records: Array<CombinedRecord<R>> | ||
} | ||
|
||
export const addRecord = <R extends BaseRecordAny>(record: R, state: RecordsState<R>): RecordsState<R> => { | ||
|
@@ -48,6 +61,46 @@ export const removeRecord = <R extends BaseRecordAny>(record: R, state: RecordsS | |
} | ||
} | ||
|
||
export const addCombinedRecord = <R extends BaseRecordAny>( | ||
combinedRecord: CombinedRecord<R>, | ||
state: CombinedRecordsState<R> | ||
): CombinedRecordsState<R> => { | ||
const newRecordsState = [...state.records] | ||
newRecordsState.unshift(combinedRecord) | ||
return { | ||
loading: state.loading, | ||
records: newRecordsState, | ||
} | ||
} | ||
|
||
export const updateCombinedRecord = <R extends BaseRecordAny>( | ||
combinedRecord: CombinedRecord<R>, | ||
state: CombinedRecordsState<R> | ||
): CombinedRecordsState<R> => { | ||
const { record } = combinedRecord | ||
const newRecordsState = [...state.records] | ||
const index = newRecordsState.findIndex(({ record: r }) => r.id === record.id) | ||
if (index > -1) { | ||
newRecordsState[index] = combinedRecord | ||
} | ||
return { | ||
loading: state.loading, | ||
records: newRecordsState, | ||
} | ||
} | ||
|
||
export const removeCombinedRecord = <R extends BaseRecordAny>( | ||
combinedRecord: CombinedRecord<R>, | ||
state: CombinedRecordsState<R> | ||
): CombinedRecordsState<R> => { | ||
const { record } = combinedRecord | ||
const newRecordsState = state.records.filter(({ record: r }) => r.id !== record.id) | ||
return { | ||
loading: state.loading, | ||
records: newRecordsState, | ||
} | ||
} | ||
|
||
const filterByType = <R extends BaseRecordAny>(recordClass: RecordClass<R>) => { | ||
return pipe( | ||
map((event: BaseEvent) => (event.payload as Record<string, R>).record), | ||
|
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.
Maybe useCredentialsWithFormatData()? Or do you think that is too long?
combined doesn't really give any context on what is combined