@lasterp/shared 1.0.0-beta.2 → 1.0.0-beta.21

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.
@@ -1,4 +1,41 @@
1
1
  "use client";
2
+ // src/catalog/schema.ts
3
+ import { z } from "zod";
4
+ var ATTRIBUTES = ["Colour", "Storage", "Memory", "Screen"];
5
+ var colourFormSchema = z.object({
6
+ color: z.string().min(1, "Color is required"),
7
+ color_hex: z.string().optional(),
8
+ color_system: z.string().optional(),
9
+ color_system_hex: z.string().optional(),
10
+ images: z.array(z.string()).default([])
11
+ });
12
+ var screenFormSchema = z.object({
13
+ screen: z.string().min(1, "Screen is required"),
14
+ height: z.number().positive().optional(),
15
+ width: z.number().positive().optional(),
16
+ weight: z.number().positive().optional()
17
+ });
18
+ var modelFormSchema = z.object({
19
+ model: z.string().min(1, "Model is required"),
20
+ sim_card_type: z.string().optional(),
21
+ connectivity: z.array(z.string())
22
+ });
23
+ var itemFormSchema = z.object({
24
+ item_code: z.string().min(1, "Item is required"),
25
+ item_group: z.string().optional(),
26
+ brand: z.string().optional(),
27
+ os: z.string().optional(),
28
+ release_year: z.number().int().min(1900).max(2100).optional(),
29
+ attributes: z.array(z.enum(ATTRIBUTES)),
30
+ colours: z.array(colourFormSchema),
31
+ storages: z.array(z.string()),
32
+ memories: z.array(z.string()),
33
+ screens: z.array(screenFormSchema),
34
+ models: z.array(modelFormSchema),
35
+ width: z.number().positive().optional(),
36
+ height: z.number().positive().optional(),
37
+ weight: z.number().positive().optional()
38
+ });
2
39
  // src/frappe/provider.tsx
3
40
  import { useMemo, createContext } from "react";
4
41
  import { FrappeApp } from "frappe-js-sdk";
