-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Josmar-jr/feat/check-organization-permission
Feat/check organization permission
- Loading branch information
Showing
5 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters