Skip to content

Commit

Permalink
fix(UI): glitches in ui fixed (#32)
Browse files Browse the repository at this point in the history
* fix(UI): glitches in ui fixed

* fix[API_KEY]: error checking invalid api keys on save

* refactor[UI]: error handling in UI fixed

* fix[file_upload]: refactor error handling state

* fix[UI]: small refactory for condition
  • Loading branch information
ArslanSaleem authored and gventuri committed Oct 23, 2024
1 parent a94157c commit 274d310
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
8 changes: 8 additions & 0 deletions backend/app/api/v1/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ def request_user_api_key(api_key_request: APIKeyRequest, db: Session = Depends(g
def save_user_api_key(
api_key_request: UpdateAPIKeyRequest, db: Session = Depends(get_db)
):

users = user_repository.get_users(db, n=1)

if not users:
raise HTTPException(status_code=404, detail="No User Exists!")

try:
requests.get_user_usage_data(api_key_request.api_key)
except Exception:
raise HTTPException(
status_code=400, detail="Invalid API Key"
)

user_repository.update_user_api_key(db, users[0].id, api_key_request.api_key)

return {
Expand Down
15 changes: 12 additions & 3 deletions frontend/src/app/(app)/projects/[projectId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export default function Project() {
hasNextPage,
isFetchingNextPage,
isLoading: isAssetsLoading,
isFetching: isAssetsFetching,
refetch: refetchAssets,
} = useInfiniteQuery({
queryKey: ["projectAssets", id],
queryFn: ({ pageParam = 1 }) => GetProjectAssets(id, pageParam, pageSize),
Expand Down Expand Up @@ -200,11 +202,11 @@ export default function Project() {

const handleFileUpload = async (fileList: FileList | null) => {
if (fileList) {
setUploadingFile(true);
const files = Array.from(fileList);
setUploadingFiles((prev) => [...prev, ...files]);

try {
setUploadingFile(true);
for (const file of files) {
const response = await AddProjectAsset(id, file);

Expand All @@ -216,11 +218,12 @@ export default function Project() {
setUploadingFile(false);
setUploadingFiles([]);
setUploadedFiles([]);
queryClient.invalidateQueries({ queryKey: ["projectAssets"] });
refetchAssets();
} catch (error) {
console.error("Error uploading files:", error);
setUploadingFiles([]);
setUploadedFiles([]);
setUploadingFile(false);
}
}
};
Expand Down Expand Up @@ -300,6 +303,12 @@ export default function Project() {
}
}, [inView, fetchNextPage, hasNextPage]);

const shouldShowDragAndDrop =
!uploadingFile &&
!isAssetsLoading &&
!isAssetsFetching &&
(!assets || assets.length === 0);

return (
<>
<Head>
Expand Down Expand Up @@ -370,7 +379,7 @@ export default function Project() {

{activeTab === "assets" && (
<>
{!uploadingFile && assets && assets?.length === 0 ? (
{shouldShowDragAndDrop ? (
<DragAndDrop
onFileSelect={handleFileUpload}
accept={[".pdf", "application/pdf"]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import Head from "next/head";
import { useParams } from "next/navigation";
import Breadcrumb from "@/components/ui/Breadcrumb";
import { Download } from "lucide-react";
import { ProcessDetailsResponse, ProcessStatus } from "@/interfaces/processes";
import {
ProcessData,
ProcessDetailsResponse,
ProcessStatus,
} from "@/interfaces/processes";
import { Column, Table } from "@/components/ui/Table";
import Label from "@/components/ui/Label";
import DateLabel from "@/components/ui/Date";
Expand All @@ -18,6 +22,8 @@ import { truncateTextFromCenter } from "@/lib/utils";
import { useQuery } from "@tanstack/react-query";
import PageLoader from "@/components/ui/PageLoader";
import { Card } from "@/components/ui/Card";
import File from "@/components/FileIconCard";
import { useRouter } from "next/navigation";

const statusLabel = (process: ProcessDetailsResponse) => {
switch (process.status) {
Expand Down Expand Up @@ -68,6 +74,7 @@ const columns: Column<ProcessDetailsResponse>[] = [

export default function Process() {
const params = useParams();
const router = useRouter();
const processId = (params?.processId as string) || "";
const projectId = (params?.projectId as string) || "";
const {
Expand Down Expand Up @@ -97,6 +104,10 @@ export default function Process() {
const project = processResponse?.data?.data?.[0]?.process?.project;
const process = processResponse?.data?.data?.[0]?.process;

const handleFileClick = (process: ProcessData) => {
router.push(`/projects/${projectId}/processes/${process.id}/csv`);
};

useEffect(() => {
if (isError) {
setErrorMessage("Something went wrong fetching process");
Expand Down Expand Up @@ -134,6 +145,15 @@ export default function Process() {
</div>
)}

<div className="pb-4">
<File
key={process?.id}
name={`${process?.name}.csv`}
type="spreadsheet"
onClick={() => handleFileClick(process)}
/>
</div>

{isLoading ? (
<PageLoader />
) : errorMessage ? (
Expand Down
18 changes: 13 additions & 5 deletions frontend/src/app/(app)/projects/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,19 @@ export default function Projects() {

const { data: response, isLoading } = useQuery({
queryKey: ["projects", page, pageSize],
queryFn: () => GetProjects(page, pageSize),
queryFn: async () => {
try {
const resp = await GetProjects(page, pageSize);
if (resp?.data?.data.length === 0) {
newProject();
}
return resp;
} catch (error) {
console.error("Failed to fetch projects:", error);
toast.error("Failed to fetch projects. Please try again.");
throw error;
}
},
});

const projects: ProjectData[] = response?.data?.data || [];
Expand All @@ -78,10 +90,6 @@ export default function Projects() {
router.push("/projects/new");
};

if (!isLoading && projects?.length === 0) {
newProject();
}

const breadcrumbItems = [{ label: "Projects", href: "/" }];

const viewOptions = [
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/api-key-setup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const ApiKeySetup: React.FC = () => {
router.push("/"); // Redirect to the main app
} catch (error) {
console.error("Error saving API key:", error);
toast.error("Failed to save API key. Please try again.");
toast.error("Invalid API Key. Please try again.");
}
};

Expand Down

0 comments on commit 274d310

Please sign in to comment.