@@ -33,6 +43,7 @@ export const VoucherForm: React.FC = () => {
className="bg-[#000] text-[#fff] px-2 py-1 rounded mt-5 text-center h-10"
onClick={() => {
setVoucher(voucherValue);
+ checkVoucher(voucherValue);
}}
>
{loading && localCart?.extra?.voucher ? _t('loading') : _t('cart.useVoucher')}
@@ -44,12 +55,14 @@ export const VoucherForm: React.FC = () => {
onClick={() => {
setVoucherValue('');
setVoucher('');
+ setShowMessage('');
}}
>
{_t('delete')}
)}
+ {showMessage && {showMessage}
}
);
diff --git a/shared/application/ui/hooks/useLocalCart.ts b/shared/application/ui/hooks/useLocalCart.ts
index 3d85c2f..501ffec 100644
--- a/shared/application/ui/hooks/useLocalCart.ts
+++ b/shared/application/ui/hooks/useLocalCart.ts
@@ -1,6 +1,8 @@
'use client';
import { useLocalStorage, writeStorage } from '@rehooks/local-storage';
import { LocalCart } from '~/use-cases/contracts/LocalCart';
+import { ServiceAPI } from '~/use-cases/service-api';
+import { useAppContext } from '../app-context/provider';
const InitializeEmptyLocalCart = (): LocalCart => {
return {
@@ -18,6 +20,7 @@ export function useLocalCart() {
...cart,
});
};
+ const { state: appContextState } = useAppContext();
const isImmutable = () => {
return cart.state === 'placed';
@@ -106,5 +109,13 @@ export function useLocalCart() {
},
});
},
+ validateVoucher: async (voucher: string) => {
+ const api = ServiceAPI({
+ language: appContextState.language,
+ serviceApiUrl: appContextState.serviceApiUrl,
+ });
+
+ return await api.validateVoucher(voucher);
+ },
};
}
diff --git a/shared/application/use-cases/contracts/RemoteCart.ts b/shared/application/use-cases/contracts/RemoteCart.ts
index 5ce3496..dacc855 100644
--- a/shared/application/use-cases/contracts/RemoteCart.ts
+++ b/shared/application/use-cases/contracts/RemoteCart.ts
@@ -17,4 +17,14 @@ export type Cart = {
};
state: 'cart' | 'placed' | 'ordered';
orderId?: string;
+ context?: {
+ price: {
+ markets?: string[];
+ decimals: number;
+ selectedVariantIdentifier: string;
+ fallbackVariantIdentifiers: string[];
+ compareAtVariantIdentifier: string;
+ voucherCode?: string;
+ };
+ };
};
diff --git a/shared/application/use-cases/crystallize/read/validateVoucher.server.ts b/shared/application/use-cases/crystallize/read/validateVoucher.server.ts
new file mode 100644
index 0000000..9139128
--- /dev/null
+++ b/shared/application/use-cases/crystallize/read/validateVoucher.server.ts
@@ -0,0 +1,29 @@
+import type { ClientInterface } from '@crystallize/js-api-client';
+import { jsonToGraphQLQuery } from 'json-to-graphql-query';
+
+type Deps = {
+ apiClient: ClientInterface;
+};
+
+export const validateVoucher = async (voucher: string, { apiClient }: Deps) => {
+ const query = {
+ validateVoucher: {
+ __args: {
+ voucher,
+ },
+ isValid: true,
+ },
+ };
+
+ const rawQuery = jsonToGraphQLQuery({ query });
+
+ try {
+ const response = await apiClient.shopCartApi(rawQuery);
+ return response.validateVoucher.isValid;
+ } catch (exception: any) {
+ console.error('Failed to fetch cart', exception.message);
+ return {
+ valid: false,
+ };
+ }
+};
diff --git a/shared/application/use-cases/crystallize/write/editCart.ts b/shared/application/use-cases/crystallize/write/editCart.ts
index 7e7bfde..501c5bc 100644
--- a/shared/application/use-cases/crystallize/write/editCart.ts
+++ b/shared/application/use-cases/crystallize/write/editCart.ts
@@ -8,9 +8,10 @@ type Deps = {
type Input = {
items: Array<{ sku: string; quantity: number }>;
- context: Record