|
| 1 | +import { Alert, Button, Hyperlink } from '@openedx/paragon'; |
| 2 | +import { Policy } from '@openedx/paragon/icons'; |
| 3 | +import { |
| 4 | + getGatingAgreementType, |
| 5 | + useUserAgreement, |
| 6 | + useUserAgreementRecord, useUserAgreementRecordUpdater, |
| 7 | +} from '@src/data/apiHooks'; |
| 8 | + |
| 9 | +const AlertAgreement = ({ agreementType }: { agreementType: string }) => { |
| 10 | + const { data, isLoading, isError } = useUserAgreement(agreementType); |
| 11 | + const mutation = useUserAgreementRecordUpdater(agreementType); |
| 12 | + const showAlert = !data && !isLoading && !isError; |
| 13 | + if (!showAlert) { return null; } |
| 14 | + const { url, name, summary } = data; |
| 15 | + return ( |
| 16 | + <Alert |
| 17 | + variant="warning" |
| 18 | + icon={Policy} |
| 19 | + actions={[ |
| 20 | + <Hyperlink destination={url}>Learn more</Hyperlink>, |
| 21 | + <Button onClick={async () => mutation.mutateAsync()}>Agree</Button>, |
| 22 | + ]} |
| 23 | + > |
| 24 | + <Alert.Heading>{name}</Alert.Heading> |
| 25 | + {summary} |
| 26 | + </Alert> |
| 27 | + ); |
| 28 | +}; |
| 29 | + |
| 30 | +const AlertAgreementWrapper = ({ agreementType }: { agreementType: string }) => { |
| 31 | + const { data, isLoading, isError } = useUserAgreementRecord(agreementType); |
| 32 | + const showAlert = !data && !isLoading && !isError; |
| 33 | + if (!showAlert) { return null; } |
| 34 | + return <AlertAgreement agreementType={agreementType} />; |
| 35 | +}; |
| 36 | + |
| 37 | +export const AlertAgreementGatedFeature = ({ gatingType }: { gatingType: string }) => { |
| 38 | + const agreementType = getGatingAgreementType(gatingType); |
| 39 | + if (!agreementType) { return null; } |
| 40 | + return <AlertAgreementWrapper agreementType={agreementType} />; |
| 41 | +}; |
0 commit comments