@akinon/pz-one-click-checkout 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +15 -0
- package/.prettierrc +13 -0
- package/CHANGELOG.md +7 -0
- package/README.md +61 -0
- package/package.json +24 -0
- package/src/endpoints.ts +37 -0
- package/src/index.ts +1 -0
- package/src/one-click-checkout-buttons/index.tsx +131 -0
package/.gitattributes
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
*.js text eol=lf
|
|
2
|
+
*.jsx text eol=lf
|
|
3
|
+
*.ts text eol=lf
|
|
4
|
+
*.tsx text eol=lf
|
|
5
|
+
*.json text eol=lf
|
|
6
|
+
*.md text eol=lf
|
|
7
|
+
|
|
8
|
+
.eslintignore text eol=lf
|
|
9
|
+
.eslintrc text eol=lf
|
|
10
|
+
.gitignore text eol=lf
|
|
11
|
+
.prettierrc text eol=lf
|
|
12
|
+
.yarnrc text eol=lf
|
|
13
|
+
|
|
14
|
+
* text=auto
|
|
15
|
+
|
package/.prettierrc
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"bracketSameLine": false,
|
|
3
|
+
"tabWidth": 2,
|
|
4
|
+
"singleQuote": true,
|
|
5
|
+
"jsxSingleQuote": false,
|
|
6
|
+
"bracketSpacing": true,
|
|
7
|
+
"semi": true,
|
|
8
|
+
"useTabs": false,
|
|
9
|
+
"arrowParens": "always",
|
|
10
|
+
"endOfLine": "lf",
|
|
11
|
+
"proseWrap": "never",
|
|
12
|
+
"trailingComma": "none"
|
|
13
|
+
}
|
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# pz-one-click-checkout
|
|
2
|
+
|
|
3
|
+
### Install the npm package
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
# For latest version
|
|
7
|
+
|
|
8
|
+
yarn add git+ssh://git@bitbucket.org:akinonteam/pz-one-click-checkout.git
|
|
9
|
+
|
|
10
|
+
# For specific version
|
|
11
|
+
|
|
12
|
+
yarn add git+ssh://git@bitbucket.org:akinonteam/pz-one-click-checkout.git#COMMIT_HASH
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Next Config Configuration
|
|
17
|
+
|
|
18
|
+
**next.config.js**
|
|
19
|
+
|
|
20
|
+
```javascript
|
|
21
|
+
transpilePackages: ['pz-one-click-checkout'],
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### tailwind.config.js Configuration
|
|
25
|
+
|
|
26
|
+
**tailwind.config.js**
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
module.exports = {
|
|
30
|
+
content: ["..", "...", "./node_modules/pz-*/src/**/*.{js,ts,jsx,tsx}"],
|
|
31
|
+
};
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
### Usage in plugin module system
|
|
36
|
+
```javascript
|
|
37
|
+
import PluginModule, { Component } from '@akinon/next/components/plugin-module';
|
|
38
|
+
|
|
39
|
+
<PluginModule
|
|
40
|
+
component={Component.OneClickCheckoutButtons}
|
|
41
|
+
props={{
|
|
42
|
+
addProductToBasket: false,
|
|
43
|
+
product: data.product,
|
|
44
|
+
translations: {
|
|
45
|
+
buttonText: 'Pay with AKIFAST'
|
|
46
|
+
}
|
|
47
|
+
}}
|
|
48
|
+
/>
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Props
|
|
52
|
+
|
|
53
|
+
| Properties | Type | Description | Default |
|
|
54
|
+
| --------------- | --------- | ------------------------------------------------------------------ | ------- |
|
|
55
|
+
| product | `object` | Product information. |
|
|
56
|
+
| productQuantity | `number` | It is used to determine the quantity of the product to be added to the cart. |
|
|
57
|
+
| clearBasket | `boolean` | It is used when the basket needs to be cleaned while adding the product. | `true` |
|
|
58
|
+
| isDisabled | `boolean` | Button click control is provided. | `false` |
|
|
59
|
+
| className | `string` | The CSS class to apply to the button. | `bg-[#5381f7] px-7 py-2 w-full text-white text-sm font-semibold hover:bg-[#547ee7]`|
|
|
60
|
+
| translations.buttonText | `string` | The text of the button. | `ProviderName Checkout`|
|
|
61
|
+
| addBeforeClick | `function`| Can be used for checks before the Click event. The function should return boolean as value.|
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@akinon/pz-one-click-checkout",
|
|
3
|
+
"version": "1.17.0",
|
|
4
|
+
"main": "./src/index.ts",
|
|
5
|
+
"module": "./src/index.ts",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"peerDependencies": {
|
|
8
|
+
"react": "^18.0.0",
|
|
9
|
+
"react-dom": "^18.0.0",
|
|
10
|
+
"@reduxjs/toolkit": "^1.8.3"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@types/node": "^18.7.8",
|
|
14
|
+
"@types/react": "^18.0.17",
|
|
15
|
+
"@types/react-dom": "^18.0.6",
|
|
16
|
+
"react": "^18.2.0",
|
|
17
|
+
"react-dom": "^18.2.0",
|
|
18
|
+
"typescript": "^4.7.4",
|
|
19
|
+
"@reduxjs/toolkit": "^1.8.3",
|
|
20
|
+
"express": "4.17.3",
|
|
21
|
+
"node-fetch": "2.6.7",
|
|
22
|
+
"@types/express": "4.17.13"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/endpoints.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { api } from '@akinon/next/data/client/api';
|
|
2
|
+
import { buildClientRequestUrl } from '@akinon/next/utils';
|
|
3
|
+
import { checkout } from '@akinon/next/data/urls';
|
|
4
|
+
|
|
5
|
+
export const checkoutProviderApi = api.injectEndpoints({
|
|
6
|
+
endpoints: (build) => ({
|
|
7
|
+
getCheckoutProviders: build.query<any, void>({
|
|
8
|
+
query: () => ({
|
|
9
|
+
url: buildClientRequestUrl(checkout.getCheckoutProviders)
|
|
10
|
+
})
|
|
11
|
+
}),
|
|
12
|
+
getCheckoutProvidersIndex: build.mutation<any, void>({
|
|
13
|
+
query: () => ({
|
|
14
|
+
url: buildClientRequestUrl(checkout.getCheckoutProvidersIndex),
|
|
15
|
+
method: 'POST'
|
|
16
|
+
})
|
|
17
|
+
}),
|
|
18
|
+
setCheckoutProviders: build.mutation({
|
|
19
|
+
query: (pk: number) => ({
|
|
20
|
+
url: buildClientRequestUrl(checkout.setCheckoutProvider, {
|
|
21
|
+
useFormData: true
|
|
22
|
+
}),
|
|
23
|
+
method: 'POST',
|
|
24
|
+
body: {
|
|
25
|
+
checkout_provider: String(pk)
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
}),
|
|
30
|
+
overrideExisting: true
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export const {
|
|
34
|
+
useGetCheckoutProvidersQuery,
|
|
35
|
+
useGetCheckoutProvidersIndexMutation,
|
|
36
|
+
useSetCheckoutProvidersMutation } =
|
|
37
|
+
checkoutProviderApi;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './one-click-checkout-buttons';
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
|
+
import clsx from 'clsx';
|
|
3
|
+
import { twMerge } from 'tailwind-merge';
|
|
4
|
+
import {
|
|
5
|
+
basketApi,
|
|
6
|
+
useClearBasketMutation
|
|
7
|
+
} from '@akinon/next/data/client/basket';
|
|
8
|
+
import { useAppDispatch } from '@akinon/next/redux/hooks';
|
|
9
|
+
import { Product } from '@akinon/next/types';
|
|
10
|
+
import { useAddProductToBasket } from 'hooks';
|
|
11
|
+
import {
|
|
12
|
+
useGetCheckoutProvidersQuery,
|
|
13
|
+
useGetCheckoutProvidersIndexMutation,
|
|
14
|
+
useSetCheckoutProvidersMutation
|
|
15
|
+
} from '../endpoints';
|
|
16
|
+
|
|
17
|
+
type ProvidersType = [
|
|
18
|
+
{
|
|
19
|
+
pk: number;
|
|
20
|
+
name: string;
|
|
21
|
+
slug: string;
|
|
22
|
+
}
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
export interface OneClickCheckoutButtonsProps {
|
|
26
|
+
product?: Product;
|
|
27
|
+
productQuantity?: number;
|
|
28
|
+
clearBasket?: boolean;
|
|
29
|
+
isDisabled?: boolean;
|
|
30
|
+
className?: string;
|
|
31
|
+
translations: {
|
|
32
|
+
buttonText?: string;
|
|
33
|
+
};
|
|
34
|
+
addBeforeClick?: () => boolean;
|
|
35
|
+
openMiniBasket?: boolean;
|
|
36
|
+
providers?: ProvidersType[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const OneClickCheckoutButtons = (
|
|
40
|
+
props: OneClickCheckoutButtonsProps
|
|
41
|
+
) => {
|
|
42
|
+
const dispatch = useAppDispatch();
|
|
43
|
+
const {
|
|
44
|
+
product,
|
|
45
|
+
productQuantity = 1,
|
|
46
|
+
clearBasket = false,
|
|
47
|
+
isDisabled = false,
|
|
48
|
+
className,
|
|
49
|
+
translations,
|
|
50
|
+
openMiniBasket = false,
|
|
51
|
+
addBeforeClick,
|
|
52
|
+
providers = []
|
|
53
|
+
} = props;
|
|
54
|
+
|
|
55
|
+
const [disabled, setDisabled] = useState(isDisabled);
|
|
56
|
+
const [clearBasketAction] = useClearBasketMutation();
|
|
57
|
+
const [addProductToBasketAction] = useAddProductToBasket();
|
|
58
|
+
const [checkoutProvidersIndex] = useGetCheckoutProvidersIndexMutation();
|
|
59
|
+
const [setCheckoutProvider] = useSetCheckoutProvidersMutation();
|
|
60
|
+
const { data:checkoutProviders } = useGetCheckoutProvidersQuery(null, { skip: providers.length > 0 });
|
|
61
|
+
const providersList = (providers.length > 0 ? providers : checkoutProviders) ?? [];
|
|
62
|
+
|
|
63
|
+
const handleOnClick = async (e, value) => {
|
|
64
|
+
e.preventDefault();
|
|
65
|
+
e.stopPropagation();
|
|
66
|
+
|
|
67
|
+
if (addBeforeClick) {
|
|
68
|
+
const isContinue = addBeforeClick();
|
|
69
|
+
if(!isContinue) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
setDisabled(true);
|
|
75
|
+
|
|
76
|
+
if (clearBasket) {
|
|
77
|
+
await clearBasketAction()
|
|
78
|
+
.unwrap()
|
|
79
|
+
.then((basket) =>
|
|
80
|
+
dispatch(
|
|
81
|
+
basketApi.util.updateQueryData(
|
|
82
|
+
'getBasket',
|
|
83
|
+
undefined,
|
|
84
|
+
(draftBasket) => {
|
|
85
|
+
Object.assign(draftBasket, basket);
|
|
86
|
+
}
|
|
87
|
+
)
|
|
88
|
+
)
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (product) {
|
|
93
|
+
await addProductToBasketAction({
|
|
94
|
+
product: product?.pk,
|
|
95
|
+
quantity: productQuantity,
|
|
96
|
+
attributes: {},
|
|
97
|
+
shouldOpenMiniBasket: openMiniBasket
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
await checkoutProvidersIndex();
|
|
102
|
+
await setCheckoutProvider(value.pk);
|
|
103
|
+
|
|
104
|
+
setDisabled(false);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
<div className="w-full mt-4">
|
|
109
|
+
|
|
110
|
+
{providersList.map((value) => {
|
|
111
|
+
return (
|
|
112
|
+
<button
|
|
113
|
+
key={value.slug}
|
|
114
|
+
onClick={(e) => handleOnClick(e, value)}
|
|
115
|
+
className={twMerge(
|
|
116
|
+
clsx(
|
|
117
|
+
'bg-[#5381f7] px-7 py-2 w-full text-white text-sm font-semibold hover:bg-[#547ee7]',
|
|
118
|
+
className
|
|
119
|
+
)
|
|
120
|
+
)}
|
|
121
|
+
disabled={disabled}
|
|
122
|
+
>
|
|
123
|
+
{translations?.buttonText
|
|
124
|
+
? translations.buttonText
|
|
125
|
+
: `${value.name} Checkout`}
|
|
126
|
+
</button>
|
|
127
|
+
);
|
|
128
|
+
})}
|
|
129
|
+
</div>
|
|
130
|
+
);
|
|
131
|
+
};
|