Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: display 'no data' if backend returns none in home page #147

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions ui/src/components/shared/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { getSearchUrl } from "@/utils/utils";

interface SearchBarProps {
placeholder?: string;
hide?: boolean;
className?: string;
}

const SearchBar: React.FC<SearchBarProps> = ({
placeholder = "Search",
hide = false,
className,
}) => {
const router = useRouter();
Expand All @@ -20,14 +22,18 @@ const SearchBar: React.FC<SearchBarProps> = ({
const { Search } = Input;

return (
<Search
onSearch={() => router.push(getSearchUrl({ search: val }, true))}
placeholder={placeholder}
enterButton
className={className}
value={val}
onChange={(e) => setVal(e?.target?.value)}
/>
<>
{!hide && (
<Search
onSearch={() => router.push(getSearchUrl({ search: val }, true))}
placeholder={placeholder}
enterButton
className={className}
value={val}
onChange={(e) => setVal(e?.target?.value)}
/>
)}
</>
);
};

Expand Down
23 changes: 16 additions & 7 deletions ui/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { Tabs } from "antd";
import { Tabs, Empty } from "antd";
import Image from "next/image";
import { GetServerSideProps } from "next";

Expand All @@ -15,8 +15,12 @@ interface HomePageProps {
}

const HomePage: React.FC<HomePageProps> = ({ count, facets, query }) => {
const journals: Journal[] = facets?._filter_journal?.journal?.buckets;
const partners: Country[] = facets?._filter_country?.country?.buckets;
const journals: Journal[] = facets
? facets?._filter_journal?.journal?.buckets
: [];
const partners: Country[] = facets
? facets?._filter_country?.country?.buckets
: [];

const tabItems = [
{
Expand Down Expand Up @@ -48,10 +52,15 @@ const HomePage: React.FC<HomePageProps> = ({ count, facets, query }) => {
Search <b>{count} Open Access</b> articles:
</p>
<SearchBar
hide={count == 0}
placeholder="Type and press enter to search"
className="home-searchbar mb-6"
/>
<Tabs type="card" items={tabItems} />
{journals.length > 0 || partners.length > 0 ? (
<Tabs type="card" items={tabItems} />
) : (
<Empty />
)}
</div>
</div>
</>
Expand All @@ -60,11 +69,11 @@ const HomePage: React.FC<HomePageProps> = ({ count, facets, query }) => {

export const getServerSideProps: GetServerSideProps = async () => {
const query = { search: "" };

const res = await fetch(`${getApiUrl() + getSearchUrl(query)}`, authToken);
const { count, facets } = (await res?.json()) as Response;

return { props: { count, facets, query } };
const countValue = { count: count || 0 };
const facetsValue = { facets: facets || null };
return { props: Object.assign(countValue, facetsValue, query) };
};

export default HomePage;