Skip to content

Commit

Permalink
Add loading (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
keithrfung authored Aug 21, 2024
1 parent 76d5a99 commit 84a1d5f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/app/files/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { mimeTypeFilters, sortOptions } from "../lib/definitions";

export default function FilesPage() {
const {
loading,
searchTerm,
handleSearch,
filter,
Expand Down Expand Up @@ -44,7 +45,7 @@ export default function FilesPage() {
className="border rounded p-2"
/>
</div>
<FileList files={files} missingText="No files found" />
<FileList files={files} missingText="No files found" loading={loading} />
</div>
);
}
10 changes: 9 additions & 1 deletion src/components/FileList/FileList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ import FileListItem from "./FileListItem";
export interface FileListProps {
files: GoogleDriveFile[];
missingText: string;
loading?: boolean;
}

export default function FileList({ files, missingText }: FileListProps) {
export default function FileList({
files,
missingText,
loading,
}: FileListProps) {
if (loading) {
return <p className="text-center text-gray-500">Loading...</p>;
}
return (
<div>
{files.length === 0 ? (
Expand Down
5 changes: 5 additions & 0 deletions src/hooks/useDriveList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const DEFAULT_SORT: SortOption = "name";
const SEARCH_DEBOUNCE_TIME = 2000;

export default function useDriveList() {
const [loading, setLoading] = useState(false);
const [files, setFiles] = useState<GoogleDriveFile[]>([]);
const [error, setError] = useState<string>();
const [searchTerm, setSearchTerm] = useState<string>();
Expand All @@ -24,6 +25,7 @@ export default function useDriveList() {
useEffect(() => {
const getFiles = async () => {
try {
setLoading(true);
const queryParams = new URLSearchParams();
if (hiddenSearchTerm) queryParams.append("search", hiddenSearchTerm);
if (filter && filter !== "all")
Expand All @@ -39,6 +41,8 @@ export default function useDriveList() {
setError(undefined);
} catch (error) {
setError((error as Error)?.message);
} finally {
setLoading(false);
}
};

Expand Down Expand Up @@ -68,6 +72,7 @@ export default function useDriveList() {
};

return {
loading,
searchTerm,
handleSearch,
filter,
Expand Down

0 comments on commit 84a1d5f

Please sign in to comment.