Skip to content

Commit

Permalink
Edit Stream Scope Improvements (#374)
Browse files Browse the repository at this point in the history
This PR adds the add page flow stream scope improvements to the page
edits. There is now a new edit scope button on the page panel next to
the settings button which opens up a new Stream Scope Edit modal.

The modal is pre-populated by the current stream scope config in the
page template. Since we are only allowing users to edit one stream scope
at a time, if the developer has specified a mix of stream scopes, all
editing will be disabled. Users can only submit their new selection if
changes have been made, similar to the old page settings.

If there are no entity types or saved filters, then these fields will
not be editable. In these cases, even if the developer has specified a
stream scope, it will not be displayed.

J=SLAP-2918
TEST=auto, manual
  • Loading branch information
alextaing authored Sep 19, 2023
1 parent d5c46aa commit 28997c6
Show file tree
Hide file tree
Showing 19 changed files with 589 additions and 449 deletions.
6 changes: 6 additions & 0 deletions packages/studio/src/components/ActivePagePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PropsWithChildren, useMemo } from "react";
import RemovePageButton from "./RemovePageButton";
import { Tooltip } from "react-tooltip";
import PageSettingsButton from "./PageSettingsButton/PageSettingsButton";
import EditStreamScopeButton from "./EditStreamScopeButton";

