Skip to content

Commit 42d9a90

Browse files
authored
Merge pull request #27 from Josmar-jr/feat/check-organization-permission
Feat/check organization permission
2 parents d1ec9ab + fa462d1 commit 42d9a90

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Added
11+
- Create a check organization permission component
12+
1013
## [1.2.1] - 2023-05-26
1114

1215
### Added

manifest.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"vtex.css-handles": "1.x",
1414
"vtex.styleguide": "9.x",
1515
"vtex.admin-customers-graphql": "3.x",
16-
"vtex.storefront-permissions": "1.x"
16+
"vtex.storefront-permissions": "1.x",
17+
"vtex.b2b-organizations-graphql": "0.x",
18+
"vtex.b2b-organizations": "1.x"
1719
},
1820
"builders": {
1921
"react": "3.x",
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import React from 'react'
2+
import type { ElementType } from 'react'
3+
import { ExtensionPoint } from 'vtex.render-runtime'
4+
import { useQuery } from 'react-apollo'
5+
6+
import getOrganizationPermissions from './queries/getOrganizationPermissions.gql'
7+
8+
type OrganizationRole = 'createQuote'
9+
10+
interface Props {
11+
roles: OrganizationRole[]
12+
AllowedContent?: ElementType
13+
DisallowedContent?: ElementType
14+
LoadingContent?: ElementType
15+
}
16+
17+
function CheckOrganizationPermission({
18+
roles = [],
19+
AllowedContent,
20+
DisallowedContent,
21+
LoadingContent,
22+
}: Props) {
23+
const { data, called, error, loading } = useQuery(
24+
getOrganizationPermissions,
25+
{
26+
ssr: false,
27+
skip: !roles.length,
28+
onCompleted(insideData) {
29+
if (insideData?.getOrganizationByIdStorefront?.permissions) {
30+
sessionStorage.setItem(
31+
'checkout.createQuote',
32+
JSON.stringify(
33+
insideData?.getOrganizationByIdStorefront?.permissions.createQuote
34+
)
35+
)
36+
}
37+
},
38+
}
39+
)
40+
41+
if (error) {
42+
console.error('CheckOrganizationPermission component error:', error)
43+
44+
return null
45+
}
46+
47+
if (called && loading) {
48+
return LoadingContent ? (
49+
<LoadingContent />
50+
) : (
51+
<ExtensionPoint id="loading-content" />
52+
)
53+
}
54+
55+
if (!roles.length || !data) {
56+
return null
57+
}
58+
59+
const hasPermission = roles.every(
60+
(key) => data.getOrganizationByIdStorefront.permissions[key] === true
61+
)
62+
63+
if (hasPermission) {
64+
return AllowedContent ? (
65+
<AllowedContent />
66+
) : (
67+
<ExtensionPoint id="allowed-content" />
68+
)
69+
}
70+
71+
return DisallowedContent ? (
72+
<DisallowedContent />
73+
) : (
74+
<ExtensionPoint id="disallowed-content" />
75+
)
76+
}
77+
78+
export default CheckOrganizationPermission
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
query GetOrganizationStorefront($id: ID) {
2+
getOrganizationByIdStorefront(id: $id)
3+
@context(provider: "vtex.b2b-organizations-graphql") {
4+
permissions {
5+
createQuote
6+
}
7+
}
8+
}

store/interfaces.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"check-organization-permission": {
3+
"component": "CheckOrganizationPermission",
4+
"allowed": ["allowed-content", "disallowed-content", "loading-content"]
5+
},
26
"check-permission": {
37
"component": "CheckPermission",
48
"allowed": ["allowed-content", "disallowed-content", "loading-content"]

0 commit comments

Comments
 (0)