@bigbinary/neeto-themes-frontend 2.0.3 → 2.0.5

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/dist/index.js CHANGED
@@ -1,20 +1,20 @@
1
1
  import { shallow } from 'zustand/shallow';
2
2
  import { useEffect, createElement, useState, useRef, memo, useCallback } from 'react';
3
3
  import { t as t$1 } from 'i18next';
4
+ import { isNotPresent, snakeToCamelCase, filterBy, isPresent, isNot, findBy, findIndexBy, toLabelAndValue, humanize, isNotEmpty, noop } from '@bigbinary/neeto-cist';
4
5
  import { withImmutableActions, useMutationWithInvalidation, withT, withTitle } from '@bigbinary/neeto-commons-frontend/react-utils';
5
6
  import Spinner from '@bigbinary/neetoui/Spinner';
7
+ import { useQuery } from '@tanstack/react-query';
8
+ import axios from 'axios';
9
+ import { prop, isEmpty, negate } from 'ramda';
6
10
  import { create as create$1 } from 'zustand';
7
- import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
8
- import { isNotPresent, snakeToCamelCase, filterBy, isPresent, isNot, findBy, findIndexBy, toLabelAndValue, humanize, isNotEmpty } from '@bigbinary/neeto-cist';
9
- import Alert from '@bigbinary/neetoui/Alert';
10
- import { useTranslation, Trans } from 'react-i18next';
11
11
  import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
12
12
  import _regeneratorRuntime from '@babel/runtime/regenerator';
13
13
  import Toastr from '@bigbinary/neetoui/Toastr';
14
- import { useQuery } from '@tanstack/react-query';
15
- import axios from 'axios';
14
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
15
+ import Alert from '@bigbinary/neetoui/Alert';
16
+ import { useTranslation, Trans } from 'react-i18next';
16
17
  import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
17
- import { prop, isEmpty, negate } from 'ramda';
18
18
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
19
19
  import Close from '@bigbinary/neeto-icons/Close';
20
20
  import Button from '@bigbinary/neetoui/Button';
@@ -40,6 +40,33 @@ import MoreDropdown from '@bigbinary/neeto-molecules/MoreDropdown';
40
40
  import LeftArrow from '@bigbinary/neeto-icons/LeftArrow';
41
41
  import Plus from '@bigbinary/neeto-icons/Plus';
42
42
 
43
+ var BASE_URL = "neeto_themes_engine";
44
+ var WHITE = "255, 255, 255";
45
+ var BLACK = "0, 0, 0";
46
+
47
+ var show$1 = function show(entityId) {
48
+ return axios.get("".concat(BASE_URL, "/api/v1/entities/").concat(entityId));
49
+ };
50
+ var entitiesApi = {
51
+ show: show$1
52
+ };
53
+
54
+ var QUERY_KEYS = {
55
+ THEMES_LIST: "themes-list",
56
+ THEME_DETAILS: "theme-details",
57
+ SCHEMA_DETAILS: "schema-details"
58
+ };
59
+
60
+ var useShowThemeEntity = function useShowThemeEntity(entityId) {
61
+ return useQuery({
62
+ queryKey: [QUERY_KEYS.THEME_ENTITY_DETAILS, entityId],
63
+ queryFn: function queryFn() {
64
+ return entitiesApi.show(entityId);
65
+ },
66
+ enabled: !!entityId
67
+ });
68
+ };
69
+
43
70
  /** @type {import("neetocommons/react-utils").ZustandStoreHook} */
