@kaspernj/api-maker 1.0.310 → 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.310",
19
+ "version": "1.0.312",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -52,6 +52,7 @@
52
52
  "on-location-changed": ">= 1.0.8",
53
53
  "qs": ">= 6.9.3",
54
54
  "replaceall": ">= 0.1.6",
55
+ "set-state-compare": ">= 1.0.25",
55
56
  "spark-md5": "^3.0.2",
56
57
  "strftime": ">= 0.10.0",
57
58
  "uniqunize": "^1.0.1",
@@ -3,19 +3,21 @@ 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.js"
6
7
 
7
8
  const ApiMakerSuperAdminLayoutHeader = ({actions, onTriggerMenu, title}) => {
9
+ const shape = useShape()
8
10
  const headerActionsRef = useRef()
9
- const [headerActionsActive, setHeaderActionsActive] = useState(false)
11
+ const setHeaderActionsActive = shape.useState("headerActionsActive", false)
10
12
  const onGearsClicked = useCallback((e) => {
11
13
  e.preventDefault()
12
- setHeaderActionsActive(!headerActionsActive)
13
- }, [headerActionsActive])
14
+ setHeaderActionsActive(!shape.state.headerActionsActive)
15
+ }, [])
14
16
 
15
17
  const onWindowMouseUp = useCallback((e) => {
16
18
  // Close the header actions menu if clicked happened outside
17
- if (headerActionsActive && headerActionsRef.current && !headerActionsRef.current.contains(e.target)) setHeaderActionsActive(false)
18
- }, [headerActionsActive, headerActionsRef])
19
+ if (shape.state.headerActionsActive && headerActionsRef.current && !headerActionsRef.current.contains(e.target)) setHeaderActionsActive(false)
20
+ }, [])
19
21
 
20
22
  return (
21
23
  <div className="components--admin--layout--header">
@@ -24,7 +26,7 @@ const ApiMakerSuperAdminLayoutHeader = ({actions, onTriggerMenu, title}) => {
24
26
  {title}
25
27
  </div>
26
28
  {actions &&
27
- <div className="header-actions-container" data-active={headerActionsActive}>
29
+ <div className="header-actions-container" data-active={shape.state.headerActionsActive}>
28
30
  <div className="header-actions" ref={headerActionsRef}>
29
31
  {actions}
30
32
  </div>
@@ -1,12 +1,14 @@
1
1
  import "./style"
2
2
  import classNames from "classnames"
3
3
  import CommandsPool from "../../commands-pool"
4
+ import config from "super-admin/config"
4
5
  import Header from "./header"
5
6
  import Menu from "./menu"
6
7
  import PropTypes from "prop-types"
7
8
  import PropTypesExact from "prop-types-exact"
8
9
  import {memo, useCallback, useEffect, useState} from "react"
9
10
  import useCurrentUser from "../../use-current-user"
11
+ import useShape from "set-state-compare/src/use-shape.js"
10
12
 
11
13
  const NoAccess = React.lazy(() => import("./no-access"))
12
14
 
@@ -21,6 +23,7 @@ const ApiMakerSuperAdminLayout = ({
21
23
  menu,
22
24
  ...restProps
23
25
  }) => {
26
+ const shape = useShape()
24
27
  const currentUser = useCurrentUser()
25
28
 
26
29
  useEffect(() => {
@@ -36,22 +39,21 @@ const ApiMakerSuperAdminLayout = ({
36
39
  document.title = "Wooftech"
37
40
  }
38
41
 
39
- const [menuTriggered, setMenuTriggered] = useState(false)
42
+ const setMenuTriggered = shape.useState("menuTriggered", false)
40
43
  const noAccess = !currentUser
41
44
  const onRequestMenuClose = useCallback(() => setMenuTriggered(false), [])
42
45
  const onTriggerMenu = useCallback((e) => {
43
46
  e.preventDefault()
44
-
45
- setMenuTriggered(!menuTriggered)
46
- }, [menuTriggered])
47
+ setMenuTriggered(!shape.state.menuTriggered)
48
+ }, [])
47
49
 
48
50
  return (
49
- <div className={classNames("components--admin--layout", className)} data-menu-triggered={menuTriggered} {...restProps}>
51
+ <div className={classNames("components--admin--layout", className)} data-menu-triggered={shape.state.menuTriggered} {...restProps}>
50
52
  <Menu
51
53
  active={active}
52
54
  noAccess={noAccess}
53
55
  onRequestMenuClose={onRequestMenuClose}
54
- triggered={menuTriggered}
56
+ triggered={shape.state.menuTriggered}
55
57
  />
56
58
  <Header actions={actions} onTriggerMenu={onTriggerMenu} title={headerTitle} />
57
59
  <div className="app-layout-content-container">
@@ -70,6 +72,7 @@ const ApiMakerSuperAdminLayout = ({
70
72
  <div className="mb-4">
71
73
  {I18n.t("js.api_maker.super_admin.layout.try_signing_in", {defaultValue: "Try signing in."})}
72
74
  </div>
75
+ {config.signInContent()}
73
76
  </>
74
77
  }
75
78
  </>
@@ -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
- }