@headless-adminapp/app 0.0.17-alpha.0 → 0.0.17-alpha.3

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.
@@ -0,0 +1,8 @@
1
+ import { FC, PropsWithChildren } from 'react';
2
+ interface AppProviderProps {
3
+ appId: string;
4
+ loadingComponent: React.ReactNode;
5
+ notFoundComponent: React.ReactNode;
6
+ }
7
+ export declare const AppProvider: FC<PropsWithChildren<AppProviderProps>>;
8
+ export {};
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.AppProvider = void 0;
13
+ const jsx_runtime_1 = require("react/jsx-runtime");
14
+ const react_query_1 = require("@tanstack/react-query");
15
+ const react_1 = require("react");
16
+ const hooks_1 = require("../metadata/hooks");
17
+ const context_1 = require("./context");
18
+ const AppProvider = ({ children, appId, loadingComponent, notFoundComponent, }) => {
19
+ const experienceStore = (0, hooks_1.useExperienceStore)();
20
+ const appStore = (0, hooks_1.useAppStore)();
21
+ const { data: schemaMetadataList } = (0, react_query_1.useQuery)({
22
+ queryKey: ['experience-schema-metadata-list'],
23
+ queryFn: () => __awaiter(void 0, void 0, void 0, function* () {
24
+ return experienceStore.getExperienceSchemaMetadatList();
25
+ }),
26
+ initialData: [],
27
+ });
28
+ const { data: app, isLoading } = (0, react_query_1.useQuery)({
29
+ queryKey: ['experience-app', appId],
30
+ queryFn: () => __awaiter(void 0, void 0, void 0, function* () {
31
+ return appStore.getApp(appId);
32
+ }),
33
+ });
34
+ const schemaMetadataDic = (0, react_1.useMemo)(() => {
35
+ var _a;
36
+ return (_a = schemaMetadataList === null || schemaMetadataList === void 0 ? void 0 : schemaMetadataList.reduce((acc, item) => {
37
+ acc[item.logicalName] = item;
38
+ return acc;
39
+ }, {})) !== null && _a !== void 0 ? _a : {};
40
+ }, [schemaMetadataList]);
41
+ if (isLoading) {
42
+ return loadingComponent;
43
+ }
44
+ if (!app) {
45
+ return notFoundComponent;
46
+ }
47
+ return ((0, jsx_runtime_1.jsx)(context_1.AppContext.Provider, { value: { app, schemaMetadataDic, schemaMetadataList }, children: children }));
48
+ };
49
+ exports.AppProvider = AppProvider;
@@ -0,0 +1,7 @@
1
+ import { FC, PropsWithChildren } from 'react';
2
+ import { AuthProviderPlaceholderProps } from '../auth';
3
+ interface AuthWrapperProps {
4
+ Placeholder: FC<AuthProviderPlaceholderProps>;
5
+ }
6
+ export declare const AuthWrapper: FC<PropsWithChildren<AuthWrapperProps>>;
7
+ export {};
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthWrapper = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const auth_1 = require("../auth");
6
+ const mutable_1 = require("../mutable");
7
+ const AuthWrapper = ({ children, Placeholder, }) => {
8
+ const state = (0, mutable_1.useContextSelector)(auth_1.AuthContext, (state) => state);
9
+ if (state.loadError) {
10
+ return (0, jsx_runtime_1.jsx)(Placeholder, { loadingError: true, retry: state.loadSession });
11
+ }
12
+ if (!state.initialized || state.loading) {
13
+ return (0, jsx_runtime_1.jsx)(Placeholder, { loading: true, retry: state.loadSession });
14
+ }
15
+ if (!state.authenticated) {
16
+ if (state.sessionExpired) {
17
+ return (0, jsx_runtime_1.jsx)(Placeholder, { sessionExpired: true, retry: state.loadSession });
18
+ }
19
+ return (0, jsx_runtime_1.jsx)(Placeholder, { unauthorized: true, retry: state.loadSession });
20
+ }
21
+ return children;
22
+ };
23
+ exports.AuthWrapper = AuthWrapper;
@@ -0,0 +1,28 @@
1
+ import { IDataService } from '@headless-adminapp/core/transport';
2
+ import { QueryClient } from '@tanstack/react-query';
3
+ import { Dispatch, FC, PropsWithChildren, SetStateAction } from 'react';
4
+ import { AppearanceContextState } from '../appearance';
5
+ import { AuthProviderPlaceholderProps, AuthProviderProps } from '../auth';
6
+ import { LocaleProviderProps } from '../locale';
7
+ import { MetadataProviderProps } from '../metadata/MetadataProvider';
8
+ import { RouteProviderProps } from '../route/RouteProvider';
9
+ export interface LayoutProviderProps {
10
+ routeProps: RouteProviderProps;
11
+ queryClient: QueryClient;
12
+ appearanceState: [
13
+ AppearanceContextState,
14
+ Dispatch<SetStateAction<AppearanceContextState>>
15
+ ];
16
+ localeProps: LocaleProviderProps;
17
+ dataService: IDataService;
18
+ authProps: AuthProviderProps;
19
+ authPlaceholder: FC<AuthProviderPlaceholderProps>;
20
+ metadataProps: MetadataProviderProps;
21
+ containers: {
22
+ DialogContainer: FC;
23
+ ProgressIndicatorContainer: FC;
24
+ ToastNotificationContainer: FC;
25
+ };
26
+ }
27
+ /** @todo: move to app */
28
+ export declare const LayoutProvider: FC<PropsWithChildren<LayoutProviderProps>>;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LayoutProvider = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const react_query_1 = require("@tanstack/react-query");
6
+ const appearance_1 = require("../appearance");
7
+ const auth_1 = require("../auth");
8
+ const dialog_1 = require("../dialog");
9
+ const locale_1 = require("../locale");
10
+ const metadata_1 = require("../metadata");
11
+ const progress_indicator_1 = require("../progress-indicator");
12
+ const recordset_1 = require("../recordset");
13
+ const route_1 = require("../route");
14
+ const toast_notification_1 = require("../toast-notification");
15
+ const transport_1 = require("../transport");
16
+ const AuthWrapper_1 = require("./AuthWrapper");
17
+ /** @todo: move to app */
18
+ const LayoutProvider = ({ appearanceState, authPlaceholder, authProps, dataService, localeProps, metadataProps, queryClient, routeProps, children, containers: { DialogContainer, ProgressIndicatorContainer, ToastNotificationContainer, }, }) => {
19
+ return ((0, jsx_runtime_1.jsx)(route_1.RouteProvider, Object.assign({}, routeProps, { children: (0, jsx_runtime_1.jsx)(react_query_1.QueryClientProvider, { client: queryClient, children: (0, jsx_runtime_1.jsx)(appearance_1.AppearanceContext.Provider, { value: appearanceState, children: (0, jsx_runtime_1.jsx)(locale_1.LocaleProvider, Object.assign({}, localeProps, { children: (0, jsx_runtime_1.jsx)(metadata_1.MetadataProvider, Object.assign({}, metadataProps, { children: (0, jsx_runtime_1.jsx)(transport_1.DataServiceContext.Provider, { value: dataService, children: (0, jsx_runtime_1.jsx)(dialog_1.DialogProvider, { children: (0, jsx_runtime_1.jsx)(progress_indicator_1.ProgressIndicatorProvider, { children: (0, jsx_runtime_1.jsxs)(toast_notification_1.ToastNotificationProvider, { children: [(0, jsx_runtime_1.jsx)(DialogContainer, {}), (0, jsx_runtime_1.jsx)(ProgressIndicatorContainer, {}), (0, jsx_runtime_1.jsx)(ToastNotificationContainer, {}), (0, jsx_runtime_1.jsx)(auth_1.AuthProvider, Object.assign({}, authProps, { children: (0, jsx_runtime_1.jsx)(AuthWrapper_1.AuthWrapper, { Placeholder: authPlaceholder, children: (0, jsx_runtime_1.jsx)(recordset_1.RecordSetProvider, { children: children }) }) }))] }) }) }) }) })) })) }) }) })));
20
+ };
21
+ exports.LayoutProvider = LayoutProvider;
package/app/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { AppContext } from './context';
2
2
  export { useAppContext } from './hooks';
