Skip to content
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: add ability to override sort field on relationship level #309

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/components/inputs/select/select.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface SelectProps<T> {
selected?: T;
options: SelectOption<T>[];
label?: string;
labelVariant?: "default" | "form" | "header";
labelVariant?: "default" | "form" | "header" | "small";
labelPosition?: "default" | "inline";
placeholder: string | null;
className?: string;
Expand Down Expand Up @@ -84,6 +84,7 @@ export const SelectLabel = <T extends string | number>({
labelVariant === "default" && "text-sm font-light md:text-base",
labelVariant === "form" && "block text-sm font-bold",
labelVariant === "header" && "text-sm md:text-base",
labelVariant === "small" && "text-xs text-manatee-300",
)}
>
{labelVariant === "form" ? formatObjectField(label) : label}
Expand Down Expand Up @@ -396,7 +397,9 @@ const SelectComponent = <T extends string | number>(
className={clsx(
selectClassName,
label && labelPosition === "default" && "mt-2",
label && labelPosition === "inline" && "ml-2",
label &&
labelPosition === "inline" &&
(labelVariant === "small" ? "ml-1" : "ml-2"),
)}
ref={refs.setReference}
>
Expand Down Expand Up @@ -457,15 +460,20 @@ const SelectComponent = <T extends string | number>(
data-testid="select"
className={clsx(
selectClassName,
roundedClassName,
sizingClassName,
label && "mt-2",
label && labelPosition === "default" && "mt-2",
label &&
labelPosition === "inline" &&
(labelVariant === "small" ? "ml-1" : "ml-2"),
)}
ref={mergeRefs([refs.setReference, propRef])}
>
<span
className={clsx(
"block truncate",
!selectedOption?.label && "text-gray-300",
variant === "pill" && "pr-6",
)}
>
{selectedOption?.label || placeholder || "Select option"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ describe("relationships view", () => {

await waitFor(() => {
expect(
withinEpisodesRelationship.getByText("Sorted by: Episode number"),
withinEpisodesRelationship.getByLabelText("Sorted by:"),
).toBeInTheDocument();
});

const sortedBy = withinEpisodesRelationship.getByLabelText("Sorted by:");
expect(sortedBy).toHaveTextContent("Episode number (Default)");
});

test("displays the value of the relationship sort field", async () => {
Expand Down
16 changes: 5 additions & 11 deletions src/components/panel/panel.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
BuiltInSkylarkObjectType,
GQLSkylarkErrorResponse,
ParsedSkylarkObjectConfig,
ParsedSkylarkRelationshipConfig,
ModifiedRelationshipsObject,
} from "src/interfaces/skylark";
import { parseMetadataForHTMLForm } from "src/lib/skylark/parsers";
import {
Expand Down Expand Up @@ -274,10 +276,8 @@ export const Panel = ({
updated: null,
});

const [modifiedRelationships, setModifiedRelationships] = useState<Record<
string,
{ added: ParsedSkylarkObject[]; removed: string[] }
> | null>(null);
const [modifiedRelationships, setModifiedRelationships] =
useState<ModifiedRelationshipsObject | null>(null);

const [modifiedAvailabilityObjects, setModifiedAvailabilityObjects] =
useState<{
Expand Down Expand Up @@ -543,13 +543,7 @@ export const Panel = ({

const handleRelationshipsObjectsModified = useCallback(
(
updatedModifiedRelationships: Record<
string,
{
added: ParsedSkylarkObject[];
removed: string[];
}
>,
updatedModifiedRelationships: ModifiedRelationshipsObject,
errors: HandleDropError[],
) => {
setModifiedRelationships(updatedModifiedRelationships);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
SkylarkObjectType,
ParsedSkylarkObject,
ParsedSkylarkObjectRelationship,
ParsedSkylarkRelationshipConfig,
ModifiedRelationshipsObject,
ModifiedRelationship,
} from "src/interfaces/skylark";
import { DragType, DroppableType } from "src/lib/dndkit/dndkit";
import { formatObjectField, hasProperty } from "src/lib/utils";
Expand All @@ -34,12 +37,9 @@
objectType: SkylarkObjectType;
uid: string;
tabState: PanelTabState[PanelTab.Relationships];
modifiedRelationships: Record<
string,
{ added: ParsedSkylarkObject[]; removed: string[] }
>;
modifiedRelationships: ModifiedRelationshipsObject;
setModifiedRelationships: (
rels: Record<string, { added: ParsedSkylarkObject[]; removed: string[] }>,
rels: ModifiedRelationshipsObject,
errors: HandleDropError[],
) => void;
inEditMode: boolean;
Expand Down Expand Up @@ -184,10 +184,10 @@
};

const parseAddedAndRemovedRelationshipObjects = (
existing: { added: ParsedSkylarkObject[]; removed: string[] },
existing: ModifiedRelationship,
added: ParsedSkylarkObject[],
removed: string[],
) => {
): ModifiedRelationship => {
const updatedAdded = added ? [...existing.added, ...added] : existing.added;
const updatedRemoved = removed
? [...existing.removed, ...removed]
Expand All @@ -200,6 +200,7 @@
return {
added: updatedAdded.filter(({ uid }) => !duplicates.includes(uid)),
removed: updatedRemoved.filter((uid) => !duplicates.includes(uid)),
config: existing.config,
};
};

Expand Down Expand Up @@ -276,7 +277,7 @@
) => {
const relationship = hasProperty(modifiedRelationships, relationshipName)
? modifiedRelationships[relationshipName]
: { added: [], removed: [] };
: { added: [], removed: [], config: {} };

setModifiedRelationships(
{
Expand All @@ -291,6 +292,29 @@
);
};

const modifyRelationshipConfig = (
relationshipName: string,
config: Partial<ParsedSkylarkRelationshipConfig>,
) => {

Check warning on line 298 in src/components/panel/panelSections/panelRelationships.component.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/panel/panelSections/panelRelationships.component.tsx#L298

Added line #L298 was not covered by tests
const relationship = hasProperty(modifiedRelationships, relationshipName)
? modifiedRelationships[relationshipName]
: { added: [], removed: [], config: {} };

Check warning on line 301 in src/components/panel/panelSections/panelRelationships.component.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/panel/panelSections/panelRelationships.component.tsx#L300-L301

Added lines #L300 - L301 were not covered by tests

setModifiedRelationships(

Check warning on line 303 in src/components/panel/panelSections/panelRelationships.component.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/panel/panelSections/panelRelationships.component.tsx#L303

Added line #L303 was not covered by tests
{
...modifiedRelationships,
[relationshipName]: {
...relationship,
config: {
...relationship.config,
...config,
},
},
},
[],
);
};

const relationshipNames = objectMetaRelationships.map(
({ relationshipName }) => relationshipName,
);
Expand Down Expand Up @@ -338,14 +362,20 @@
orderedRelationships,
activeRelationship,
)?.map((relationship) => {
const config =
const objectTypeDefaultConfig =
objectTypeRelationshipConfig?.[relationship.name] || null;

return (
<PanelRelationshipSection
key={relationship.name}
relationship={relationship}
config={config}
config={{
objectTypeDefault: objectTypeDefaultConfig,
overrides: {},
}}
modifiedConfig={
modifiedRelationships?.[relationship.name]?.config
}
inEditMode={inEditMode}
isFetchingMoreRelationships={isFetchingMoreRelationships}
newUids={getNewUidsForRelationship(
Expand All @@ -364,6 +394,9 @@
removed: [uid],
});
}}
updateRelationshipConfig={(
updatedConfig: Partial<ParsedSkylarkRelationshipConfig>,
) => modifyRelationshipConfig(relationship.name, updatedConfig)}

Check warning on line 399 in src/components/panel/panelSections/panelRelationships.component.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/panel/panelSections/panelRelationships.component.tsx#L399

Added line #L399 was not covered by tests
setSearchObjectsModalState={setSearchObjectsModalState}
/>
);
Expand All @@ -379,7 +412,7 @@
emptyOrderedRelationships,
activeRelationship,
)?.map((relationship) => {
const config =
const objectTypeDefaultConfig =
objectTypeRelationshipConfig?.[relationship.name] || null;

return (
Expand All @@ -389,7 +422,13 @@
isExpanded={!!activeRelationship}
key={relationship.name}
relationship={relationship}
config={config}
config={{
objectTypeDefault: objectTypeDefaultConfig,
overrides: {},
}}
modifiedConfig={
modifiedRelationships?.[relationship.name]?.config
}
inEditMode={inEditMode}
isFetchingMoreRelationships={isFetchingMoreRelationships}
newUids={getNewUidsForRelationship(
Expand All @@ -402,6 +441,9 @@
removed: [uid],
});
}}
updateRelationshipConfig={(
updatedConfig: Partial<ParsedSkylarkRelationshipConfig>,
) => modifyRelationshipConfig(relationship.name, updatedConfig)}

Check warning on line 446 in src/components/panel/panelSections/panelRelationships.component.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/panel/panelSections/panelRelationships.component.tsx#L446

Added line #L446 was not covered by tests
setSearchObjectsModalState={setSearchObjectsModalState}
/>
);
Expand Down
Loading
Loading