-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/trunk' into feat/promotions-mile…
…stone-2
- Loading branch information
Showing
12 changed files
with
327 additions
and
35 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,43 @@ | ||
import { startCase } from "lodash-es"; | ||
import { useMemo } from "react"; | ||
|
||
import { SelectOptionType } from "types/common"; | ||
import { useGetIntrospectSchemaQuery } from "@graphql/generates"; | ||
import { client } from "@graphql/graphql-request-client"; | ||
import { FieldProperty, SchemaProperties, Type } from "types/schema"; | ||
|
||
export type FieldPropertySelectOption = SelectOptionType & FieldProperty | ||
|
||
export const isObjectType = (fieldProperty: FieldProperty): fieldProperty is FieldProperty<Type.Object> => fieldProperty.type === Type.Object; | ||
|
||
export const isArrayType = (fieldProperty: FieldProperty): fieldProperty is FieldProperty<Type.Array> => fieldProperty.type === Type.Array; | ||
|
||
const normalizeSchemaProperties = ({ schemaProperties = {}, filterFn, prependFieldName }: | ||
{ schemaProperties?: SchemaProperties, | ||
filterFn?: (property: FieldPropertySelectOption) => boolean, | ||
prependFieldName?: string | ||
}) : FieldPropertySelectOption[] => { | ||
const normalizedResults = Object.entries(schemaProperties).flatMap(([field, fieldProperty]) => { | ||
if (isObjectType(fieldProperty) && Object.keys(fieldProperty.properties).length) { | ||
return normalizeSchemaProperties({ schemaProperties: fieldProperty.properties, filterFn, prependFieldName: field }); | ||
} | ||
if (isArrayType(fieldProperty) && isObjectType(fieldProperty.items[0])) { | ||
return normalizeSchemaProperties({ schemaProperties: fieldProperty.items[0].properties, filterFn, prependFieldName: field }); | ||
} | ||
return [{ label: startCase(prependFieldName ? `${prependFieldName} ${field}` : field), value: fieldProperty.path, ...fieldProperty }]; | ||
}); | ||
|
||
return filterFn ? normalizedResults.filter(filterFn) : normalizedResults; | ||
}; | ||
|
||
export const useIntrospectSchema = ({ schemaName, filterFn, enabled }: | ||
{schemaName: string, filterFn?: (property: FieldProperty) => boolean, enabled: boolean}) => { | ||
const { data, isLoading } = useGetIntrospectSchemaQuery(client, { schemaName }, { enabled }); | ||
|
||
const schemaProperties = useMemo(() => { | ||
const properties = data?.introspectSchema.schema.properties; | ||
return normalizeSchemaProperties({ schemaProperties: properties, filterFn }); | ||
}, [data, filterFn]); | ||
|
||
return { schemaProperties, originalSchema: data?.introspectSchema.schema.properties, isLoading }; | ||
}; |
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,6 @@ | ||
query getIntrospectSchema($schemaName: String!) { | ||
introspectSchema(schemaName: $schemaName) { | ||
schemaName | ||
schema | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/hooks/useIntrospectSchema/useIntrospectSchema.test.tsx
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,26 @@ | ||
import { QueryClient, QueryClientProvider } from "react-query"; | ||
import { cartItemProperties } from "@mocks/handlers/introspectSchemaHandlers"; | ||
|
||
import { renderHook, waitFor } from "@utils/testUtils"; | ||
|
||
import { useIntrospectSchema } from "."; | ||
|
||
const client = new QueryClient(); | ||
const wrapper = ({ children }: {children: JSX.Element}) => <QueryClientProvider client={client}>{children}</QueryClientProvider>; | ||
|
||
describe("useIntrospectSchema", () => { | ||
it("should return normalize schema data", async () => { | ||
const { result } = renderHook(() => useIntrospectSchema({ schemaName: "CartItem", enabled: true }), { wrapper }); | ||
await waitFor(() => { | ||
expect(result.current.schemaProperties).toEqual([ | ||
{ label: "Product Id", value: "$.productId", ...cartItemProperties.productId }, | ||
{ label: "Price Type", value: "$.priceType", ...cartItemProperties.priceType }, | ||
{ label: "Product Tag Ids", value: "$.productTagIds", ...cartItemProperties.productTagIds }, | ||
{ label: "Parcel Containers", value: "$.parcel.containers", ...cartItemProperties.parcel.properties.containers }, | ||
{ label: "Parcel Length", value: "$.parcel.length", ...cartItemProperties.parcel.properties.length }, | ||
{ label: "Attributes Label", value: "$.attributes.[0].label", ...cartItemProperties.attributes.items[0].properties.label }, | ||
{ label: "Attributes Value", value: "$.attributes.[0].value", ...cartItemProperties.attributes.items[0].properties.value } | ||
]); | ||
}); | ||
}); | ||
}); |
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,83 @@ | ||
import { graphql } from "msw"; | ||
|
||
export const mockCartItemSchema = { | ||
schema: { | ||
properties: { | ||
productId: { | ||
type: "string", | ||
path: "$.productId" | ||
}, | ||
priceType: { | ||
type: "string", | ||
enum: [ | ||
"full", | ||
"clearance", | ||
"sale" | ||
], | ||
path: "$.priceType" | ||
}, | ||
productTagIds: { | ||
type: "array", | ||
items: [ | ||
{ | ||
type: "string", | ||
path: "$.productTagIds.[0]" | ||
} | ||
], | ||
additionalItems: false, | ||
path: "$.productTagIds" | ||
}, | ||
parcel: { | ||
type: "object", | ||
properties: { | ||
containers: { | ||
type: "string", | ||
path: "$.parcel.containers" | ||
}, | ||
length: { | ||
type: "number", | ||
path: "$.parcel.length" | ||
} | ||
}, | ||
required: [], | ||
additionalProperties: false, | ||
path: "$.parcel" | ||
}, | ||
attributes: { | ||
type: "array", | ||
items: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
label: { | ||
type: "string", | ||
path: "$.attributes.[0].label" | ||
}, | ||
value: { | ||
type: "string", | ||
path: "$.attributes.[0].value" | ||
} | ||
}, | ||
required: [ | ||
"label" | ||
], | ||
additionalProperties: false, | ||
path: "$.attributes.[0]" | ||
} | ||
], | ||
additionalItems: false, | ||
path: "$.attributes" | ||
} | ||
} | ||
}, | ||
schemaName: "CartItem" | ||
}; | ||
|
||
export const cartItemProperties = mockCartItemSchema.schema.properties; | ||
const getIntrospectSchemaHandler = graphql.query("getIntrospectSchema", (req, res, ctx) => | ||
res(ctx.data({ introspectSchema: mockCartItemSchema }))); | ||
|
||
|
||
export const handlers = [ | ||
getIntrospectSchemaHandler | ||
]; |
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
Oops, something went wrong.