From 5a1be434cb2df0bbb20ba20032a4f8feab2b87fc Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Wed, 25 Oct 2023 09:07:34 +0100 Subject: [PATCH] Add functionality to load your own version --- .../CreateVersion/CreateVersion.tsx | 78 ++++++++++++++++--- 1 file changed, 67 insertions(+), 11 deletions(-) diff --git a/src/components/CreateVersion/CreateVersion.tsx b/src/components/CreateVersion/CreateVersion.tsx index eeb1fc312..c206af64b 100644 --- a/src/components/CreateVersion/CreateVersion.tsx +++ b/src/components/CreateVersion/CreateVersion.tsx @@ -1,9 +1,10 @@ -import { useState } from 'react'; -import { httpPost } from '../../lib/http'; +import { useEffect, useState } from 'react'; +import { httpGet, httpPost } from '../../lib/http'; import { useToast } from '../../hooks/use-toast'; import { isLoggedIn } from '../../lib/jwt'; -import { GitFork, Layers2, Loader2 } from 'lucide-react'; +import { GitFork, Layers2, Loader2, Map } from 'lucide-react'; import { showLoginPopup } from '../../lib/popup'; +import type { RoadmapDocument } from '../CustomRoadmap/CreateRoadmap/CreateRoadmapModal.tsx'; type CreateVersionProps = { roadmapId: string; @@ -13,11 +14,39 @@ export function CreateVersion(props: CreateVersionProps) { const { roadmapId } = props; const toast = useToast(); - const [isLoading, setIsLoading] = useState(false); + const [isLoading, setIsLoading] = useState(true); + const [isCreating, setIsCreating] = useState(false); const [isConfirming, setIsConfirming] = useState(false); + const [userVersion, setUserVersion] = useState(); + + async function loadMyVersion() { + if (!isLoggedIn()) { + return; + } + + setIsLoading(true); + const { response, error } = await httpGet( + `${import.meta.env.PUBLIC_API_URL}/v1-get-my-version/${roadmapId}`, + {}, + ); + + if (error || !response) { + setIsLoading(false); + return; + } + + setIsLoading(false); + setUserVersion(response); + } + + useEffect(() => { + loadMyVersion().finally(() => { + setIsLoading(false); + }); + }, []); async function createVersion() { - if (isLoading || !roadmapId) { + if (isCreating || !roadmapId) { return; } @@ -26,14 +55,14 @@ export function CreateVersion(props: CreateVersionProps) { return; } - setIsLoading(true); + setIsCreating(true); const { response, error } = await httpPost<{ roadmapId: string }>( `${import.meta.env.PUBLIC_API_URL}/v1-create-version/${roadmapId}`, {}, ); if (error || !response) { - setIsLoading(false); + setIsCreating(false); toast.error(error?.message || 'Failed to create version'); return; } @@ -42,10 +71,30 @@ export function CreateVersion(props: CreateVersionProps) { import.meta.env.PUBLIC_EDITOR_APP_URL }/${response?.roadmapId}`; - setIsLoading(false); + setIsCreating(false); window.open(roadmapEditorUrl, '_blank'); } + if (isLoading) { + return ( +
+ ); + } + + if (!isLoading && userVersion) { + return ( +
+ + + Visit your own version + +
+ ); + } + if (isConfirming) { return (

@@ -72,11 +121,18 @@ export function CreateVersion(props: CreateVersionProps) { return (