From 44a0cc0ddf2c09ddf085db22561e674ae1c453bd Mon Sep 17 00:00:00 2001 From: Josmar Soares Trigueiro Junior Date: Mon, 9 Sep 2024 10:27:48 -0300 Subject: [PATCH 1/4] feat: create a check organization permission component and set on interfaces.json file --- manifest.json | 4 +- react/CheckOrganizationPermission.tsx | 78 ++++++++++++++++++++ react/queries/getOrganizationPermissions.gql | 9 +++ store/interfaces.json | 4 + 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 react/CheckOrganizationPermission.tsx create mode 100644 react/queries/getOrganizationPermissions.gql diff --git a/manifest.json b/manifest.json index c7503b5..651db15 100644 --- a/manifest.json +++ b/manifest.json @@ -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", diff --git a/react/CheckOrganizationPermission.tsx b/react/CheckOrganizationPermission.tsx new file mode 100644 index 0000000..b2e83df --- /dev/null +++ b/react/CheckOrganizationPermission.tsx @@ -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 error:', error) + + return null + } + + if (called && loading) { + return LoadingContent ? ( + + ) : ( + + ) + } + + if (!roles.length || !data) { + return null + } + + const hasPermission = roles.every( + (key) => data.getOrganizationByIdStorefront.permissions[key] === true + ) + + if (hasPermission) { + return AllowedContent ? ( + + ) : ( + + ) + } + + return DisallowedContent ? ( + + ) : ( + + ) +} + +export default CheckOrganizationPermission diff --git a/react/queries/getOrganizationPermissions.gql b/react/queries/getOrganizationPermissions.gql new file mode 100644 index 0000000..702556b --- /dev/null +++ b/react/queries/getOrganizationPermissions.gql @@ -0,0 +1,9 @@ +query GetOrganizationStorefront($id: ID) { + getOrganizationByIdStorefront(id: $id) + @context(provider: "vtex.b2b-organizations-graphql") { + id + permissions { + createQuote + } + } +} diff --git a/store/interfaces.json b/store/interfaces.json index 9fccdbe..087a7cf 100644 --- a/store/interfaces.json +++ b/store/interfaces.json @@ -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"] From 6b8573a9b13f7319c123127cd0628798b9947717 Mon Sep 17 00:00:00 2001 From: Josmar Soares Trigueiro Junior Date: Mon, 9 Sep 2024 11:48:15 -0300 Subject: [PATCH 2/4] feat: create a check organization permission component and set on interfaces.json file --- react/CheckOrganizationPermission.tsx | 2 +- react/queries/getOrganizationPermissions.gql | 9 --------- 2 files changed, 1 insertion(+), 10 deletions(-) delete mode 100644 react/queries/getOrganizationPermissions.gql diff --git a/react/CheckOrganizationPermission.tsx b/react/CheckOrganizationPermission.tsx index b2e83df..c0d2815 100644 --- a/react/CheckOrganizationPermission.tsx +++ b/react/CheckOrganizationPermission.tsx @@ -39,7 +39,7 @@ function CheckOrganizationPermission({ ) if (error) { - console.error('CheckOrganizationPermission error:', error) + console.error('CheckOrganizationPermission component error:', error) return null } diff --git a/react/queries/getOrganizationPermissions.gql b/react/queries/getOrganizationPermissions.gql deleted file mode 100644 index 702556b..0000000 --- a/react/queries/getOrganizationPermissions.gql +++ /dev/null @@ -1,9 +0,0 @@ -query GetOrganizationStorefront($id: ID) { - getOrganizationByIdStorefront(id: $id) - @context(provider: "vtex.b2b-organizations-graphql") { - id - permissions { - createQuote - } - } -} From f623d285a72cf7b8fde8cfcbad79e7ecbd03d249 Mon Sep 17 00:00:00 2001 From: Josmar Soares Trigueiro Junior Date: Mon, 9 Sep 2024 11:49:04 -0300 Subject: [PATCH 3/4] doc: update change log file --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a311202..92fa394 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From fa462d1ad4543176159e7ccc8e2bd98efa59e0f1 Mon Sep 17 00:00:00 2001 From: Josmar Soares Trigueiro Junior Date: Tue, 24 Sep 2024 15:21:18 -0300 Subject: [PATCH 4/4] fix: add getOrganizationPermissions function --- react/queries/getOrganizationPermissions.gql | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 react/queries/getOrganizationPermissions.gql diff --git a/react/queries/getOrganizationPermissions.gql b/react/queries/getOrganizationPermissions.gql new file mode 100644 index 0000000..cb30344 --- /dev/null +++ b/react/queries/getOrganizationPermissions.gql @@ -0,0 +1,8 @@ +query GetOrganizationStorefront($id: ID) { + getOrganizationByIdStorefront(id: $id) + @context(provider: "vtex.b2b-organizations-graphql") { + permissions { + createQuote + } + } +}