@@ -58,7 +95,7 @@ var FrappeProvider = ({
58
95
  children,
59
96
  customHeaders
60
97
  }) => {
61
- const frappeConfig = useMemo(() => {
98
+ const config = useMemo(() => {
62
99
  const frappe = new FrappeApp(url, tokenParams, undefined, customHeaders);
63
100
  return {
64
101
  url,
@@ -72,7 +109,7 @@ var FrappeProvider = ({
72
109
  };
73
110
  }, [url, tokenParams, enableSocket, socketPort, siteName, customHeaders]);
74
111
  return /* @__PURE__ */ jsx(FrappeContext.Provider, {
75
- value: frappeConfig,
112
+ value: config,
76
113
  children
77
114
  });
78
115
  };
@@ -161,7 +198,15 @@ var useFrappeGetCall = (method, params, options, type = "GET") => {
161
198
  const queryKey = ["frappeCall", type, method, url];
162
199
  const { data, error, isLoading, isFetching, refetch } = useQuery2({
163
200
  queryKey,
164
- queryFn: type === "GET" ? () => call.get(method, params) : () => call.post(method, params),
201
+ queryFn: async () => {
202
+ if (type === "GET") {
203
+ const response = await call.get(method, params);
204
+ return response.message;
205
+ } else {
206
+ const response = await call.post(method, params);
207
+ return response.message;
208
+ }
209
+ },
165
210
  enabled: (options?.enabled ?? true) && !!method,
166
211
  staleTime: options?.staleTime,
167
212
  retry: options?.retry ?? false
@@ -374,7 +419,7 @@ var useFrappeGetDoc = (doctype, name, options) => {
374
419
  const { data, error, isLoading, isFetching, refetch } = useQuery4({
375
420
  queryKey,
376
421
  queryFn: () => db.getDoc(doctype, name),
377
- enabled: !!name,
422
+ enabled: options?.enabled ?? true,
378
423
  staleTime: options?.staleTime ?? 5 * 60 * 1000,
379
424
  retry: options?.retry ?? false
380
425
  });
@@ -465,6 +510,29 @@ var useFrappeUpdateDoc = () => {
465
510
  reset
466
511
  };
467
512
  };
513
+ // src/frappe/hooks/meta.ts
514
+ import { useContext as useContext12 } from "react";
515
+ import { useQuery as useQuery6 } from "@tanstack/react-query";
516
+ var useFrappeGetMeta = (doctype, options) => {
517
+ const { url, call } = useContext12(FrappeContext);
518
+ const { data, error, isLoading, isFetching, refetch } = useQuery6({
519
+ queryKey: ["meta", url, doctype],
520
+ queryFn: async () => {
521
+ const response = await call.get("lasterp.api.get_meta", { doctype });
522
+ return response.message;
523
+ },
524
+ enabled: !!doctype,
525
+ staleTime: options?.staleTime ?? Infinity,
526
+ retry: options?.retry ?? false
527
+ });
528
+ return {
529
+ data,
530
+ error,
531
+ isLoading,
532
+ isFetching,
533
+ refetch
534
+ };
535
+ };
468
536
  // src/hooks/use-variant-selector/hook.ts
469
537
  import { useCallback as useCallback4, useMemo as useMemo2, useState as useState5 } from "react";
470
538
 
@@ -570,66 +638,72 @@ var useVariantSelector = (props) => {
570
638
  getOptions: (key) => options[key] || []
571
639
  };
572
640
  };
573
- // src/utils/char.ts
574
- import { camelize, decamelize, camelizeKeys, decamelizeKeys } from "humps";
575
- function equalsIgnoreCase(str1, str2) {
576
- return str1.localeCompare(str2, undefined, { sensitivity: "accent" }) === 0;
577
- }
578
- // src/context/server-provider.node.tsx
641
+ // src/locale/provider.tsx
579
642
  import {
643
+ createContext as createContext2,
580
644
  useCallback as useCallback5,
645
+ useContext as useContext13,
581
646
  useEffect as useEffect4,
582
- useRef
647
+ useState as useState6
583
648
  } from "react";
584
- import { useLogto } from "@logto/react";
585
649
  import { jsx as jsx2 } from "react/jsx-runtime";
586
- var TOKEN_STALE_MS = 50 * 60 * 1000;
587
- function ServerProvider({
588
- frappeUrl,
589
- siteName,
590
- resource,
650
+ function translate(message, replace, messages = {}) {
651
+ let translated = messages[message] ?? message;
652
+ replace?.forEach((val, i) => {
653
+ translated = translated.replace(`{${i}}`, String(val));
654
+ });
655
+ return translated;
656
+ }
657
+ var LocaleContext = createContext2((message) => message);
658
+ function LocaleProvider({
659
+ method,
660
+ language,
661
+ storage,
591
662
  children
592
663
  }) {
593
- const { getAccessToken, isAuthenticated } = useLogto();
594
- const tokenRef = useRef("");
595
- const fetchedAtRef = useRef(0);
596
- const isRefreshingRef = useRef(false);
597
- const refresh = useCallback5(async () => {
598
- const token = await getAccessToken(resource);
599
- if (token) {
600
- tokenRef.current = token;
601
- fetchedAtRef.current = Date.now();
602
- }
603
- }, [getAccessToken, resource]);
664
+ const [messages, setMessages] = useState6({});
604
665
  useEffect4(() => {
605
- if (isAuthenticated)
606
- refresh();
607
- }, [isAuthenticated, refresh]);
608
- const getToken = useCallback5(() => {
609
- if (!isRefreshingRef.current && fetchedAtRef.current > 0 && Date.now() - fetchedAtRef.current > TOKEN_STALE_MS) {
610
- isRefreshingRef.current = true;
611
- refresh().finally(() => {
612
- isRefreshingRef.current = false;
613
- });
614
- }
615
- return tokenRef.current;
616
- }, [refresh]);
617
- return /* @__PURE__ */ jsx2(FrappeProvider, {
618
- url: frappeUrl,
619
- siteName,
620
- customHeaders: siteName ? { "X-Frappe-Site-Name": siteName } : undefined,
621
- enableSocket: false,
622
- tokenParams: { useToken: true, token: getToken, type: "Bearer" },
666
+ if (!storage)
667
+ return;
668
+ const key = `locale:${language}`;
669
+ Promise.resolve(storage.getItem(key)).then((cached) => {
670
+ if (cached)
671
+ setMessages(JSON.parse(cached));
672
+ }).catch(() => {});
673
+ }, [language, storage]);
674
+ const { data, isLoading } = useFrappeGetCall(method, { lang: language }, { staleTime: Infinity });
675
+ useEffect4(() => {
676
+ if (!data)
677
+ return;
678
+ setMessages(data);
679
+ storage?.setItem(`locale:${language}`, JSON.stringify(data));
680
+ }, [data, language, storage]);
681
+ const __ = useCallback5((message, replace) => translate(message, replace, messages), [messages]);
682
+ if (isLoading && Object.keys(messages).length === 0)
683
+ return null;
684
+ return /* @__PURE__ */ jsx2(LocaleContext.Provider, {
685
+ value: __,
623
686
  children
624
687
  });
625
688
  }
689
+ function useLocale() {
690
+ const __ = useContext13(LocaleContext);
691
+ return { __ };
692
+ }
693
+ // src/utils/char.ts
694
+ import { camelize, decamelize, camelizeKeys, decamelizeKeys } from "humps";
695
+ function equalsIgnoreCase(str1, str2) {
696
+ return str1.localeCompare(str2, undefined, { sensitivity: "accent" }) === 0;
697
+ }
626
698
  export {
627
699
  useVariantSelector,
628
700
  useSearch,
701
+ useLocale,
629
702
  useFrappeUpdateDoc,
630
703
  useFrappePutCall,
631
704
  useFrappePrefetchDoc,
632
705
  useFrappePostCall,
706
+ useFrappeGetMeta,
633
707
  useFrappeGetDocList,
634
708
  useFrappeGetDocCount,
635
709
  useFrappeGetDoc,
@@ -642,12 +716,17 @@ export {
642
716
  useFrappeDeleteCall,
643
717
  useFrappeCreateDoc,
644
718
  useFrappeAuth,
719
+ screenFormSchema,
720
+ modelFormSchema,
721
+ itemFormSchema,
645
722
  equalsIgnoreCase,
646
723
  decamelizeKeys,
647
724
  decamelize,
725
+ colourFormSchema,
648
726
  camelizeKeys,
649
727
  camelize,
650
- ServerProvider,
728
+ LocaleProvider,
651
729
  FrappeProvider,
652
- FrappeContext
730
+ FrappeContext,
731
+ ATTRIBUTES
653
732
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lasterp/shared",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.21",
4
4
  "description": "Shared repo for webapp and native app",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -20,38 +20,33 @@
20
20
  "typescript": "^5.9.3"
21
21
  },
22
22
  "peerDependencies": {
23
+ "@tanstack/react-query": "^5.90.21",
23
24
  "react": ">=18.0.0",
24
25
  "react-dom": ">=18.0.0",
25
- "typescript": ">=4.5.0",
26
- "@tanstack/react-query": "^5.90.21",
27
- "@logto/react": "^4.0.13",
28
- "@logto/rn": "^1.1.0"
26
+ "typescript": ">=4.5.0"
29
27
  },
30
28
  "peerDependenciesMeta": {
31
29
  "typescript": {
32
30
  "optional": true
33
- },
34
- "@logto/react": {
35
- "optional": true
36
- },
37
- "@logto/rn": {
38
- "optional": true
39
31
  }
40
32
  },
41
33
  "type": "module",
42
34
  "exports": {
43
35
  ".": {
44
- "node": "./dist/node/index.js",
45
- "rn": "./dist/rn/index.js",
46
- "default": "./dist/node/index.js"
36
+ "types": "./dist/index.d.ts",
37
+ "import": "./dist/index.js",
38
+ "require": "./dist/index.cjs"
47
39
  },
48
40
  "./package.json": "./package.json"
49
41
  },
50
42
  "module": "./dist/index.js",
43
+ "main": "./dist/index.cjs",
51
44
  "types": "./dist/index.d.ts",
52
45
  "dependencies": {
53
46
  "frappe-js-sdk": "^1.12.0",
54
47
  "humps": "^2.0.1",
55
- "socket.io-client": "^4.8.3"
48
+ "socket.io-client": "^4.8.3",
49
+ "zod": "^4.3.6",
50
+ "zustand": "^5.0.11"
56
51
  }
57
52
  }