3
+ export { AppProvider } from './AppProvider';
package/app/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useAppContext = exports.AppContext = void 0;
3
+ exports.AppProvider = exports.useAppContext = exports.AppContext = void 0;
4
4
  var context_1 = require("./context");
5
5
  Object.defineProperty(exports, "AppContext", { enumerable: true, get: function () { return context_1.AppContext; } });
6
6
  var hooks_1 = require("./hooks");
7
7
  Object.defineProperty(exports, "useAppContext", { enumerable: true, get: function () { return hooks_1.useAppContext; } });
8
+ var AppProvider_1 = require("./AppProvider");
9
+ Object.defineProperty(exports, "AppProvider", { enumerable: true, get: function () { return AppProvider_1.AppProvider; } });
@@ -61,12 +61,6 @@ class BaseSchemaExperienceBuilder {
61
61
  quickCreateForms: this.quickCreateForms,
62
62
  };
63
63
  }
64
- // public createView(view: Omit<View<S>, 'logicalName'>): View<S> {
65
- // return {
66
- // logicalName: this.logicalName,
67
- // ...view,
68
- // };
69
- // }
70
64
  defineFormExperience(formExperience) {
71
65
  return formExperience;
72
66
  }
@@ -190,11 +184,7 @@ class SchemaExperienceBuilder extends BaseSchemaExperienceBuilder {
190
184
  return defaultId;
191
185
  }
