Skip to content

Commit

Permalink
Add draft indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
constantgillet committed Apr 30, 2024
1 parent e6475c5 commit d7643e4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
8 changes: 7 additions & 1 deletion app/components/SaveTreeIndicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import { defaultTree } from "~/constants/defaultTree";

type SaveTreeIndicatorProps = {
templateId: string;
onSave?: () => void;
};

export const SaveTreeIndicator = (props: SaveTreeIndicatorProps) => {
const { templateId } = props;
const { templateId, onSave } = props;

const [isSaving, setIsSaving] = useState(false);
const [isSaved, setIsSaved] = useState(true);
Expand All @@ -37,6 +38,11 @@ export const SaveTreeIndicator = (props: SaveTreeIndicatorProps) => {
.then((response) => response.json())
.then((data) => {
setIsSaved(true);

if (data.changed) {
onSave?.();
}

setIsSaving(false);
})
.catch((error) => {
Expand Down
36 changes: 29 additions & 7 deletions app/components/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,19 @@ const toolsData = [
type TopBarProps = {
initalName: string;
templateId: string;
isDraft: boolean;
};

export const TopBar = ({ initalName, templateId }: TopBarProps) => {
export const TopBar = ({
initalName,
templateId,
isDraft: defaultIsDraft,
}: TopBarProps) => {
const selectedTool = useEditorStore((state) => state.selectedTool);
const setSelectedTool = useEditorStore((state) => state.setSelectedTool);

const [isDraft, setIsDraft] = useState(defaultIsDraft);

return (
<header
className={css({
Expand Down Expand Up @@ -139,11 +146,19 @@ export const TopBar = ({ initalName, templateId }: TopBarProps) => {
alignItems: "center",
})}
>
<SaveTreeIndicator templateId={templateId} />
<Tooltip content="Click on the Deploy button for setting it in production">
<Badge color="gray">Draft</Badge>
</Tooltip>
<DeployButton templateId={templateId} />
<SaveTreeIndicator
templateId={templateId}
onSave={() => setIsDraft(true)}
/>
{isDraft && (
<Tooltip content="Click on the Deploy button for setting it in production">
<Badge color="gray">Draft</Badge>
</Tooltip>
)}
<DeployButton
templateId={templateId}
onDeploy={() => setIsDraft(false)}
/>
</div>
</div>
</header>
Expand Down Expand Up @@ -268,14 +283,21 @@ const TemplateNameButton = ({
);
};

export const DeployButton = ({ templateId }: { templateId: string }) => {
export const DeployButton = ({
templateId,
onDeploy,
}: {
templateId: string;
onDeploy?: () => void;
}) => {
const [open, setOpen] = useState(false);
const fetcher = useFetcher<typeof updateTemplateToProdAction>();

useEffect(() => {
if (fetcher.data?.ok) {
setOpen(false);
toast.success("Template set in production");
onDeploy();
}
}, [fetcher.data]);

Expand Down
13 changes: 10 additions & 3 deletions app/routes/_app.editor.$templateId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ export async function loader({
tree: templateData?.tree as unknown as Tree,
};

return json({ template });
const isDraft =
JSON.stringify(template.tree) !== JSON.stringify(template.prodTree);

return json({ template, isDraft });
} catch (error) {
console.error(error);
throw new Error("Error fetching template");
Expand All @@ -66,7 +69,7 @@ export async function loader({

export default function Index() {
const container = useRef<HTMLDivElement>(null);
const { template } = useLoaderData<typeof loader>();
const { template, isDraft } = useLoaderData<typeof loader>();

const infiniteViewer = useRef<InfiniteViewer>(null);

Expand All @@ -89,7 +92,11 @@ export default function Index() {
overflow: "hidden",
}}
>
<TopBar initalName={template.name} templateId={template.id} />
<TopBar
initalName={template.name}
templateId={template.id}
isDraft={isDraft}
/>
<div
className={css({
flex: 1,
Expand Down
2 changes: 1 addition & 1 deletion app/routes/api.update-template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function action({ context, request }: ActionFunctionArgs) {
deleteFolder(folder);
console.log("TREE IS DIFFERENT, DELETING CACHE");

return json({ ok: true });
return json({ ok: true, changed: true });
} catch (error) {
console.error(error);
return json({ data: "error" }, { status: 500 });
Expand Down

0 comments on commit d7643e4

Please sign in to comment.