@kaspernj/api-maker 1.0.349 → 1.0.351

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.349",
19
+ "version": "1.0.351",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
package/src/api.mjs CHANGED
@@ -10,23 +10,12 @@ const logger = new Logger({name: "ApiMaker / Api"})
10
10
  // logger.setDebug(true)
11
11
 
12
12
  export default class Api {
13
- static get(path, pathParams = null) {
14
- return Api.requestLocal({path, pathParams, method: "GET"})
15
- }
16
-
17
- static delete(path, pathParams = null) {
18
- return Api.requestLocal({path, pathParams, method: "DELETE"})
19
- }
20
-
21
- static patch(path, data = {}) {
22
- return Api.requestLocal({path, data, method: "PATCH"})
23
- }
24
-
25
- static post(path, data = {}) {
26
- return Api.requestLocal({path, data, method: "POST"})
27
- }
13
+ static get = async (path, pathParams = null) => await Api.requestLocal({path, pathParams, method: "GET"})
14
+ static delete = async (path, pathParams = null) => await Api.requestLocal({path, pathParams, method: "DELETE"})
15
+ static patch = async (path, data = {}) => await Api.requestLocal({path, data, method: "PATCH"})
16
+ static post = async (path, data = {}) => await Api.requestLocal({path, data, method: "POST"})
28
17
 
29
- static request({data, headers, method, path, pathParams}) {
18
+ static async request({data, headers, method, path, pathParams}) {
30
19
  let requestPath = ""
31
20
  if (config.getHost()) requestPath += config.getHost()
32
21
  requestPath += path
@@ -36,16 +25,24 @@ export default class Api {
36
25
  requestPath += `?${pathParamsString}`
37
26
  }
38
27
 
39
- return new Promise((resolve, reject) => {
40
- const xhr = new XMLHttpRequest()
41
- xhr.open(method, requestPath, true)
28
+ const xhr = new XMLHttpRequest()
42
29
 
43
- if (headers) {
44
- for (const headerName in headers) {
45
- xhr.setRequestHeader(headerName, headers[headerName])
46
- }
30
+ xhr.open(method, requestPath, true)
31
+ xhr.withCredentials = true
32
+
33
+ if (headers) {
34
+ for (const headerName in headers) {
35
+ xhr.setRequestHeader(headerName, headers[headerName])
47
36
  }
37
+ }
38
+
39
+ const response = await Api.executeXhr(xhr, data)
48
40
 
41
+ return response
42
+ }
43
+
44
+ static executeXhr(xhr, data) {
45
+ return new Promise((resolve, reject) => {
49
46
  xhr.onload = () => {
50
47
  const response = this._parseResponse(xhr)
51
48
 
@@ -93,8 +90,8 @@ export default class Api {
93
90
  return await this.request(args)
94
91
  }
95
92
 
96
- static put(path, data = {}) {
97
- return this.requestLocal({path, data, method: "PUT"})
93
+ static async put(path, data = {}) {
94
+ return await this.requestLocal({path, data, method: "PUT"})
98
95
  }
99
96
 
100
97
  static _token = async () => await SessionStatusUpdater.current().getCsrfToken()
@@ -107,6 +107,10 @@ export default class BaseModel {
107
107
  return new Collection({modelClass: this}, {ransack: query})
108
108
  }
109
109
 
110
+ static select(select) {
111
+ return this.ransack().select(select)
112
+ }
113
+
110
114
  static ransackableAssociations() {
111
115
  const relationships = digg(this.modelClassData(), "ransackable_associations")
112
116
  const reflections = []
package/src/devise.mjs CHANGED
@@ -54,9 +54,7 @@ export default class ApiMakerDevise {
54
54
  }
55
55
 
56
56
  static updateSession(model, args = {}) {
57
- if (!args.scope) {
58
- args.scope = digg(model.modelClassData(), "name")
59
- }
57
+ if (!args.scope) args.scope = "user"
60
58
 
61
59
  const camelizedScopeName = inflection.camelize(args.scope, true)
62
60
 
@@ -3,39 +3,43 @@ import {digg} from "diggerize"
3
3
  import * as inflection from "inflection"
4
4
  import Params from "../params"
5
5
  import PropTypes from "prop-types"
6
- import {memo, useCallback} from "react"
6
+ import {memo, useCallback, useMemo} from "react"
7
7
  import Table from "../table/table"
8
8
  import useCurrentUser from "../use-current-user"
9
+ import useShape from "set-state-compare/src/use-shape.js"
9
10
 
10
- const ApiMakerSuperAdminModelClassTable = ({modelClass, ...restProps}) => {
11
+ const ApiMakerSuperAdminModelClassTable = (props) => {
12
+ const s = useShape(props)
13
+ const {modelClass, ...restProps} = props
11
14
  const currentUser = useCurrentUser()
12
-
13
- const columns = useCallback(() => {
14
- const configReader = ConfigReader.forModel(modelClass)
15
-
16
- return configReader.tableColumns()
17
- }, [modelClass])
15
+ const configReader = useMemo(() => ConfigReader.forModel(modelClass), [modelClass])
16
+ const columns = useMemo(() => configReader.tableColumns(), [modelClass])
17
+ const tableConfig = configReader.modelConfig?.table
18
18
 
19
19
  const editModelPath = useCallback((args) => {
20
- const argName = inflection.camelize(digg(modelClass.modelClassData(), "name"), true)
20
+ const argName = inflection.camelize(digg(s.p.modelClass.modelClassData(), "name"), true)
21
21
  const model = digg(args, argName)
22
22
 
23
23
  return Params.withParams({
24
- model: modelClass.modelClassData().name,
24
+ model: s.p.modelClass.modelClassData().name,
25
25
  model_id: model.primaryKey(),
26
26
  mode: "edit"
27
27
  })
28
- })
28
+ }, [])
29
29
 
30
30
  const viewModelPath = useCallback((args) => {
31
- const argName = inflection.camelize(digg(modelClass.modelClassData(), "name"), true)
31
+ const argName = inflection.camelize(digg(s.p.modelClass.modelClassData(), "name"), true)
32
32
  const model = digg(args, argName)
33
33
 
34
34
  return Params.withParams({
35
- model: modelClass.modelClassData().name,
35
+ model: s.p.modelClass.modelClassData().name,
36
36
  model_id: model.primaryKey()
37
37
  })
38
- }, [modelClass])
38
+ }, [])
39
+
40
+ const tableProps = {}
41
+
42
+ if (tableConfig?.query) tableProps.collection = tableConfig.query
39
43
 
40
44
  return (
41
45
  <Table
@@ -44,6 +48,7 @@ const ApiMakerSuperAdminModelClassTable = ({modelClass, ...restProps}) => {
44
48
  editModelPath={editModelPath}
45
49
  modelClass={modelClass}
46
50
  viewModelPath={viewModelPath}
51
+ {...tableProps}
47
52
  {...restProps}
48
53
  />
49
54
  )
@@ -0,0 +1,49 @@
1
+ import {Platform, useWindowDimensions} from "react-native"
2
+
3
+ const getWindowLayout = (width) => {
4
+ if (width <= 575) {
5
+ return "xs"
6
+ } else if (width <= 767) {
7
+ return "sm"
8
+ } else if (width <= 991) {
9
+ return "md"
10
+ } else if (width <= 1199) {
11
+ return "lg"
12
+ } else if (width <= 1399) {
13
+ return "xl"
14
+ } else if (width >= 1400) {
15
+ return "xxl"
16
+ } else {
17
+ console.error(`Couldn't determine window layout from width: ${width}`)
18
+ }
19
+ }
20
+
21
+ const useScreenSize = () => {
22
+ if (Platform.OS == "web") {
23
+ const shared = useMemo(() => ({}))
24
+
25
+ shared.width = window.innerWidth
26
+
27
+ const [screenLayout, setScreenLayout] = useState(() => getWindowLayout(shared.width))
28
+
29
+ const onResize = useCallback(() => {
30
+ const newWindowLayout = getWindowLayout(window.innerWidth)
31
+
32
+ if (shared.screenlayout != newWindowLayout) {
33
+ setScreenLayout(newWindowLayout)
34
+ }
35
+ }, [])
36
+
37
+ useEventListener(window, "resize", onResize)
38
+
39
+ shared.screenLayout = screenLayout
40
+
41
+ return shared.screenLayout
42
+ } else {
43
+ const windowDimensions = useWindowDimensions()
44
+
45
+ return getWindowLayout(windowDimensions.width)
46
+ }
47
+ }
48
+
49
+ export default useScreenSize