-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #474 from GenomicDataInfrastructure/fix-card-ui
feat: extract card component for consistency + minor ui fixes
- Loading branch information
Showing
21 changed files
with
546 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// SPDX-FileCopyrightText: 2024 PNED G.I.E. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import Button from "@/components/Button"; | ||
import { useWindowSize } from "@/hooks"; | ||
import { useDatasetBasket } from "@/providers/DatasetBasketProvider"; | ||
import { SearchedDataset } from "@/services/discovery/types/dataset.types"; | ||
import { truncateDescription } from "@/utils/textProcessing"; | ||
import { faMinusCircle, faPlusCircle } from "@fortawesome/free-solid-svg-icons"; | ||
import Card, { CardItem } from "../../components/Card"; | ||
|
||
type DatasetCardProps = { | ||
dataset: SearchedDataset; | ||
cardItems: CardItem[]; | ||
}; | ||
|
||
function DatasetCard({ dataset, cardItems }: Readonly<DatasetCardProps>) { | ||
const screenSize = useWindowSize(); | ||
const truncatedDesc = dataset.description | ||
? truncateDescription(dataset.description, screenSize) | ||
: null; | ||
|
||
const { basket, addDatasetToBasket, removeDatasetFromBasket, isLoading } = | ||
useDatasetBasket(); | ||
|
||
const isInBasket = basket.some((ds) => ds.id === dataset.id); | ||
|
||
const toggleDatasetInBasket = (e: React.MouseEvent) => { | ||
e.preventDefault(); | ||
if (isInBasket) { | ||
removeDatasetFromBasket(dataset); | ||
} else { | ||
addDatasetToBasket(dataset); | ||
} | ||
}; | ||
|
||
const hasIdentifier = !!dataset.identifier; | ||
const buttonDisabled = isLoading || !hasIdentifier; | ||
|
||
return ( | ||
<Card | ||
url={`/datasets/${dataset.id}`} | ||
title={dataset.title} | ||
subTitles={dataset.themes?.map((theme) => theme.label)} | ||
description={truncatedDesc || "No description available"} | ||
cardItems={cardItems} | ||
keywords={dataset.keywords?.map((keyword) => keyword.label)} | ||
button={ | ||
<Button | ||
text={isInBasket ? "Remove from basket" : "Add to basket"} | ||
icon={isInBasket ? faMinusCircle : faPlusCircle} | ||
onClick={toggleDatasetInBasket} | ||
type={isInBasket ? "warning" : "primary"} | ||
disabled={buttonDisabled} | ||
flex={true} | ||
className="text-xs sm:text-base" | ||
/> | ||
} | ||
/> | ||
); | ||
} | ||
|
||
export default DatasetCard; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// SPDX-FileCopyrightText: 2024 PNED G.I.E. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { SearchedDataset } from "@/services/discovery/types/dataset.types"; | ||
import { createDatasetCardItems } from "../datasetCardItems"; | ||
|
||
describe("datasetCardItems", () => { | ||
beforeEach(() => { | ||
global.window = {} as unknown as Window & typeof globalThis; | ||
}); | ||
|
||
afterEach(() => { | ||
global.window = undefined as unknown as Window & typeof globalThis; | ||
}); | ||
|
||
it("should return dataset card items", () => { | ||
const dataset: SearchedDataset = { | ||
id: "1", | ||
title: "", | ||
description: "dataset1 description", | ||
themes: [{ value: "theme1", label: "theme1" }], | ||
keywords: [ | ||
{ | ||
label: "keyword1", | ||
value: "keyword1", | ||
}, | ||
{ | ||
label: "keyword2", | ||
value: "keyword2", | ||
}, | ||
], | ||
distributions: [ | ||
{ | ||
id: "r", | ||
title: "distribution1", | ||
description: "distribution1 description", | ||
format: { | ||
label: "format1", | ||
value: "format1", | ||
}, | ||
licenses: [], | ||
createdAt: "2022-01-01T00:00:00.000Z", | ||
modifiedAt: "2022-01-01T00:00:00.000Z", | ||
uri: "distribution1", | ||
}, | ||
], | ||
organization: { | ||
id: "1", | ||
title: "organization1", | ||
imageUrl: "", | ||
numberOfDatasets: 0, | ||
name: "organization1", | ||
description: "organization1 description", | ||
}, | ||
createdAt: "2024-03-01T00:00:00.000Z", | ||
modifiedAt: "", | ||
recordsCount: 21, | ||
}; | ||
|
||
const items = createDatasetCardItems(dataset); | ||
expect(items.length).toBe(5); | ||
expect(items[0].text).toBe("Created on 1 March 2024"); | ||
expect(items[1].text).toBe(""); | ||
expect(items[2].text).toBe("Published by organization1"); | ||
expect(items[3].text).toBe("1 Distribution"); | ||
expect(items[4].text).toBe("21 Records"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// SPDX-FileCopyrightText: 2024 PNED G.I.E. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { CardItem } from "@/components/Card"; | ||
import { SearchedDataset } from "@/services/discovery/types/dataset.types"; | ||
import { formatDate } from "@/utils/formatDate"; | ||
import { | ||
faBookBookmark, | ||
faCalendarAlt, | ||
faFile, | ||
faSyncAlt, | ||
faUser, | ||
} from "@fortawesome/free-solid-svg-icons"; | ||
|
||
export function createDatasetCardItems(dataset: SearchedDataset): CardItem[] { | ||
return [ | ||
{ | ||
text: | ||
(dataset.createdAt && `Created on ${formatDate(dataset.createdAt)}`) || | ||
"", | ||
icon: faCalendarAlt, | ||
}, | ||
{ | ||
text: | ||
(dataset.modifiedAt && | ||
`Modified on ${formatDate(dataset.modifiedAt)}`) || | ||
"", | ||
icon: faSyncAlt, | ||
}, | ||
{ | ||
text: | ||
(dataset.organization?.title && | ||
`Published by ${dataset.organization.title}`) || | ||
"", | ||
icon: faUser, | ||
}, | ||
{ | ||
text: | ||
(dataset.distributions?.length && | ||
(dataset.distributions.length === 1 | ||
? "1 Distribution" | ||
: `${dataset.distributions.length} Distributions`)) || | ||
"", | ||
icon: faFile, | ||
}, | ||
{ | ||
text: | ||
(dataset.recordsCount && | ||
(dataset.recordsCount === 1 | ||
? "1 Record" | ||
: `${dataset.recordsCount} Records`)) || | ||
"", | ||
icon: faBookBookmark, | ||
}, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-FileCopyrightText: 2024 PNED G.I.E. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { ListedApplication } from "@/types/application.types"; | ||
import Card from "@/components/Card"; | ||
import { createApplicationCardItems } from "./applicationCardItems"; | ||
|
||
export default function ApplicationCard({ | ||
application, | ||
}: Readonly<{ | ||
application: ListedApplication; | ||
}>) { | ||
return ( | ||
<Card | ||
url={`/applications/${application.id}`} | ||
title={application.title} | ||
subTitles={[application.currentState.split("/").pop() || ""]} | ||
description={application.description} | ||
cardItems={createApplicationCardItems(application)} | ||
/> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.