/**
* ActivePagePanel displays the available pages and allows the user to switch
Expand Down Expand Up @@ -59,6 +60,10 @@ function PageItem({ pageName }: { pageName: string }) {
void updateActivePage(pageName);
}

const isEntityPage = useStudioStore(
(store) => !!store.pages.pages[pageName].pagesJS?.streamScope
);

return (
<ListItem>
<div className="flex items-center overflow-auto">
Expand All @@ -72,6 +77,7 @@ function PageItem({ pageName }: { pageName: string }) {
</button>
</div>
<div className="flex items-center space-x-3">
{isEntityPage && <EditStreamScopeButton pageName={pageName} />}
{isPagesJSRepo && <PageSettingsButton pageName={pageName} />}
<RemovePageButton pageName={pageName} />
</div>
Expand Down
10 changes: 6 additions & 4 deletions packages/studio/src/components/AddPageButton/EntityIdField.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import Select, { MultiValue, StylesConfig } from "react-select";
import useStudioStore from "../../store/useStudioStore";
import StreamScopeFieldLabel from "./StreamScopeFieldLabel";
import StreamScopeFieldLabel from "../common/StreamScopeFieldLabel";
import { ChangeEvent, useCallback, useMemo, useState } from "react";
import { pillContainerClass } from "../PillPicker/PillPickerInput";
import classNames from "classnames";
import { updateScopeField } from "../StreamScopePicker";

interface Props {
disabled: boolean;
updateSelection: (selectedIds: string[]) => void;
updateSelection: updateScopeField;
selectedIds?: string[];
}

Expand All @@ -20,7 +21,7 @@ export default function EntityIdField({
store.accountContent.entitiesRecord,
store.accountContent.fetchEntities,
]);
const availableEntityTypes = disabled ? [] : Object.keys(entitiesRecord);
const availableEntityTypes = Object.keys(entitiesRecord);
const [isLoading, setIsLoading] = useState(false);
const [entityType, setEntityType] = useState<string | undefined>(
availableEntityTypes[0]
Expand Down Expand Up @@ -72,7 +73,7 @@ export default function EntityIdField({
"mt-2 bg-gray-50 text-gray-500 pb-2"
)}
>
{!disabled && "No entities found in the account."}
No entities found in the account.
</div>
</div>
);
Expand Down Expand Up @@ -104,6 +105,7 @@ export default function EntityIdField({
isSearchable={false}
placeholder="Select entity ids..."
styles={selectStyles}
isDisabled={disabled}
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { useCallback, useMemo } from "react";
import DialogModal from "../common/DialogModal";
import { FlowStepModalProps } from "./FlowStep";
import { useStreamScope } from "./AddPageContext";
import useStudioStore from "../../store/useStudioStore";
import StreamScopePicker, {
updateScopeFieldFactory,
} from "../StreamScopePicker";
import { StreamScope } from "@yext/studio-plugin";
import StreamScopeField, {
StreamScopeFieldProps,
} from "../common/StreamScopeField";
import EntityIdField from "./EntityIdField";

export default function StreamScopeCollector({
isOpen,
Expand All @@ -23,60 +21,33 @@ export default function StreamScopeCollector({
await handleConfirm();
}, [streamScope, setStreamScope, handleConfirm]);

const streamScopeFields = useStreamScopeFields();
const updateSelection: updateScopeFieldFactory = useCallback(
(streamScopeField: keyof StreamScope) => (selectedIds: string[]) => {
if (selectedIds.length) {
setStreamScope({
[streamScopeField]: selectedIds,
});
} else {
setStreamScope({});
}
},
[setStreamScope]
);

const modalBodyContent = useMemo(() => {
const totalStreamScopeItems = Object.values(streamScope ?? {}).reduce(
(numItems, scopeItems) => {
return numItems + scopeItems.length;
},
0
);

const updateSelection =
(streamScopeField: keyof StreamScope) => (selectedIds: string[]) => {
if (selectedIds.length) {
setStreamScope({
[streamScopeField]: selectedIds,
});
} else {
setStreamScope({});
}
};

return (
<>
<div className="italic mb-4">
Use one of the optional fields below to specify which entities this
page can access.
</div>
<EntityIdField
disabled={
!!streamScope?.entityTypes?.length ||
!!streamScope?.savedFilterIds?.length
}
updateSelection={updateSelection("entityIds")}
selectedIds={streamScope?.entityIds}
<StreamScopePicker
streamScope={streamScope}
updateSelection={updateSelection}
/>
{streamScopeFields.map(([streamScopeField, options]) => {
const selectedIds: string[] | undefined =
streamScope?.[streamScopeField];
const hasOtherScopeFilters =
totalStreamScopeItems > (selectedIds?.length ?? 0);
return (
<StreamScopeField
key={streamScopeField}
streamScopeField={streamScopeField}
options={options}
selectedIds={selectedIds}
updateSelection={updateSelection(streamScopeField)}
disabled={hasOtherScopeFilters}
/>
);
})}
</>
);
}, [streamScopeFields, streamScope, setStreamScope]);
}, [streamScope, updateSelection]);

return (
<DialogModal
Expand All @@ -90,20 +61,3 @@ export default function StreamScopeCollector({
/>
);
}

function useStreamScopeFields() {
const [savedFilters, entitiesRecord] = useStudioStore((store) => [
store.accountContent.savedFilters,
store.accountContent.entitiesRecord,
]);

return useMemo(() => {
return [
[
"entityTypes",
Object.keys(entitiesRecord).map((entityType) => ({ id: entityType })),
],
["savedFilterIds", savedFilters],
] satisfies [keyof StreamScope, StreamScopeFieldProps["options"]][];
}, [entitiesRecord, savedFilters]);
}

This file was deleted.

2 changes: 1 addition & 1 deletion packages/studio/src/components/DeployButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function DeployButton() {

return (
<button
className="ml-4 py-1 px-3 text-white rounded-md disabled:bg-gray-400 bg-blue-600 hover:bg-blue-500"
className="ml-4 py-1 px-3 text-white rounded-md disabled:bg-gray-400 bg-blue-600 hover:bg-blue-700"
onClick={handleClick}
disabled={isDisabled}
aria-label="Deploy Changes to Repository"
Expand Down
40 changes: 40 additions & 0 deletions packages/studio/src/components/EditStreamScopeButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ReactComponent as Scope } from "../icons/scope.svg";
import { useCallback } from "react";
import ButtonWithModal, { renderModalFunction } from "./common/ButtonWithModal";
import EditStreamScopeModal from "./EditStreamScopeModal";

interface EditStreamScopeButtonProps {
pageName: string;
}

// TODO: Consolidate with PageSettingsButton once we rework Form Modal (from SLAP-2918)
/**
* Renders a button for editing the Stream Scope for an Entity page in a PagesJS repo.
* When the button is clicked, a modal is displayed where the Stream Scope can
* be edited.
*/
export default function EditStreamScopeButton({
pageName,
}: EditStreamScopeButtonProps): JSX.Element {
const renderModal: renderModalFunction = useCallback(
(isOpen, handleClose) => {
return (
<EditStreamScopeModal
pageName={pageName}
isOpen={isOpen}
handleClose={handleClose}
/>
);
},
[pageName]
);

return (
<ButtonWithModal
buttonContent={<Scope />}
renderModal={renderModal}
ariaLabel={`Edit ${pageName} Stream Scope Settings`}
buttonClassName="text-gray-800"
/>
);
}
102 changes: 102 additions & 0 deletions packages/studio/src/components/EditStreamScopeModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { useCallback, useMemo, useState } from "react";
import DialogModal from "./common/DialogModal";
import StreamScopePicker, {
updateScopeFieldFactory,
} from "./StreamScopePicker";
import { ResponseType, StreamScope } from "@yext/studio-plugin";
import useStudioStore from "../store/useStudioStore";
import { isEqual, sortBy } from "lodash";
import { toast } from "react-toastify";

