@kaspernj/api-maker 1.0.342 → 1.0.344

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/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  ]
17
17
  },
18
18
  "name": "@kaspernj/api-maker",
19
- "version": "1.0.342",
19
+ "version": "1.0.344",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -70,7 +70,7 @@
70
70
  "babel-jest": "^29.0.1",
71
71
  "eslint": "^8.2.0",
72
72
  "eslint-find-rules": "^4.0.0",
73
- "eslint-plugin-jest": "^27.0.1",
73
+ "eslint-plugin-jest": "^28.2.0",
74
74
  "eslint-plugin-react": "^7.23.2",
75
75
  "i18n-on-steroids": "^1.0.7",
76
76
  "jest": "^29.0.1",
@@ -460,7 +460,7 @@ export default class BaseModel {
460
460
  BaseModel.sendValidationErrorsEvent(validationErrors, options)
461
461
 
462
462
  if (!options || options.throwValidationError != false) {
463
- throw new ValidationError(validationErrors, digg(error, "args"))
463
+ throw error
464
464
  }
465
465
  }
466
466
 
@@ -1,3 +1,4 @@
1
+ import config from "./config.mjs"
1
2
  import Devise from "./devise.mjs"
2
3
  import * as inflection from "inflection"
3
4
  import Logger from "./logger.mjs"
@@ -22,17 +23,23 @@ export default class ApiMakerSessionStatusUpdater {
22
23
  }
23
24
 
24
25
  connectOnlineEvent () {
25
- window.addEventListener("online", () => this.updateSessionStatus(), false)
26
+ window.addEventListener("online", this.updateSessionStatus, false)
26
27
  }
27
28
 
28
29
  connectWakeEvent () {
29
- wakeEvent(() => this.updateSessionStatus())
30
+ wakeEvent(updateSessionStatus)
30
31
  }
31
32
 
32
33
  async sessionStatus () {
33
34
  return new Promise((resolve) => {
35
+ const requestPath = ""
36
+
37
+ if (config.getHost()) requestPath += config.getHost()
38
+
39
+ requestPath += "/api_maker/session_statuses"
40
+
34
41
  const xhr = new XMLHttpRequest()
35
- xhr.open("POST", "/api_maker/session_statuses", true)
42
+ xhr.open("POST", requestPath, true)
36
43
  xhr.onload = () => {
37
44
  const response = JSON.parse(xhr.responseText)
38
45
  resolve(response)
@@ -65,7 +72,7 @@ export default class ApiMakerSessionStatusUpdater {
65
72
  clearTimeout(this.updateTimeout)
66
73
  }
67
74
 
68
- async updateSessionStatus () {
75
+ updateSessionStatus = async () => {
69
76
  logger.debug("updateSessionStatus")
70
77
 
71
78
  const result = await this.sessionStatus()
package/src/use-input.mjs CHANGED
@@ -1,16 +1,15 @@
1
- import {dig, digg, digs} from "diggerize"
1
+ import {dig, digg} from "diggerize"
2
2
  import {useCallback, useEffect, useMemo} from "react"
3
3
  import idForComponent from "./inputs/id-for-component.mjs"
4
4
  import nameForComponent from "./inputs/name-for-component.mjs"
5
5
  import strftime from "strftime"
6
- import useEventListener from "./use-event-listener.mjs"
7
6
  import useShape from "set-state-compare/src/use-shape.js"
7
+ import useValidationErrors from "./use-validation-errors.mjs"
8
8
 
9
9
  const useInput = ({props, wrapperOptions}) => {
10
10
  const s = useShape(props)
11
11
 
12
12
  s.useStates({
13
- errors: [],
14
13
  form: undefined
15
14
  })
16
15
 
@@ -73,12 +72,6 @@ const useInput = ({props, wrapperOptions}) => {
73
72
  }
74
73
  }, [])
75
74
 
76
- const inputName = useCallback(() => {
77
- if (s.state.blankInputName) return ""
78
-
79
- return getName()
80
- }, [])
81
-
82
75
  const inputRefBackup = useCallback(() => {
83
76
  if (!s.meta._inputRefBackup) s.meta._inputRefBackup = React.createRef()
84
77
 
@@ -105,16 +98,6 @@ const useInput = ({props, wrapperOptions}) => {
105
98
  }
106
99
  }, [])
107
100
 
108
- const onValidationErrors = useCallback((event) => {
109
- const errors = event.detail.getValidationErrorsForInput({
110
- attribute: s.props.attribute,
111
- inputName: inputName(),
112
- onMatchValidationError: s.props.onMatchValidationError
113
- })
114
-
115
- s.set({errors})
116
- }, [])
117
-
118
101
  const setForm = useCallback(() => {
119
102
  const inputElement = inputRef().current
120
103
 
@@ -155,21 +138,25 @@ const useInput = ({props, wrapperOptions}) => {
155
138
 
156
139
  const {inputProps: oldInputProps, ...restProps} = props
157
140
  const type = inputType()
158
- const inputProps = getInputProps()
159
141
 
160
- if (!inputProps.ref) throw new Error("No input ref?")
161
- if (!handleAsSelect()) inputProps.type = type
142
+ s.meta.inputProps = getInputProps()
143
+ s.meta.inputNameWithoutId = useMemo(() => s.m.inputProps.name?.replace(/\[(.+)_id\]$/, "[$1]"), [s.m.inputProps.name])
144
+
145
+ if (!s.m.inputProps.ref) throw new Error("No input ref?")
146
+ if (!handleAsSelect()) s.m.inputProps.type = type
147
+
148
+ const {validationErrors} = useValidationErrors((validationError) =>
149
+ validationError.inputName == s.m.inputProps.name || validationError.inputName == s.m.inputNameWithoutId
150
+ )
162
151
 
163
152
  const wrapperOpts = {
164
- errors: s.s.errors,
153
+ errors: validationErrors,
165
154
  form: s.s.form,
166
155
  label: label()
167
156
  }
168
157
 
169
- useEventListener(s.s.form, "validation-errors", onValidationErrors)
170
-
171
158
  return {
172
- inputProps,
159
+ inputProps: s.m.inputProps,
173
160
  wrapperOpts,
174
161
  restProps
175
162
  }
@@ -1,4 +1,5 @@
1
1
  import events from "./events.mjs"
2
+ import {useCallback} from "react"
2
3
  import useEventEmitter from "./use-event-emitter.mjs"
3
4
  import useShape from "set-state-compare/src/use-shape.js"
4
5
 
@@ -11,6 +11,9 @@ class ValidationError extends BaseError {
11
11
  this.validationErrors = validationErrors
12
12
  }
13
13
 
14
+ getUnhandledErrors = () => this.validationErrors.getValidationErrors().filter((validationError) => !validationError.getHandled())
15
+ getValidationErrors = () => digg(this, "validationErrors")
16
+
14
17
  hasUnhandledErrors() {
15
18
  const unhandledError = this.validationErrors.getValidationErrors().find((validationError) => !validationError.getHandled())
16
19
 
@@ -89,7 +89,7 @@ class ValidationErrors {
89
89
  if (onMatchValidationError) {
90
90
  return onMatchValidationError(validationError)
91
91
  } else {
92
- return validationError.matchesAttributeAndInputName(attribute, inputName, onMatchValidationError)
92
+ return validationError.matchesAttributeAndInputName(attribute, inputName)
93
93
  }
94
94
  })
95
95