192
186
  defineExperience(experience) {
193
- // if (!experience.defaultFormId) {
194
- // throw new Error('Default form is required');
195
- // }
196
187
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
197
- // console.log('experience', experience);
198
188
  let lookups = experience.lookups;
199
189
  let views = experience.views;
200
190
  let forms = experience.forms;
@@ -33,7 +33,6 @@ function InitialValueResolver() {
33
33
  formInstanceRef.current = formInstance;
34
34
  (0, react_1.useEffect)(() => {
35
35
  const timer = setTimeout(() => {
36
- // console.log('resetting form', initialValues);
37
36
  formInstanceRef.current.reset(initialValues);
38
37
  });
39
38
  return () => {
@@ -121,9 +121,6 @@ function saveRecord(_a) {
121
121
  isError: false,
122
122
  };
123
123
  }
124
- if (operations.length > 0) {
125
- console.log('operations', operations);
126
- }
127
124
  for (const operation of operations) {
128
125
  switch (operation.type) {
129
126
  case 'create':
@@ -217,7 +214,6 @@ function getInitialValues({ cloneRecord, form, record, recordId, defaultParamete
217
214
  }
218
215
  exports.formValidator = (0, lodash_1.memoize)(function formValidator({ form, schema, readonlyAttributes, formReadOnly, getSchema, language, strings, }) {
219
216
  return (values, context, options) => __awaiter(this, void 0, void 0, function* () {
220
- // console.log('formValidator', values);
221
217
  let validator = yup.object().shape({});
222
218
  if (!formReadOnly) {
223
219
  const activeControls = form.experience.tabs
@@ -264,7 +260,6 @@ exports.formValidator = (0, lodash_1.memoize)(function formValidator({ form, sch
264
260
  }
265
261
  const resolver = (0, yup_1.yupResolver)(validator);
266
262
  const result = yield resolver(values, context, options);
267
- // console.log(result);
268
263
  return result;
269
264
  });
270
265
  }, ({ form, language, schema, strings, readonlyAttributes, formReadOnly }) => JSON.stringify({
@@ -11,7 +11,7 @@ export interface GridContextState<S extends SchemaAttributes = SchemaAttributes,
11
11
  schema: Schema<S>;
12
12
  view: View<S>;
13
13
  viewLookup: LocalizedDataLookup[];
14
- onChangeView: (viewId: string) => void;
14
+ onChangeView?: (viewId: string) => void;
15
15
  extraFilter?: Filter;
16
16
  commands: CommandItemExperience<CommandContext>[][];
17
17
  maxRecords?: number;
@@ -7,6 +7,6 @@ const context_2 = require("../context");
7
7
  function useChangeView() {
8
8
  const onChangeView = (0, context_1.useContextSelector)(context_2.GridContext, (state) => state.onChangeView);
9
9
  return (0, react_1.useCallback)((viewId) => {
10
- onChangeView(viewId);
10
+ onChangeView === null || onChangeView === void 0 ? void 0 : onChangeView(viewId);
11
11
  }, [onChangeView]);
12
12
  }
@@ -97,129 +97,7 @@ function useMainGridCommandHandlerContext() {
97
97
  function useMainGridCommands() {
98
98
  const commands = (0, useGridCommands_1.useGridCommands)();
99
99
  const handlerContext = useMainGridCommandHandlerContext();
100
- // console.log('temp. gc', commands);
101
100
  return (0, command_1.useCommands)(commands, handlerContext);
102
- // return [
103
- // [
104
- // {
105
- // type: 'button',
106
- // icon: 'Add',
107
- // text: 'Add',
108
- // },
109
- // {
110
- // type: 'button',
111
- // icon: 'Delete',
112
- // text: 'Delete',
113
- // danger: true,
114
- // },
115
- // {
116
- // type: 'button',
117
- // icon: 'Add',
118
- // text: 'Add',
119
- // },
120
- // {
121
- // type: 'button',
122
- // icon: 'Add',
123
- // text: 'Add',
124
- // },
125
- // ],
126
- // [
127
- // {
128
- // type: 'button',
129
- // icon: 'Add',
130
- // text: 'Add',
131
- // },
132
- // {
133
- // type: 'button',
134
- // icon: 'Add',
135
- // text: 'Add',
136
- // },
137
- // {
138
- // type: 'button',
139
- // icon: 'Add',
140
- // text: 'Add',
141
- // },
142
- // {
143
- // type: 'button',
144
- // icon: 'Add',
145
- // text: 'Add',
146
- // },
147
- // {
148
- // type: 'button',
149
- // icon: 'Add',
150
- // text: 'Add',
151
- // },
152
- // {
153
- // type: 'button',
154
- // icon: 'Add',
155
- // text: 'Add',
156
- // },
157
- // {
158
- // type: 'menu',
159
- // icon: 'Add',
160
- // text: 'Report',
161
- // danger: true,
162
- // handlerId: 'report',
163
- // items: [
164
- // {
165
- // icon: 'Add',
166
- // text: 'Export',
167
- // danger: true,
168
- // },
169
- // ],
170
- // },
171
- // {
172
- // type: 'menu',
173
- // icon: 'Add',
174
- // text: 'Report',
175
- // danger: true,
176
- // items: [
177
- // {
178
- // icon: 'Add',
179
- // text: 'Export',
180
- // danger: true,
181
- // },
182
- // ],
183
- // },
184
- // ],
185
- // [
186
- // {
187
- // type: 'button',
188
- // icon: 'Add',
189
- // text: 'Add',
190
- // },
191
- // {
192
- // type: 'button',
193
- // icon: 'Add',
194
- // text: 'Add',
195
- // },
196
- // {
197
- // type: 'button',
198
- // icon: 'Add',
199
- // text: 'Add',
200
- // },
201
- // {
202
- // type: 'button',
203
- // icon: 'Add',
204
- // text: 'Add',
205
- // },
206
- // {
207
- // type: 'button',
208
- // icon: 'Add',
209
- // text: 'Add',
210
- // },
211
- // {
212
- // type: 'button',
213
- // icon: 'Add',
214
- // text: 'Add',
215
- // },
216
- // {
217
- // type: 'button',
218
- // icon: 'Add',
219
- // text: 'Add',
220
- // },
221
- // ],
222
- // ];
223
101
  }
224
102
  function useMainGridContextCommands() {
225
103
  const commands = (0, useGridCommands_1.useGridCommands)();
@@ -46,7 +46,6 @@ function useSubGridCommandHandlerContext() {
46
46
  const utility = (0, useMainGridCommands_1.useUtility)();
47
47
  const locale = (0, useLocale_1.useLocale)();
48
48
  const mainFormHandlerContext = (0, useMainFormCommands_1.useMainFormCommandHandlerContext)();
49
- // console.log('mainFormHandlerContext', mainFormHandlerContext);
50
49
  return {
51
50
  dataService,
52
51
  queryClient,
@@ -76,8 +75,6 @@ function useSubGridCommandHandlerContext() {
76
75
  function useSubGridCommands() {
77
76
  const commands = (0, useGridCommands_1.useGridCommands)();
78
77
  const handlerContext = useSubGridCommandHandlerContext();
79
- console.log('temp. commands (s)', commands);
80
- console.log('temp. handlerContext (s)', handlerContext);
81
78
  return (0, command_1.useCommands)(commands, handlerContext);
82
79
  }
83
80
  function useSubGridContextCommands() {
package/index.css ADDED
@@ -0,0 +1,4 @@
1
+ .hdl-scrollbar.rtl > div {
2
+ margin-right: 0 !important;
3
+ margin-left: -15px;
4
+ }
@@ -1,6 +1,6 @@
1
1
  import { Locale } from '@headless-adminapp/core/experience/locale';
2
2
  import { FC, PropsWithChildren } from 'react';
3
- interface LocaleProviderProps {
3
+ export interface LocaleProviderProps {
4
4
  locale: string;
5
5
  options?: {
6
6
  direction?: Locale['direction'];
@@ -10,4 +10,3 @@ interface LocaleProviderProps {
10
10
  };
11
11
  }
12
12
  export declare const LocaleProvider: FC<PropsWithChildren<LocaleProviderProps>>;
13
- export {};
@@ -5,9 +5,9 @@ const jsx_runtime_1 = require("react/jsx-runtime");
5
5
  const locale_1 = require("@headless-adminapp/core/experience/locale");
6
6
  const react_1 = require("react");
7
7
  const context_1 = require("./context");
8
- const LocaleProvider = ({ children, locale, }) => {
8
+ const LocaleProvider = ({ children, locale, options, }) => {
9
9
  const localeState = (0, react_1.useMemo)(() => {
10
- return (0, locale_1.getLocale)(locale);
10
+ return (0, locale_1.getLocale)(locale, options);
11
11
  }, [locale]);
12
12
  return ((0, jsx_runtime_1.jsx)(context_1.LocaleContext.Provider, { value: localeState, children: children }));
13
13
  };
package/locale/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { LocaleContext } from './context';
2
2
  export * from './useLocale';
3
+ export { type LocaleProviderProps, LocaleProvider } from './LocaleProvider';
package/locale/index.js CHANGED
@@ -14,7 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.LocaleContext = void 0;
17
+ exports.LocaleProvider = exports.LocaleContext = void 0;
18
18
  var context_1 = require("./context");
19
19
  Object.defineProperty(exports, "LocaleContext", { enumerable: true, get: function () { return context_1.LocaleContext; } });
20
20
  __exportStar(require("./useLocale"), exports);
21
+ var LocaleProvider_1 = require("./LocaleProvider");
22
+ Object.defineProperty(exports, "LocaleProvider", { enumerable: true, get: function () { return LocaleProvider_1.LocaleProvider; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@headless-adminapp/app",
3
- "version": "0.0.17-alpha.0",
3
+ "version": "0.0.17-alpha.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -18,14 +18,11 @@
18
18
  },
19
19
  "scripts": {
20
20
  "build": "tsc",
21
- "copy-files": "cp package.json dist",
21
+ "ts-check": "tsc --noEmit",
22
+ "copy-files": "cp package.json dist && cp src/index.css dist",
22
23
  "prepublishOnly": "pnpm run build && pnpm run copy-files",
23
24
  "test": "echo \"Error: no test specified\" && exit 1"
24
25
  },
25
- "peerDependencies": {
26
- "@tanstack/react-query": "^5",
27
- "react-hook-form": "^7"
28
- },
29
26
  "keywords": [],
30
27
  "author": "",
31
28
  "license": "ISC",
@@ -33,5 +30,5 @@
33
30
  "clsx": "2.1.1",
34
31
  "react-custom-scrollbars-2": "^4.5.0"
35
32
  },
36
- "gitHead": "f57583f8390f15b373fbe095c8f3391f538b94d3"
33
+ "gitHead": "f6ce162167fabc3cbac101038b157ddd22455a4f"
37
34
  }
@@ -7,7 +7,7 @@ interface PageEntityViewProviderProps<S extends SchemaAttributes> {
7
7
  view: View<S>;
8
8
  commands: EntityMainGridCommandItemExperience[][];
9
9
  availableViews: LocalizedDataLookup[];
10
- onChangeView: (viewId: string) => void;
10
+ onChangeView?: (viewId: string) => void;
11
11
  }
12
12
  export declare function PageEntityViewProvider<S extends SchemaAttributes>({ availableViews, commands, schema, view, children, onChangeView, }: PropsWithChildren<PageEntityViewProviderProps<S>>): import("react/jsx-runtime").JSX.Element;
13
13
  export {};
@@ -44,7 +44,6 @@ function useRecordSetResult() {
44
44
  const { isPending, data, error } = (0, react_query_1.useQuery)({
45
45
  queryKey: ['data', 'recordset', context.logicalName, context.ids, columns],
46
46
  queryFn: () => __awaiter(this, void 0, void 0, function* () {
47
- console.log('fetching data', context.ids, !context.logicalName, !context.ids.length, !cardView);
48
47
  if (!context.logicalName || !context.ids.length || !cardView) {
49
48
  return [];
50
49
  }
@@ -3,3 +3,4 @@ export * from './useSearchParams';
3
3
  export * from './useRouter';
4
4
  export * from './useBasePath';
5
5
  export * from './useRouteResolver';
6
+ export * from './useIsRouteActive';
@@ -19,3 +19,4 @@ __exportStar(require("./useSearchParams"), exports);
19
19
  __exportStar(require("./useRouter"), exports);
20
20
  __exportStar(require("./useBasePath"), exports);
21
21
  __exportStar(require("./useRouteResolver"), exports);
22
+ __exportStar(require("./useIsRouteActive"), exports);
@@ -0,0 +1 @@
1
+ export declare function useIsRouteActive(): import("../context").InternalIsRouteActive;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useIsRouteActive = useIsRouteActive;
4
+ const react_1 = require("react");
5
+ const context_1 = require("../context");
6
+ function useIsRouteActive() {
7
+ return (0, react_1.useContext)(context_1.RouteHelperContext).isRouteActive;
8
+ }
@@ -0,0 +1,15 @@
1
+ import { Attribute, InferredAttributeType } from '@headless-adminapp/core/attributes';
2
+ interface AttributeFormattedValueStringsSet {
3
+ yes: string;
4
+ no: string;
5
+ }
6
+ export declare function getAttributeFormattedValue<A extends Attribute = Attribute>(attribute: Attribute, value: InferredAttributeType<A> | null | undefined, options?: {
7
+ maxCount?: number;
8
+ strings?: AttributeFormattedValueStringsSet;
9
+ dateFormat?: string;
10
+ locale?: string;
11
+ currency?: string;
12
+ currencySign?: 'accounting' | 'standard';
13
+ currencyDisplay?: 'symbol' | 'narrowSymbol' | 'code';
14
+ }): string | null | undefined;
15
+ export {};
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getAttributeFormattedValue = getAttributeFormattedValue;
7
+ const dayjs_1 = __importDefault(require("dayjs"));
8
+ const defaultAttributeFormattedValueStrings = {
9
+ yes: 'Yes',
10
+ no: 'No',
11
+ };
12
+ const defaultDateFormat = 'YYYY-MM-DD';
13
+ const defaultLocale = 'en-US';
14
+ const defaultCurrency = 'USD';
15
+ const defaultCurrencySign = 'accounting';
16
+ const defaultCurrencyDisplay = 'symbol';
17
+ function getAttributeFormattedValue(attribute, value, options) {
18
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
19
+ if (value === null || value === undefined) {
20
+ return null;
21
+ }
22
+ const strings = (_a = options === null || options === void 0 ? void 0 : options.strings) !== null && _a !== void 0 ? _a : defaultAttributeFormattedValueStrings;
23
+ const dateFormat = (_b = options === null || options === void 0 ? void 0 : options.dateFormat) !== null && _b !== void 0 ? _b : defaultDateFormat;
24
+ const locale = (_c = options === null || options === void 0 ? void 0 : options.locale) !== null && _c !== void 0 ? _c : defaultLocale;
25
+ const currency = (_d = options === null || options === void 0 ? void 0 : options.currency) !== null && _d !== void 0 ? _d : defaultCurrency;
26
+ const currencySign = (_e = options === null || options === void 0 ? void 0 : options.currencySign) !== null && _e !== void 0 ? _e : defaultCurrencySign;
27
+ const currencyDisplay = (_f = options === null || options === void 0 ? void 0 : options.currencyDisplay) !== null && _f !== void 0 ? _f : defaultCurrencyDisplay;
28
+ switch (attribute.type) {
29
+ case 'boolean':
30
+ return value
31
+ ? (_g = attribute.trueLabel) !== null && _g !== void 0 ? _g : strings.yes
32
+ : (_h = attribute.falseLabel) !== null && _h !== void 0 ? _h : strings.no;
33
+ case 'choice':
34
+ return ((_j = attribute.options.find((option) => option.value === value)) !== null && _j !== void 0 ? _j : {
35
+ label: '',
36
+ }).label;
37
+ case 'choices':
38
+ return value
39
+ .map((v) => {
40
+ var _a;
41
+ return ((_a = attribute.options.find((option) => option.value === v)) !== null && _a !== void 0 ? _a : {
42
+ label: '',
43
+ }).label;
44
+ })
45
+ .join(', ');
46
+ case 'date':
47
+ return (0, dayjs_1.default)(value).format(dateFormat);
48
+ case 'daterange':
49
+ if (!value)
50
+ return null;
51
+ const from = value[0];
52
+ const to = value[1];
53
+ if (!from && !to) {
54
+ return null;
55
+ }
56
+ if (from && to) {
57
+ return ((0, dayjs_1.default)(from).format(dateFormat) + ' - ' + (0, dayjs_1.default)(to).format(dateFormat));
58
+ }
59
+ if (from) {
60
+ return 'After ' + (0, dayjs_1.default)(from).format(dateFormat);
61
+ }
62
+ if (to) {
63
+ return 'Before ' + (0, dayjs_1.default)(to).format(dateFormat);
64
+ }
65
+ return null;
66
+ case 'lookup':
67
+ return value === null || value === void 0 ? void 0 : value.name;
68
+ case 'lookups':
69
+ const items = value === null || value === void 0 ? void 0 : value.map((v) => v.name);
70
+ if ((options === null || options === void 0 ? void 0 : options.maxCount) && (items === null || items === void 0 ? void 0 : items.length) > options.maxCount) {
71
+ return (items.slice(0, options.maxCount).join(', ') +
72
+ ` (+${items.length - options.maxCount})`);
73
+ }
74
+ else {
75
+ return items === null || items === void 0 ? void 0 : items.join(', ');
76
+ }
77
+ case 'money':
78
+ return new Intl.NumberFormat(locale, {
79
+ style: 'currency',
80
+ currency,
81
+ currencySign,
82
+ currencyDisplay,
83
+ }).format(value);
84
+ case 'number':
85
+ return new Intl.NumberFormat(locale).format(value);
86
+ default:
87
+ return typeof value === 'object'
88
+ ? JSON.stringify(value)
89
+ : value;
90
+ }
91
+ }
package/utils/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './calculateColumnWidths';
2
+ export * from './getAttributeFormattedValue';
package/utils/index.js CHANGED
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./calculateColumnWidths"), exports);
18
+ __exportStar(require("./getAttributeFormattedValue"), exports);