-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UILD-410: Record generation refactoring (#42)
- Loading branch information
1 parent
5b0b7b8
commit 6b737ce
Showing
16 changed files
with
1,051 additions
and
240 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useRecoilValue } from 'recoil'; | ||
import { useServicesContext } from './useServicesContext'; | ||
import state from '@state'; | ||
|
||
export const useRecordGeneration = () => { | ||
const { recordGeneratorService } = useServicesContext(); | ||
const schema = useRecoilValue(state.config.schema); | ||
const userValues = useRecoilValue(state.inputs.userValues); | ||
const selectedEntries = useRecoilValue(state.config.selectedEntries); | ||
const initialSchemaKey = useRecoilValue(state.config.initialSchemaKey); | ||
|
||
const generateRecord = () => { | ||
recordGeneratorService?.init({ | ||
schema, | ||
initKey: initialSchemaKey, | ||
userValues, | ||
selectedEntries, | ||
}); | ||
|
||
return recordGeneratorService?.generate(); | ||
}; | ||
|
||
return { generateRecord }; | ||
}; |
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,2 @@ | ||
export { SchemaTraverser } from './schemaTraverser'; | ||
export { RecordGenerator } from './record'; |
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,9 @@ | ||
export interface IRecordGenerator { | ||
init: (params: { | ||
schema: Map<string, SchemaEntry>; | ||
initKey: string | null; | ||
userValues: UserValues; | ||
selectedEntries: string[]; | ||
}) => void; | ||
generate: () => Record<string, RecordEntry<RecursiveRecordSchema>> | undefined; | ||
} |
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,57 @@ | ||
import { filterUserValues } from '@common/helpers/profile.helper'; | ||
import { SchemaTraverser } from './schemaTraverser'; | ||
import { IRecordGenerator } from './record.interface'; | ||
|
||
export class RecordGenerator implements IRecordGenerator { | ||
private schema: Map<string, SchemaEntry>; | ||
private initKey: string | null; | ||
private userValues: UserValues; | ||
private selectedEntries: string[]; | ||
|
||
constructor(private readonly schemaTraverser: SchemaTraverser) { | ||
this.schemaTraverser = schemaTraverser; | ||
this.schema = new Map(); | ||
this.initKey = null; | ||
this.userValues = {}; | ||
this.selectedEntries = []; | ||
} | ||
|
||
init({ | ||
schema, | ||
initKey, | ||
userValues, | ||
selectedEntries, | ||
}: { | ||
schema: Map<string, SchemaEntry>; | ||
initKey: string | null; | ||
userValues: UserValues; | ||
selectedEntries: string[]; | ||
}) { | ||
this.schema = schema; | ||
this.initKey = initKey; | ||
this.userValues = userValues; | ||
this.selectedEntries = selectedEntries; | ||
} | ||
|
||
public generate() { | ||
if (!Object.keys(this.userValues).length || !this.schema.size || !this.initKey) { | ||
return; | ||
} | ||
|
||
const filteredValues = filterUserValues(this.userValues); | ||
const result: Record<string, RecordEntry> = {}; | ||
|
||
this.schemaTraverser | ||
.init({ | ||
schema: this.schema, | ||
userValues: filteredValues, | ||
selectedEntries: this.selectedEntries, | ||
initialContainer: result, | ||
}) | ||
.traverse({ | ||
key: this.initKey, | ||
}); | ||
|
||
return result; | ||
} | ||
} |
Oops, something went wrong.