44
71
  var useConfigStore = create$1(withImmutableActions(function (set) {
45
72
  return {
@@ -129,35 +156,101 @@ var hexToRgba = function hexToRgba(hex) {
129
156
  return "".concat(r, ", ").concat(g, ", ").concat(b).concat(a !== undefined ? ", ".concat(a) : "");
130
157
  };
131
158
 
132
- var BASE_URL = "neeto_themes_engine";
133
- var WHITE = "255, 255, 255";
134
- var BLACK = "0, 0, 0";
135
-
136
- var show$1 = function show(entityId) {
137
- return axios.get("".concat(BASE_URL, "/api/v1/entities/").concat(entityId));
159
+ var root = document.querySelector(":root");
160
+ var setVariable = function setVariable(name, value, variableNamesMap) {
161
+ var variableName = variableNamesMap[name];
162
+ if (!variableName) return;
163
+ root.style.setProperty(variableName, value);
138
164
  };
139
- var entitiesApi = {
140
- show: show$1
165
+ var setTheme = function setTheme(theme, variableNamesMap, themeConfig) {
166
+ if (isNotPresent(theme === null || theme === void 0 ? void 0 : theme.properties)) return;
167
+ theme.properties.forEach(function (property) {
168
+ var attribute = snakeToCamelCase(property.key);
169
+ if (property.kind === "color") {
170
+ setVariable(attribute, hexToRgba(property.value), variableNamesMap);
171
+ } else if (property.kind === "overlay_opacity") {
172
+ var overlayOpacity = Number(property.value);
173
+ setVariable(snakeToCamelCase("".concat(property.key, "_overlay_opacity")), Math.abs(overlayOpacity / 100), variableNamesMap);
174
+ setVariable(snakeToCamelCase("".concat(property.key, "_overlay_color")), overlayOpacity < 0 ? BLACK : WHITE, variableNamesMap);
175
+ } else if (property.kind === "radius") {
176
+ setVariable(attribute, "".concat(property.value, "px"), variableNamesMap);
177
+ } else {
178
+ setVariable(attribute, property.value, variableNamesMap);
179
+ }
180
+ });
181
+ filterBy({
182
+ kind: "image"
183
+ }, themeConfig).forEach(function (_ref) {
184
+ var _theme$attribute;
185
+ var key = _ref.key;
186
+ var attribute = snakeToCamelCase(key);
187
+ setVariable(attribute, "url(".concat((_theme$attribute = theme[attribute]) === null || _theme$attribute === void 0 ? void 0 : _theme$attribute.url, ")"), variableNamesMap);
188
+ });
141
189
  };
142
190
 
143
- var QUERY_KEYS = {
144
- THEMES_LIST: "themes-list",
145
- THEME_DETAILS: "theme-details",
146
- SCHEMA_DETAILS: "schema-details"
191
+ var list$1 = function list() {
192
+ return axios.get("".concat(BASE_URL, "/api/v1/schema"));
193
+ };
194
+ var schemasApi = {
195
+ list: list$1
147
196
  };
148
197
 
149
- var useShowThemeEntity = function useShowThemeEntity(entityId) {
198
+ var useListSchema = function useListSchema() {
150
199
  return useQuery({
151
- queryKey: [QUERY_KEYS.THEME_ENTITY_DETAILS, entityId],
152
- queryFn: function queryFn() {
153
- return entitiesApi.show(entityId);
154
- },
155
- enabled: !!entityId
200
+ queryKey: [QUERY_KEYS.SCHEMA_DETAILS],
201
+ queryFn: schemasApi.list
156
202
  });
157
203
  };
158
204
 
205
+ var useThemeUtils = function useThemeUtils() {
206
+ var setConfigState = useConfigStore(prop("setConfigState"));
207
+ var _useThemeStore = useThemeStore(function (store) {
208
+ return {
209
+ previewingTheme: store["previewingTheme"],
210
+ currentTheme: store["currentTheme"]
211
+ };
212
+ }, shallow),
213
+ previewingTheme = _useThemeStore.previewingTheme,
214
+ currentTheme = _useThemeStore.currentTheme;
215
+ var _useListSchema = useListSchema(),
216
+ isFetchingSchema = _useListSchema.isLoading,
217
+ data = _useListSchema.data;
218
+ var _ref = data || {},
219
+ _ref$schema = _ref.schema,
220
+ themePropertiesSchema = _ref$schema === void 0 ? [] : _ref$schema,
221
+ _ref$variableNamesMap = _ref.variableNamesMap,
222
+ variableNamesMap = _ref$variableNamesMap === void 0 ? {} : _ref$variableNamesMap;
223
+ useEffect(function () {
224
+ if (data) {
225
+ setConfigState({
226
+ themePropertiesSchema: data === null || data === void 0 ? void 0 : data.schema,
227
+ variableNamesMap: data === null || data === void 0 ? void 0 : data.variableNamesMap,
228
+ defaultThemeName: data === null || data === void 0 ? void 0 : data.defaultThemeName
229
+ });
230
+ }
231
+ }, [data]);
232
+ useEffect(function () {
233
+ return setConfigState({
234
+ isFetchingSchema: isFetchingSchema
235
+ });
236
+ }, [isFetchingSchema]);
237
+ var setVariable$1 = function setVariable$1(name, value) {
238
+ return setVariable(name, value, variableNamesMap);
239
+ };
240
+ var setTheme$1 = function setTheme$1(theme) {
241
+ return setTheme(theme, variableNamesMap, themePropertiesSchema);
242
+ };
243
+ return {
244
+ setTheme: setTheme$1,
245
+ setVariable: setVariable$1,
246
+ previewingTheme: previewingTheme,
247
+ currentTheme: currentTheme,
248
+ isFetchingSchema: isFetchingSchema
249
+ };
250
+ };
251
+
159
252
  var _excluded$1 = ["themeId"];
160
- var list$1 = function list(entityId) {
253
+ var list = function list(entityId) {
161
254
  return axios.get("".concat(BASE_URL, "/api/v1/themes"), {
162
255
  params: {
163
256
  entity_id: entityId
@@ -193,7 +286,7 @@ var apply = function apply(_ref2) {
193
286
  });
194
287
  };
195
288
  var themesApi = {
196
- list: list$1,
289
+ list: list,
197
290
  show: show,
198
291
  create: create,
199
292
  update: update,
@@ -250,99 +343,6 @@ var useApplyTheme = function useApplyTheme(_ref) {
250
343
  });
251
344
  };
252
345
 
253
- var root = document.querySelector(":root");
254
- var setVariable = function setVariable(name, value, variableNamesMap) {
255
- var variableName = variableNamesMap[name];
256
- if (!variableName) return;
257
- root.style.setProperty(variableName, value);
258
- };
259
- var setTheme = function setTheme(theme, variableNamesMap, themeConfig) {
260
- if (isNotPresent(theme === null || theme === void 0 ? void 0 : theme.properties)) return;
261
- theme.properties.forEach(function (property) {
262
- var attribute = snakeToCamelCase(property.key);
263
- if (property.kind === "color") {
264
- setVariable(attribute, hexToRgba(property.value), variableNamesMap);
265
- } else if (property.kind === "overlay_opacity") {
266
- var overlayOpacity = Number(property.value);
267
- setVariable(snakeToCamelCase("".concat(property.key, "_overlay_opacity")), Math.abs(overlayOpacity / 100), variableNamesMap);
268
- setVariable(snakeToCamelCase("".concat(property.key, "_overlay_color")), overlayOpacity < 0 ? BLACK : WHITE, variableNamesMap);
269
- } else if (property.kind === "radius") {
270
- setVariable(attribute, "".concat(property.value, "px"), variableNamesMap);
271
- } else {
272
- setVariable(attribute, property.value, variableNamesMap);
273
- }
274
- });
275
- filterBy({
276
- kind: "image"
277
- }, themeConfig).forEach(function (_ref) {
278
- var _theme$attribute;
279
- var key = _ref.key;
280
- var attribute = snakeToCamelCase(key);
281
- setVariable(attribute, "url(".concat((_theme$attribute = theme[attribute]) === null || _theme$attribute === void 0 ? void 0 : _theme$attribute.url, ")"), variableNamesMap);
282
- });
283
- };
284
-
285
- var list = function list() {
286
- return axios.get("".concat(BASE_URL, "/api/v1/schema"));
287
- };
288
- var schemasApi = {
289
- list: list
290
- };
291
-
292
- var useListSchema = function useListSchema() {
293
- return useQuery({
294
- queryKey: [QUERY_KEYS.SCHEMA_DETAILS],
295
- queryFn: schemasApi.list
296
- });
297
- };
298
-
299
- var useThemeUtils = function useThemeUtils() {
300
- var setConfigState = useConfigStore(prop("setConfigState"));
301
- var _useThemeStore = useThemeStore(function (store) {
302
- return {
303
- previewingTheme: store["previewingTheme"],
304
- currentTheme: store["currentTheme"]
305
- };
306
- }, shallow),
307
- previewingTheme = _useThemeStore.previewingTheme,
308
- currentTheme = _useThemeStore.currentTheme;
309
- var _useListSchema = useListSchema(),
310
- isFetchingSchema = _useListSchema.isLoading,
311
- data = _useListSchema.data;
312
- var _ref = data || {},
313
- _ref$schema = _ref.schema,
314
- themePropertiesSchema = _ref$schema === void 0 ? [] : _ref$schema,
315
- _ref$variableNamesMap = _ref.variableNamesMap,
316
- variableNamesMap = _ref$variableNamesMap === void 0 ? {} : _ref$variableNamesMap;
317
- useEffect(function () {
318
- if (data) {
319
- setConfigState({
320
- themePropertiesSchema: data === null || data === void 0 ? void 0 : data.schema,
321
- variableNamesMap: data === null || data === void 0 ? void 0 : data.variableNamesMap,
322
- defaultThemeName: data === null || data === void 0 ? void 0 : data.defaultThemeName
323
- });
324
- }
325
- }, [data]);
326
- useEffect(function () {
327
- return setConfigState({
328
- isFetchingSchema: isFetchingSchema
329
- });
330
- }, [isFetchingSchema]);
331
- var setVariable$1 = function setVariable$1(name, value) {
332
- return setVariable(name, value, variableNamesMap);
333
- };
334
- var setTheme$1 = function setTheme$1(theme) {
335
- return setTheme(theme, variableNamesMap, themePropertiesSchema);
336
- };
337
- return {
338
- setTheme: setTheme$1,
339
- setVariable: setVariable$1,
340
- previewingTheme: previewingTheme,
341
- currentTheme: currentTheme,
342
- isFetchingSchema: isFetchingSchema
343
- };
344
- };
345
-
346
346
  var POSITION_OPTIONS = [{
347
347
  label: "Left",
348
348
  value: "left"
@@ -1689,7 +1689,8 @@ var MemoizedAlert = /*#__PURE__*/memo(Alert);
1689
1689
  var Sidebar = function Sidebar(_ref) {
1690
1690
  var _themeToEdit$current, _themeToEdit$current2, _themeToDelete$curren2, _themeToDelete$curren3, _themeToDelete$curren4;
1691
1691
  var thumbnail = _ref.thumbnail,
1692
- onPropertiesChange = _ref.onPropertiesChange;
1692
+ onPropertiesChange = _ref.onPropertiesChange,
1693
+ onApplyThemeSuccess = _ref.onApplyThemeSuccess;
1693
1694
  var _useState = useState(DESIGN_SCREENS.THEMES),
1694
1695
  _useState2 = _slicedToArray(_useState, 2),
1695
1696
  currentScreen = _useState2[0],
@@ -1756,10 +1757,11 @@ var Sidebar = function Sidebar(_ref) {
1756
1757
  });
1757
1758
  setThemeBeingApplied({});
1758
1759
  setCurrentScreen(DESIGN_SCREENS.THEMES);
1760
+ onApplyThemeSuccess === null || onApplyThemeSuccess === void 0 || onApplyThemeSuccess();
1759
1761
  }
1760
1762
  }),
1761
1763
  applyTheme = _useApplyTheme.mutate,
1762
- isApplyingTheme = _useApplyTheme.isLoading;
1764
+ isApplyingTheme = _useApplyTheme.isPending;
1763
1765
  var themeToDelete = useRef(null);
1764
1766
  var themeToEdit = useRef(null);
1765
1767
  var handleEditTheme = function handleEditTheme(theme) {
@@ -1787,10 +1789,10 @@ var Sidebar = function Sidebar(_ref) {
1787
1789
  var highlightTheme = function highlightTheme(themeToHighlight) {
1788
1790
  setTimeout(function () {
1789
1791
  scrollElementIntoView(themeToHighlight.id);
1790
- }, 1000);
1792
+ }, 1500);
1791
1793
  setTimeout(function () {
1792
1794
  setThemeToHighlight(null);
1793
- }, 3500);
1795
+ }, 4000);
1794
1796
  setThemeToHighlight(themeToHighlight.id);
1795
1797
  };
1796
1798
  var onCreateTheme = function onCreateTheme(createdTheme) {
@@ -1821,18 +1823,6 @@ var Sidebar = function Sidebar(_ref) {
1821
1823
  }
1822
1824
  });
1823
1825
  }, [deleteTheme, themeToDelete]);
1824
- useEffect(function () {
1825
- if (isNotPresent(currentTheme)) return;
1826
- setTheme(currentTheme);
1827
- }, [currentTheme, entityId]);
1828
- useEffect(function () {
1829
- if (isNotPresent(currentTheme)) return;
1830
- setThemeState({
1831
- currentTheme: currentTheme,
1832
- isCurrentThemeLoading: isCurrentThemeLoading,
1833
- previewingTheme: currentTheme
1834
- });
1835
- }, [currentTheme, isCurrentThemeLoading]);
1836
1826
  return /*#__PURE__*/jsxs("div", {
1837
1827
  className: "neeto-themes-sidebar neeto-ui-border-gray-200 flex-shrink-0 border-r",
1838
1828
  children: [/*#__PURE__*/jsx(TitleBar, {
@@ -1890,19 +1880,31 @@ var NeetoThemesBuilder = function NeetoThemesBuilder(_ref) {
1890
1880
  thumbnail = _ref.thumbnail,
1891
1881
  onPropertiesChange = _ref.onPropertiesChange,
1892
1882
  _ref$isTemplateThemes = _ref.isTemplateThemesEnabled,
1893
- isTemplateThemesEnabled = _ref$isTemplateThemes === void 0 ? false : _ref$isTemplateThemes;
1883
+ isTemplateThemesEnabled = _ref$isTemplateThemes === void 0 ? false : _ref$isTemplateThemes,
1884
+ _ref$onApplyThemeSucc = _ref.onApplyThemeSuccess,
1885
+ onApplyThemeSuccess = _ref$onApplyThemeSucc === void 0 ? noop : _ref$onApplyThemeSucc;
1894
1886
  var _useThemeStore = useThemeStore(function (store) {
1895
1887
  return {
1896
- isCurrentThemeLoading: store["isCurrentThemeLoading"]
1888
+ setThemeState: store["setThemeState"]
1897
1889
  };
1898
1890
  }, shallow),
1899
- isCurrentThemeLoading = _useThemeStore.isCurrentThemeLoading;
1891
+ setThemeState = _useThemeStore.setThemeState;
1892
+ var _useThemeUtils = useThemeUtils(),
1893
+ setTheme = _useThemeUtils.setTheme;
1900
1894
  var _useConfigStore = useConfigStore(function (store) {
1901
1895
  return {
1902
1896
  setConfigState: store["setConfigState"]
1903
1897
  };
1904
1898
  }, shallow),
1905
1899
  setConfigState = _useConfigStore.setConfigState;
1900
+ var _useShowThemeEntity = useShowThemeEntity(entityId),
1901
+ _useShowThemeEntity$d = _useShowThemeEntity.data,
1902
+ _useShowThemeEntity$d2 = _useShowThemeEntity$d === void 0 ? {} : _useShowThemeEntity$d,
1903
+ _useShowThemeEntity$d3 = _useShowThemeEntity$d2.theme,
1904
+ currentTheme = _useShowThemeEntity$d3 === void 0 ? {} : _useShowThemeEntity$d3,
1905
+ isLoading = _useShowThemeEntity.isLoading,
1906
+ isFetchingCurrentTheme = _useShowThemeEntity.isFetching;
1907
+ var isCurrentThemeLoading = isLoading || isFetchingCurrentTheme;
1906
1908
  useEffect(function () {
1907
1909
  setConfigState({
1908
1910
  entityType: entityType,
@@ -1910,9 +1912,22 @@ var NeetoThemesBuilder = function NeetoThemesBuilder(_ref) {
1910
1912
  isTemplateThemesEnabled: isTemplateThemesEnabled
1911
1913
  });
1912
1914
  }, []);
1915
+ useEffect(function () {
1916
+ if (isNotPresent(currentTheme)) return;
1917
+ setThemeState({
1918
+ currentTheme: currentTheme,
1919
+ isCurrentThemeLoading: isCurrentThemeLoading,
1920
+ previewingTheme: currentTheme
1921
+ });
1922
+ }, [currentTheme, isCurrentThemeLoading]);
1923
+ useEffect(function () {
1924
+ if (isNotPresent(currentTheme)) return;
1925
+ setTheme(currentTheme);
1926
+ }, [currentTheme, entityId]);
1913
1927
  return /*#__PURE__*/jsxs("div", {
1914
1928
  className: "neeto-themes__wrapper",
1915
1929
  children: [/*#__PURE__*/jsx(Sidebar, {
1930
+ onApplyThemeSuccess: onApplyThemeSuccess,
1916
1931
  onPropertiesChange: onPropertiesChange,
1917
1932
  thumbnail: thumbnail
1918
1933
  }), /*#__PURE__*/jsx("div", {