Skip to content

Commit

Permalink
chore(web): publish modal (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkumbobeaty authored Sep 6, 2023
1 parent 4ee1969 commit df854f8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default (
publishing?: publishingType,
publishStatus?: PublishStatus,
defaultAlias?: string,
onPublish?: (publishStatus: PublishStatus) => Promise<void>,
onPublish?: (alias: string | undefined, publishStatus: PublishStatus) => void | Promise<void>,
onClose?: () => void,
onAliasValidate?: (alias: string) => void,
onCopyToClipBoard?: () => void,
Expand Down Expand Up @@ -95,15 +95,17 @@ export default (

const handlePublish = useCallback(async () => {
if (!publishing) return;
const a = publishing !== "unpublishing" ? alias || generateAlias() : undefined;

const mode =
publishing === "unpublishing" ? "unpublished" : !searchIndex ? "limited" : "published";
await onPublish?.(mode);
await onPublish?.(a, mode);
if (publishing === "unpublishing") {
handleClose?.();
} else {
setStatusChange(true);
}
}, [onPublish, publishing, searchIndex, setStatusChange, handleClose]);
}, [alias, generateAlias, onPublish, publishing, searchIndex, setStatusChange, handleClose]);

return {
statusChanged,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Props = {
publishing?: publishingType;
validatingAlias?: boolean;
url?: string[];
onPublish: (publishStatus: PublishStatus) => Promise<void>;
onPublish: (alias: string | undefined, publishStatus: PublishStatus) => void | Promise<void>;
onClose?: () => void;
onCopyToClipBoard?: () => void;
onAliasValidate?: (alias: string) => void;
Expand Down Expand Up @@ -79,10 +79,9 @@ const PublishModal: React.FC<Props> = ({
const publishDisabled = useMemo(
() =>
loading ||
(publishing === "unpublishing" && publishStatus === "unpublished") ||
((publishing === "publishing" || publishing === "updating") &&
(!alias || !!validation || validatingAlias || !validAlias)),
[alias, loading, publishStatus, publishing, validation, validAlias, validatingAlias],
[alias, loading, publishing, validation, validAlias, validatingAlias],
);

const modalTitleText = useMemo(() => {
Expand Down Expand Up @@ -125,7 +124,12 @@ const PublishModal: React.FC<Props> = ({
button1={<Button text={secondaryButtonText} buttonType="secondary" onClick={handleClose} />}
button2={
!statusChanged && (
<Button text={primaryButtonText} disabled={publishDisabled} onClick={handlePublish} />
<Button
text={primaryButtonText}
buttonType="primary"
disabled={publishDisabled}
onClick={handlePublish}
/>
)
}
onClose={handleClose}>
Expand Down
12 changes: 9 additions & 3 deletions web/src/beta/features/Editor/tabs/publish/Nav/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { publishingType } from "./PublishModal";
import { type PublishStatus } from "./PublishModal/hooks";

export default ({ projectId }: { projectId?: string }) => {
const { useProjectQuery, useProjectAliasCheckLazyQuery, usePublishProject } = useProjectFetcher();
const {
useProjectQuery,
useProjectAliasCheckLazyQuery,
usePublishProject,
publishProjectLoading,
} = useProjectFetcher();
const { project } = useProjectQuery(projectId);

const [publishing, setPublishing] = useState<publishingType>("unpublishing");
Expand Down Expand Up @@ -55,10 +60,10 @@ export default ({ projectId }: { projectId?: string }) => {
}, [project?.publishmentStatus]);

const handleProjectPublish = useCallback(
async (publishStatus: PublishStatus) => {
async (alias: string | undefined, publishStatus: PublishStatus) => {
await usePublishProject(publishStatus, projectId, alias);
},
[projectId, alias, usePublishProject],
[projectId, usePublishProject],
);

const handleOpenProjectSettings = useCallback(() => {
Expand All @@ -78,6 +83,7 @@ export default ({ projectId }: { projectId?: string }) => {
return {
publishing,
publishStatus,
publishProjectLoading,
dropdownOpen,
modalOpen,
alias,
Expand Down
12 changes: 5 additions & 7 deletions web/src/beta/features/Editor/tabs/publish/Nav/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const Nav: React.FC<Props> = ({ projectId, selectedProjectType, onProjectTypeCha
alias,
validAlias,
validatingAlias,
publishProjectLoading,
handleModalOpen,
handleModalClose,
setDropdown,
Expand All @@ -51,6 +52,7 @@ const Nav: React.FC<Props> = ({ projectId, selectedProjectType, onProjectTypeCha
[publishStatus, t],
);

const checkPublished: boolean = publishStatus === "limited" || publishStatus === "published";
return (
<>
<StyledSecondaryNav>
Expand Down Expand Up @@ -90,13 +92,8 @@ const Nav: React.FC<Props> = ({ projectId, selectedProjectType, onProjectTypeCha
onClick: () => handleModalOpen("unpublishing"),
},
{
name: t("Publish"),
onClick: () =>
handleModalOpen(
publishStatus === "limited" || publishStatus === "published"
? "updating"
: "publishing",
),
name: checkPublished ? t("Update") : t("Publish"),
onClick: () => handleModalOpen(checkPublished ? "updating" : "publishing"),
},
{
name: t("Publishing Settings"),
Expand All @@ -109,6 +106,7 @@ const Nav: React.FC<Props> = ({ projectId, selectedProjectType, onProjectTypeCha
</StyledSecondaryNav>
<PublishModal
isVisible={modalOpen}
loading={publishProjectLoading}
publishing={publishing}
publishStatus={publishStatus}
url={config()?.published?.split("{}")}
Expand Down
10 changes: 7 additions & 3 deletions web/src/services/api/projectApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@ export default () => {
[createNewProject, createScene, setNotification, t],
);

const [publishProjectMutation] = useMutation(PUBLISH_PROJECT, {
refetchQueries: ["GetProject"],
});
const [publishProjectMutation, { loading: publishProjectLoading }] = useMutation(
PUBLISH_PROJECT,
{
refetchQueries: ["GetProject"],
},
);

const usePublishProject = useCallback(
async (s: PublishStatus, projectId?: string, alias?: string) => {
Expand Down Expand Up @@ -242,6 +245,7 @@ export default () => {
);

return {
publishProjectLoading,
useProjectQuery,
useProjectAliasCheckLazyQuery,
useCreateProject,
Expand Down

0 comments on commit df854f8

Please sign in to comment.