-
Notifications
You must be signed in to change notification settings - Fork 28
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 #3839 from guardian/oliver/discussion-island
`Discussion` as an island
- Loading branch information
Showing
14 changed files
with
188 additions
and
274 deletions.
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
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
31 changes: 31 additions & 0 deletions
31
dotcom-rendering/src/web/components/DiscussionContainer.importable.tsx
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,31 @@ | ||
import { getCookie } from '@guardian/libs'; | ||
import { Discussion } from '@root/src/web/components/Discussion'; | ||
import { DiscussionWhenSignedIn } from '@root/src/web/components/DiscussionWhenSignedIn'; | ||
import type { Props as DiscussionProps } from 'src/web/components/Discussion'; | ||
|
||
/** | ||
* DiscussionContainer | ||
* | ||
* A wrapper component that decides if the user is signed in or not. | ||
* | ||
* If they | ||
* are, it renders DiscussionWhenSignedIn which includes an api call to fetch | ||
* the user profile. | ||
* | ||
* If not, it simply renders Discussion | ||
* | ||
* We use component composition like this here because you cannoy call react | ||
* hooks conditionally and we're using a hook to make the fetch request | ||
* | ||
* Note. We allow the ...props pattern here because it makes sense when we're | ||
* just passing them through | ||
* | ||
*/ | ||
// eslint-disable-next-line react/destructuring-assignment | ||
export const DiscussionContainer = (props: DiscussionProps) => { | ||
const isSignedIn = !!getCookie({ name: 'GU_U', shouldMemoize: true }); | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
if (isSignedIn) return <DiscussionWhenSignedIn {...props} />; | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
return <Discussion {...props} />; | ||
}; |
19 changes: 19 additions & 0 deletions
19
dotcom-rendering/src/web/components/DiscussionWhenSignedIn.tsx
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,19 @@ | ||
import { joinUrl } from '@guardian/libs'; | ||
import { Discussion } from '@root/src/web/components/Discussion'; | ||
import type { Props as DiscussionProps } from 'src/web/components/Discussion'; | ||
import { useApi } from '../lib/useApi'; | ||
|
||
// eslint-disable-next-line react/destructuring-assignment | ||
export const DiscussionWhenSignedIn = (props: DiscussionProps) => { | ||
const { discussionApiUrl } = props; | ||
const { data } = useApi<{ userProfile: UserProfile }>( | ||
joinUrl(discussionApiUrl, 'profile/me?strict_sanctions_check=false'), | ||
{}, | ||
{ | ||
credentials: 'include', | ||
}, | ||
); | ||
if (!data) return null; | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
return <Discussion user={data.userProfile} {...props} />; | ||
}; |
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
Oops, something went wrong.