Skip to content

Commit

Permalink
Merge pull request #27 from Josmar-jr/feat/check-organization-permission
Browse files Browse the repository at this point in the history
Feat/check organization permission
  • Loading branch information
ataideverton authored Oct 14, 2024
2 parents d1ec9ab + fa462d1 commit 42d9a90
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added
- Create a check organization permission component

## [1.2.1] - 2023-05-26

### Added
Expand Down
4 changes: 3 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"vtex.css-handles": "1.x",
"vtex.styleguide": "9.x",
"vtex.admin-customers-graphql": "3.x",
"vtex.storefront-permissions": "1.x"
"vtex.storefront-permissions": "1.x",
"vtex.b2b-organizations-graphql": "0.x",
"vtex.b2b-organizations": "1.x"
},
"builders": {
"react": "3.x",
Expand Down
78 changes: 78 additions & 0 deletions react/CheckOrganizationPermission.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React from 'react'
import type { ElementType } from 'react'
import { ExtensionPoint } from 'vtex.render-runtime'
import { useQuery } from 'react-apollo'

import getOrganizationPermissions from './queries/getOrganizationPermissions.gql'

type OrganizationRole = 'createQuote'

interface Props {
roles: OrganizationRole[]
AllowedContent?: ElementType
DisallowedContent?: ElementType
LoadingContent?: ElementType
}

function CheckOrganizationPermission({
roles = [],
AllowedContent,
DisallowedContent,
LoadingContent,
}: Props) {
const { data, called, error, loading } = useQuery(
getOrganizationPermissions,
{
ssr: false,
skip: !roles.length,
onCompleted(insideData) {
if (insideData?.getOrganizationByIdStorefront?.permissions) {
sessionStorage.setItem(
'checkout.createQuote',
JSON.stringify(
insideData?.getOrganizationByIdStorefront?.permissions.createQuote
)
)
}
},
}
)

if (error) {
console.error('CheckOrganizationPermission component error:', error)

return null
}

if (called && loading) {
return LoadingContent ? (
<LoadingContent />
) : (
<ExtensionPoint id="loading-content" />
)
}

if (!roles.length || !data) {
return null
}

const hasPermission = roles.every(
(key) => data.getOrganizationByIdStorefront.permissions[key] === true
)

if (hasPermission) {
return AllowedContent ? (
<AllowedContent />
) : (
<ExtensionPoint id="allowed-content" />
)
}

return DisallowedContent ? (
<DisallowedContent />
) : (
<ExtensionPoint id="disallowed-content" />
)
}

export default CheckOrganizationPermission
8 changes: 8 additions & 0 deletions react/queries/getOrganizationPermissions.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query GetOrganizationStorefront($id: ID) {
getOrganizationByIdStorefront(id: $id)
@context(provider: "vtex.b2b-organizations-graphql") {
permissions {
createQuote
}
}
}
4 changes: 4 additions & 0 deletions store/interfaces.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"check-organization-permission": {
"component": "CheckOrganizationPermission",
"allowed": ["allowed-content", "disallowed-content", "loading-content"]
},
"check-permission": {
"component": "CheckPermission",
"allowed": ["allowed-content", "disallowed-content", "loading-content"]
Expand Down

0 comments on commit 42d9a90

Please sign in to comment.