@kaspernj/api-maker 1.0.311 → 1.0.312

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.311",
19
+ "version": "1.0.312",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -3,7 +3,7 @@ import EventListener from "../../../event-listener"
3
3
  import PropTypes from "prop-types"
4
4
  import PropTypesExact from "prop-types-exact"
5
5
  import {memo, useCallback, useRef, useState} from "react"
6
- import useShape from "set-state-compare/src/use-shape"
6
+ import useShape from "set-state-compare/src/use-shape.js"
7
7
 
8
8
  const ApiMakerSuperAdminLayoutHeader = ({actions, onTriggerMenu, title}) => {
9
9
  const shape = useShape()
@@ -8,7 +8,7 @@ import PropTypes from "prop-types"
8
8
  import PropTypesExact from "prop-types-exact"
9
9
  import {memo, useCallback, useEffect, useState} from "react"
10
10
  import useCurrentUser from "../../use-current-user"
11
- import useShape from "set-state-compare/src/use-shape"
11
+ import useShape from "set-state-compare/src/use-shape.js"
12
12
 
13
13
  const NoAccess = React.lazy(() => import("./no-access"))
14
14
 
@@ -0,0 +1,38 @@
1
+ import CanCan from "./can-can.mjs"
2
+ import {useCallback, useEffect, useState} from "react"
3
+ import useShape from "set-state-compare/src/use-shape.js"
4
+
5
+ const useCanCan = (abilitiesCallback, dependencies) => {
6
+ const shape = useShape({abilitiesCallback})
7
+ const [canCan, setCanCan] = useState()
8
+
9
+ useEffect(() => {
10
+ loadAbilities()
11
+ }, dependencies)
12
+
13
+ const loadAbilities = useCallback(async () => {
14
+ const canCan = CanCan.current()
15
+ const abilities = shape.props.abilitiesCallback()
16
+
17
+ await canCan.loadAbilities(abilities)
18
+
19
+ setCanCan(canCan)
20
+ }, [])
21
+
22
+ const onResetAbilities = useCallback(() => {
23
+ setCanCan(undefined)
24
+ loadAbilities()
25
+ }, [])
26
+
27
+ useEffect(() => {
28
+ CanCan.current().events.addListener("onResetAbilities", onResetAbilities)
29
+
30
+ return () => {
31
+ CanCan.current().events.removeListener("onResetAbilities", onResetAbilities)
32
+ }
33
+ }, [])
34
+
35
+ return canCan
36
+ }
37
+
38
+ export default useCanCan
@@ -1,16 +1,12 @@
1
- import CanCanLoader from "./can-can-loader"
1
+ import useCanCan from "./use-can-can.mjs"
2
+ import {memo} from "react"
2
3
 
3
- export default (WrappedComponent, abilities) => class WithCanCan extends React.PureComponent {
4
- state = {
5
- canCan: undefined
6
- }
4
+ export default (WrappedComponent, abilities) => {
5
+ const WithCanCan = (props) => {
6
+ const canCan = useCanCan(() => abilities)
7
7
 
8
- render() {
9
- return (
10
- <>
11
- <CanCanLoader abilities={abilities} component={this} />
12
- <WrappedComponent canCan={this.state.canCan} {...this.props} />
13
- </>
14
- )
8
+ return <WrappedComponent canCan={canCan} {...props} />
15
9
  }
10
+
11
+ return memo(WithCanCan)
16
12
  }
@@ -1,54 +0,0 @@
1
- import ApiMakerEventEmitterListener from "./event-emitter-listener"
2
- import {digg, digs} from "diggerize"
3
- import CanCan from "./can-can.mjs"
4
- import PropTypes from "prop-types"
5
- import propTypesExact from "prop-types-exact"
6
- import React from "react"
7
-
8
- export default class ApiMakerCanCanLoader extends React.PureComponent {
9
- static propTypes = propTypesExact({
10
- abilities: PropTypes.array.isRequired,
11
- component: PropTypes.object.isRequired
12
- })
13
-
14
- componentDidMount () {
15
- this.loadAbilities()
16
- }
17
-
18
- async loadAbilities () {
19
- const canCan = CanCan.current()
20
- const {abilities} = digs(this.props, "abilities")
21
-
22
- await canCan.loadAbilities(abilities)
23
-
24
- this.updateComponent({canCan})
25
- }
26
-
27
- render () {
28
- const canCan = CanCan.current()
29
- const events = digg(canCan, "events")
30
-
31
- return (
32
- <ApiMakerEventEmitterListener
33
- event="onResetAbilities"
34
- events={events}
35
- onCalled={this.onResetAbilities}
36
- />
37
- )
38
- }
39
-
40
- onResetAbilities = () => {
41
- this.updateComponent({canCan: undefined})
42
- this.loadAbilities()
43
- }
44
-
45
- updateComponent (updatedState) {
46
- const {component} = digs(this.props, "component")
47
-
48
- if (component.shape) {
49
- component.shape.set(updatedState)
50
- } else {
51
- component.setState(updatedState)
52
- }
53
- }
54
- }