export interface EditStreamScopeModalProps {
pageName: string;
isOpen: boolean;
handleClose: () => void;
}

export default function EditStreamScopeModal(props: EditStreamScopeModalProps) {
const { pageName, isOpen, handleClose } = props;
const [streamScope, updateStreamScope, generateTestData] = useStudioStore(
(store) => [
store.pages.pages[pageName].pagesJS?.streamScope,
store.pages.updateStreamScope,
store.actions.generateTestData,
]
);

const originalScope = useMemo(() => streamScope ?? {}, [streamScope]);
const [selectedScope, setSelectedScope] = useState(originalScope);

const onConfirm = useCallback(async () => {
updateStreamScope(pageName, selectedScope);
const regenerateTestData = async () => {
const response = await generateTestData();
if (response.type === ResponseType.Error) {
toast.error(
"Error generating test data, but entity page settings were still updated."
);
}
};
await regenerateTestData();
handleClose();
}, [
generateTestData,
handleClose,
pageName,
selectedScope,
updateStreamScope,
]);

const updateSelection: updateScopeFieldFactory = useCallback(
(streamScopeField: keyof StreamScope) => (selectedIds: string[]) => {
if (selectedIds.length) {
setSelectedScope({
[streamScopeField]: selectedIds,
});
} else {
setSelectedScope({});
}
},
[]
);

const hasNoChanges = useMemo(() => {
const noScopeChanges = Object.keys(selectedScope).map((scope) =>
isEqual(sortBy(selectedScope[scope]), sortBy(originalScope[scope]))
);
return noScopeChanges.every((v) => v);
}, [selectedScope, originalScope]);

const modalBodyContent = useMemo(() => {
return (
<>
<div className="italic mb-4">
Use one of the optional fields below to edit which entities this page
can access. If multiple Stream Scopes were selected and all fields are
disabled, see a developer to edit the Stream Scope.
</div>
<StreamScopePicker
streamScope={selectedScope}
updateSelection={updateSelection}
/>
</>
);
}, [selectedScope, updateSelection]);

const resetSelectedScopeAndClose = useCallback(() => {
setSelectedScope(originalScope);
handleClose();
}, [handleClose, originalScope]);

return (
<DialogModal
isOpen={isOpen}
title="Edit Content Scope"
handleClose={resetSelectedScopeAndClose}
handleConfirm={onConfirm}
body={modalBodyContent}
confirmButtonText="Confirm"
isConfirmButtonDisabled={hasNoChanges}
/>
);
}
Loading

0 comments on commit 28997c6

Please sign in to comment.