summaryrefslogtreecommitdiff
path: root/src-migrate/libs/whatsappUrl.ts
diff options
context:
space:
mode:
authortrisusilo48 <tri.susilo@altama.co.id>2024-07-10 15:58:51 +0700
committertrisusilo48 <tri.susilo@altama.co.id>2024-07-10 15:58:51 +0700
commit2e3c726bc8217f3960cfecec44b81303b03de72b (patch)
tree1b85ced7f61f3e4c3f1f27b577b37aa161615065 /src-migrate/libs/whatsappUrl.ts
parent2b3bd9c0a454dbad69ce29cee877bfb1fca5dfa6 (diff)
parenta99bf6480eea556e53b85e6db45f3b8c2361e693 (diff)
Merge branch 'release' into development
# Conflicts: # src/pages/shop/product/variant/[slug].jsx
Diffstat (limited to 'src-migrate/libs/whatsappUrl.ts')
-rw-r--r--src-migrate/libs/whatsappUrl.ts48
1 files changed, 48 insertions, 0 deletions
diff --git a/src-migrate/libs/whatsappUrl.ts b/src-migrate/libs/whatsappUrl.ts
new file mode 100644
index 00000000..a3fcf8ad
--- /dev/null
+++ b/src-migrate/libs/whatsappUrl.ts
@@ -0,0 +1,48 @@
+import { getAuth } from './auth';
+
+const TEMPLATES = {
+ default: 'Bisa tolong bantu kebutuhan saya?',
+ product:
+ 'Saya mencari barang berikut:\n\n{{url}}\n\n```Brand: {{manufacture}}\nName: {{productName}}```',
+};
+
+interface WhatsappUrlProps {
+ template: keyof typeof TEMPLATES;
+ payload: any;
+ greeting?: boolean;
+ needLogin?: boolean;
+ fallbackUrl?: string;
+}
+
+export const whatsappUrl = ({
+ template,
+ payload,
+ greeting = true,
+ needLogin = true,
+ fallbackUrl,
+}: WhatsappUrlProps) => {
+ const auth = getAuth();
+
+ let greetingText = '';
+
+ if (needLogin && !auth) {
+ return fallbackUrl
+ ? `/login?next=${encodeURIComponent(fallbackUrl)}`
+ : '/login';
+ }
+
+ let result = TEMPLATES[template].replace(
+ /{{(.*?)}}/g,
+ (match, key) => payload[key] || ''
+ );
+
+ if (greeting && typeof auth === 'object') {
+ greetingText = `Halo Indoteknik.com, Saya ${auth.name} `;
+ if (auth.parentName) greetingText += `dari ${auth.parentName}`;
+ greetingText += '.\n\n';
+
+ result = greetingText + result;
+ }
+
+ return `https://wa.me/6281717181922?text=${encodeURIComponent(result)}`;
+};