Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/check organization permission #27

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Josmar-jr marked this conversation as resolved.
Show resolved Hide resolved

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
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