@seamly/web-ui 19.0.0-beta.2 → 19.0.0-beta.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.
@@ -1,3 +1,5 @@
1
+ import { randomId } from '../id'
2
+
1
3
  export const SLICE_DELIMITER = '/'
2
4
  export const DOMAIN_DELIMITER = '//'
3
5
 
@@ -10,7 +12,8 @@ export function createAction(
10
12
  identityReducer = (payload) => ({ payload }),
11
13
  ) {
12
14
  const action = (...params) => ({ type, ...identityReducer(...params) })
13
- action.toString = () => type.toString()
15
+ action.toString = () => String(type)
16
+ action.match = (obj) => obj?.type === String(type)
14
17
  return action
15
18
  }
16
19
 
@@ -32,14 +35,47 @@ export function createActions(baseType, ...args) {
32
35
  return handlers.map((handler) => create(...handler))
33
36
  }
34
37
 
35
- export function createThunk(type, thunkCreator) {
36
- const fn = (...args) => {
37
- const thunk = thunkCreator(...args)
38
- thunk.type = type
39
- return thunk
38
+ export function createThunk(type, payloadCreator) {
39
+ const [pending, fulfilled, rejected] = createActions(type, {
40
+ pending: (arg, requestId) => ({
41
+ meta: { arg, requestId, status: 'pending' },
42
+ }),
43
+ fulfilled: (arg, payload, requestId) => ({
44
+ payload,
45
+ meta: { arg, requestId, status: 'fulfilled' },
46
+ }),
47
+ rejected: (arg, error, requestId) => ({
48
+ error,
49
+ meta: { arg, requestId, status: 'rejected', error: String(error) },
50
+ }),
51
+ })
52
+ const thunkCreator = (arg) => (dispatch, getState, extra) => {
53
+ const requestId = randomId()
54
+ const promise = (async () => {
55
+ let finalAction
56
+ try {
57
+ dispatch(pending(arg, requestId))
58
+ const prms = payloadCreator(arg, { dispatch, getState, extra })
59
+ const result = await prms
60
+ finalAction = fulfilled(arg, result, requestId)
61
+ } catch (error) {
62
+ finalAction = rejected(arg, error, requestId)
63
+ }
64
+ dispatch(finalAction)
65
+ return finalAction
66
+ })()
67
+ return Object.assign(promise, {
68
+ type,
69
+ arg,
70
+ requestId,
71
+ })
40
72
  }
41
- fn.toString = () => type
42
- return fn
73
+ return Object.assign(thunkCreator, {
74
+ type,
75
+ pending,
76
+ fulfilled,
77
+ rejected,
78
+ })
43
79
  }
44
80
 
45
81
  export function createReducer(domain, handlers = {}, defaultState) {
@@ -3,7 +3,12 @@ export default function store(key) {
3
3
 
4
4
  return {
5
5
  get() {
6
- return JSON.parse(sessionStorage.getItem(KEY))
6
+ const candidates = [KEY, KEY.split('.').slice(0, -1).join('.')]
7
+ let val
8
+ do {
9
+ val = sessionStorage.getItem(candidates[0])
10
+ } while (candidates.shift() && !val)
11
+ return JSON.parse(val)
7
12
  },
8
13
 
9
14
  set(value) {
@@ -56,7 +56,7 @@ const SeamlyTestCore = ({ state, translations, children }) => {
56
56
  },
57
57
  })
58
58
  newStore.dispatch(ConfigActions.initialize(configSlice || {}))
59
- newStore.dispatch(I18nActions.setLocaleResolve('en-GB', translations))
59
+ newStore.dispatch(I18nActions.setLocale.fulfilled('en-GB', translations))
60
60
  return newStore
61
61
  }, [state, translations])
62
62