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

Feat: Added disable merch toggle button #143

Merged
merged 16 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
65 changes: 64 additions & 1 deletion apps/cms/src/admin/views/MerchOverview.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
import React from "react";
import React, { useEffect, useState, ChangeEvent } from "react";
import { Button } from "payload/components/elements";
import { AdminView } from "payload/config";
import ViewTemplate from "./ViewTemplate";
import StoreApi from "../../apis/store.api";

const MerchOverview: AdminView = ({ user, canAccessAdmin }) => {
const [displayText, setDisplayText] = useState<string>(
"We are currently preparing for the next merch sale. Please look forward to our email!"
);
const [isStoreDisabled, setIsStoreDisabled] = useState<boolean>(true);
const [loading, setLoading] = useState<boolean>(true);

const SHOW_DISPLAY_TEXT_INPUT = false;

useEffect(() => {
const fetchStoreStatus = async () => {
try {
const { disabled } = await StoreApi.getStoreStatus();
setIsStoreDisabled(disabled);
setLoading(false);
} catch (error) {
console.error(error);
setLoading(false);
}
};

// eslint-disable-next-line @typescript-eslint/no-floating-promises
fetchStoreStatus();
}, []);

const disableStore = async () => {
// TODO: Calls api to disable merch store
try {
setLoading(true);
await StoreApi.setStoreStatus({
displayText,
disabled: !isStoreDisabled,
});
setIsStoreDisabled(!isStoreDisabled);
setLoading(false);
} catch (error) {
console.error(error);
setLoading(false);
}
};
return (
<ViewTemplate
user={user}
Expand All @@ -19,6 +59,29 @@ const MerchOverview: AdminView = ({ user, canAccessAdmin }) => {
<Button el="link" to={"/admin"} buttonStyle="primary">
Go to Main Admin View
</Button>
<p style={{ paddingTop: 20 }}>{`Current state of merch store: ${
loading ? "..." : isStoreDisabled ? "Disabled" : "Live"
}`}</p>
{SHOW_DISPLAY_TEXT_INPUT && (
<textarea
value={displayText}
onChange={(e: ChangeEvent<HTMLTextAreaElement>) =>
setDisplayText(e.target.value)
}
placeholder="Enter display text"
rows={4}
cols={50}
/>
)}
<Button
limivann marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/no-misused-promises
onClick={disableStore}
disabled={loading}
buttonStyle="primary"
el="a"
>
Disable Store
</Button>
</ViewTemplate>
);
};
Expand Down
40 changes: 40 additions & 0 deletions apps/cms/src/apis/store.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { MerchSaleStatus } from "types";

class MerchStoreApi {
// eslint-disable-next-line @typescript-eslint/require-await
async setStoreStatus({
disabled,
displayText,
}: MerchSaleStatus): Promise<void> {
// TODO: set store status in the backend
console.log(disabled, displayText);
return new Promise((res, rej) => {
try {
setTimeout(() => {
res();
}, 1000);
} catch (error) {
rej(error);
}
});
}

async getStoreStatus(): Promise<MerchSaleStatus> {
// TODO: get store status from the backend
return new Promise((res, rej) => {
try {
setTimeout(() => {
res({
disabled: true,
displayText:
"We are currently preparing for the next merch sale. Please look forward to our email!",
});
}, 1000);
} catch (error) {
rej(error);
}
});
}
}

export default new MerchStoreApi();
1 change: 1 addition & 0 deletions apps/web/features/merch/constants/queryKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export enum QueryKeys {
ORDERS = "ORDERS",
EMAIL = "EMAIL",
CHECKOUT = "CHECKOUT",
STATUS = "STATUS",
}
15 changes: 15 additions & 0 deletions apps/web/features/merch/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Cart,
CheckoutRequest,
CheckoutResponse,
MerchSaleStatus,
PricedCart,
Product,
ProductsResponse,
Expand Down Expand Up @@ -85,6 +86,20 @@ export class Api {
email,
});
}

async getMerchSaleStatus(): Promise<MerchSaleStatus> {
// fetch merch status from backend, either true: enabled, false: disabled
// Simulating fetching data from backend and admin panel
return new Promise((res, rej) => {
setTimeout(() => {
res({
disabled: true,
displayText:
"We are currently preparing for the next merch sale. Please look forward to our email!",
});
}, 1000);
});
}
}

export const api = new Api();
Loading
Loading