diff --git a/frontend/src/App.test.tsx b/frontend/src/App.test.tsx index 574915db..ee5d3e8c 100644 --- a/frontend/src/App.test.tsx +++ b/frontend/src/App.test.tsx @@ -4,6 +4,7 @@ import { describe, it, expect, vi, beforeEach } from 'vitest'; import { BrowserRouter } from 'react-router-dom'; import App from './App'; import { FilesProvider } from './contexts/FilesContext'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; // Mock the imports vi.mock('./components/AppHeader/AppHeader.tsx', () => ({ @@ -29,11 +30,13 @@ describe('App component', () => { const renderComponent = () => { render( - - - - - + + + + + + + ); }; @@ -48,7 +51,7 @@ describe('App component', () => { }); it('displays regular content on subsequent visits', () => { - localStorage.setItem('hasVisited', 'true'); + localStorage.setItem('templates', JSON.stringify(['hello'])); renderComponent(); expect(screen.queryByTestId('first-time-exp')).not.toBeInTheDocument(); expect(screen.getByTestId('templates-index')).toBeInTheDocument(); diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 31b3daa8..316bcf11 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -9,21 +9,49 @@ import Comment from './assets/comment.svg'; import CSV from './assets/csv.svg'; import "./App.scss"; import { useFiles } from "./contexts/FilesContext.tsx"; +import { useQuery } from "@tanstack/react-query"; +import { TemplateAPI } from "./types/templates.ts"; function App() { const { pathname } = useLocation(); const navigate = useNavigate(); - const { setFiles } = useFiles(); - const [isFirstVisit, setIsFirstVisit] = useState(false); + const { setFiles, templates, setTemplates } = useFiles(); + const [hasMorethan1Template, setHasMoreThanOneTemplate] = useState(false); + const templateQuery = useQuery({ + queryKey: ["templates"], + queryFn: TemplateAPI.getTemplates, + }); useEffect(() => { - const hasVisited = localStorage.getItem("hasVisited"); - if (!hasVisited) { - setIsFirstVisit(true); - localStorage.setItem("hasVisited", "true"); + const getTemplates = async () => { + const templatesJSON = localStorage.getItem("templates") || "[]"; + if (templateQuery.data && templateQuery.data?.length > 0) { + setTemplates(templateQuery.data as []); + } else if (templatesJSON) { + setTemplates( + JSON.parse(templatesJSON).map((template) => ({ + ...template, + updatedAt: template.lastUpdated, + })), + ); + } else { + setTemplates([]); + } + }; + getTemplates(); + }, [templateQuery.data]); + + useEffect(() => { + if (templates.length > 0) { + setHasMoreThanOneTemplate(true); + } else { + setHasMoreThanOneTemplate(false); } + }, [templates.length]); + + useEffect(() => { setFiles([]); - }, []); + },[]); const navLinks = [ { text: "Annotate and Extract", url: "/" }, @@ -58,7 +86,7 @@ function App() {

Annotate and Extract

- {isFirstVisit ? ( + {!hasMorethan1Template ? (

Welcome to Report Vision diff --git a/frontend/src/components/ExtractUploadFile.scss b/frontend/src/components/ExtractUploadFile.scss index 407f9282..4e3e5853 100644 --- a/frontend/src/components/ExtractUploadFile.scss +++ b/frontend/src/components/ExtractUploadFile.scss @@ -57,7 +57,7 @@ p { display: flex; flex-direction: column; height: 150px; - width: 914px; + width: 100%; border-left: 5px solid #d63e04; background-color: #f4e3db; } diff --git a/frontend/src/components/TemplatesIndex/TemplatesIndex.tsx b/frontend/src/components/TemplatesIndex/TemplatesIndex.tsx index 21155a96..80e7cf62 100644 --- a/frontend/src/components/TemplatesIndex/TemplatesIndex.tsx +++ b/frontend/src/components/TemplatesIndex/TemplatesIndex.tsx @@ -1,18 +1,17 @@ -import { FC, useEffect, useState } from "react"; +import { FC, useEffect } from "react"; import { Button } from "@trussworks/react-uswds"; import { SortableTable } from "../SortableTable/SortableTable.tsx"; import { useNavigate } from "react-router-dom"; import extractImage from "../../assets/extract_image.svg"; -import { useQuery } from "@tanstack/react-query"; -import { TemplateAPI } from "../../types/templates.ts"; import usePagination from "../../hooks/use-pagination/index.ts"; +import { useFiles } from "../../contexts/FilesContext.tsx"; import './TemplatesIndex.scss' type TemplateIndexProps = unknown; export const TemplatesIndex: FC = () => { - const [templates, setTemplates] = useState([]); + const { templates, setTemplates } = useFiles(); const { currentItems, currentPage, @@ -24,29 +23,6 @@ export const TemplatesIndex: FC = () => { hasPreviousPage } = usePagination(templates, 10, 1); const navigate = useNavigate(); - // TODO: Pagination and sorting will be added later - const templateQuery = useQuery({ - queryKey: ["templates"], - queryFn: TemplateAPI.getTemplates, - }); - useEffect(() => { - const getTemplates = async () => { - const templatesJSON = localStorage.getItem("templates") || "[]"; - if (templateQuery.data && templateQuery.data?.length > 0) { - setTemplates(templateQuery.data as []); - } else if (templatesJSON) { - setTemplates( - JSON.parse(templatesJSON).map((template) => ({ - ...template, - updatedAt: template.lastUpdated, - })), - ); - } else { - setTemplates([]); - } - }; - getTemplates(); - }, [templateQuery.data]); useEffect(() => { const localStorageEvent = (event) => { diff --git a/frontend/src/contexts/FilesContext.tsx b/frontend/src/contexts/FilesContext.tsx index 49cdebd9..6daae40d 100644 --- a/frontend/src/contexts/FilesContext.tsx +++ b/frontend/src/contexts/FilesContext.tsx @@ -36,7 +36,9 @@ export interface FileType { interface FileContextType { files: File[]; + templates: any[]; selectedTemplates: TemplatePair[]; + setTemplates: (templates: any[]) => void; addFile: (file: File) => void; removeFile: (fileName: string) => void; clearFiles: () => void; @@ -49,6 +51,7 @@ const FilesContext = createContext(undefined); export const FilesProvider = ({ children }: { children: ReactNode }) => { const [files, setFiles] = useState([]); + const [templates, setTemplates] = useState([]); const [selectedTemplates, _setSelectedTemplates] = useState( [], ); @@ -85,8 +88,10 @@ export const FilesProvider = ({ children }: { children: ReactNode }) => { return (