Skip to content

Commit

Permalink
refactor product display logic for exams
Browse files Browse the repository at this point in the history
  • Loading branch information
abhigyanghosh30 committed Sep 28, 2023
1 parent f1b75b3 commit 0e58c7f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import {
} from "@canonical/react-components";
import classNames from "classnames";
import { currencyFormatter } from "advantage/react/utils";
import { Product } from "advantage/subscribe/blender/utils/utils";
import { Product } from "advantage/credentials/utils/utils";
import { useQuery } from "react-query";
import { getExamProducts } from "advantage/credentials/api/keys";

const CredExamShop = () => {
const ExamProductDescriptions = [
const ExamProductDescriptions: Product[] = [
{
id: "cue-linux-essentials",
name: "CUE Linux Essentials",
metadata: [
{
key: "description",
Expand All @@ -27,6 +28,7 @@ const CredExamShop = () => {
},
{
id: "cue-02-desktop",
name: "CUE Desktop",
metadata: [
{
key: "description",
Expand All @@ -37,6 +39,7 @@ const CredExamShop = () => {
},
{
id: "cue-03-server",
name: "CUE Server",
metadata: [
{
key: "description",
Expand Down Expand Up @@ -72,22 +75,18 @@ const CredExamShop = () => {
location.href = "/account/checkout";
};
useEffect(() => {
const tempExamProducts = [];
console.log(ExamData);
if(ExamData === undefined) {
if (ExamData === undefined) {
return;
}
for (const exam of ExamData) {
for (const examDescription of ExamProductDescriptions) {
for (const examDescription of ExamProductDescriptions) {
for (const exam of ExamData) {
if (exam.id === examDescription.id) {
exam.metadata = examDescription.metadata;
tempExamProducts.push(exam);
Object.assign(examDescription, exam);
}
}
}
setExamProducts(tempExamProducts);
console.log(ExamData);
console.log(tempExamProducts);
setExamProducts(ExamProductDescriptions);
console.log(ExamProductDescriptions);
}, [ExamData]);
if (isLoading) {
return <Spinner />;
Expand Down Expand Up @@ -118,7 +117,7 @@ const CredExamShop = () => {
value={examIndex}
checked={exam == examIndex}
onChange={handleChange}
disabled={examElement.private}
disabled={examElement.price === undefined}
/>
<span className="p-radio__label">
<RadioInput
Expand All @@ -127,7 +126,7 @@ const CredExamShop = () => {
checked={exam == examIndex}
value={examIndex}
onChange={handleChange}
disabled={examElement.private}
disabled={examElement.price === undefined}
/>
<hr />
<p style={{ paddingLeft: "1rem", paddingRight: "1rem" }}>
Expand All @@ -138,7 +137,7 @@ const CredExamShop = () => {
className="u-align--right"
style={{ paddingRight: "1rem" }}
>
{examElement.private
{examElement.price === undefined
? "Coming Soon!"
: "Price: " +
currencyFormatter.format(
Expand Down Expand Up @@ -171,7 +170,7 @@ const CredExamShop = () => {
checked={exam == examIndex}
value={examIndex}
onChange={handleChange}
disabled={examElement.private}
disabled={examElement.price === undefined}
/>
<span>
<p style={{ paddingLeft: "1rem", paddingRight: "1rem" }}>
Expand All @@ -183,7 +182,7 @@ const CredExamShop = () => {
className="u-align--right"
style={{ paddingRight: "1rem" }}
>
{examElement.private
{examElement.price === undefined
? "Coming Soon!"
: "Price: " +
currencyFormatter.format(examElement.price.value / 100)}
Expand All @@ -209,7 +208,7 @@ const CredExamShop = () => {
<Col size={3} small={2} style={{ display: "flex" }}>
<p className="p-heading--2" style={{ marginBlock: "auto" }}>
{currencyFormatter.format(
(ExamProducts[exam]?.price.value ?? 0) / 100 ?? 0
(ExamProducts[exam]?.price?.value ?? 0) / 100 ?? 0
)}
</p>
</Col>
Expand Down
13 changes: 13 additions & 0 deletions static/js/src/advantage/credentials/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { UserSubscriptionMarketplace } from "advantage/api/enum";

export type Product = {
longId?: string;
name: string;
price?: {
value: number;
currency: string;
};
id: string;
marketplace?: UserSubscriptionMarketplace;
metadata: Array<{ key: string; value: string }>;
};
1 change: 0 additions & 1 deletion static/js/src/advantage/subscribe/blender/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export type Product = {
id: ProductIDs;
productID: string;
marketplace: UserSubscriptionMarketplace;
metadata?: Array<{ key: string; value: string }>;
};

export type ProductListings = {
Expand Down

0 comments on commit 0e58c7f

Please sign in to comment.