-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
199 additions
and
145 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
42 changes: 42 additions & 0 deletions
42
services/course-material/src/components/ContentRenderer/moocfi/CodeGiveAway/ClaimCode.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,42 @@ | ||
import { useTranslation } from "react-i18next" | ||
|
||
import { claimCodeFromCodeGiveaway } from "@/services/backend" | ||
import Button from "@/shared-module/common/components/Button" | ||
import ErrorBanner from "@/shared-module/common/components/ErrorBanner" | ||
import useToastMutation from "@/shared-module/common/hooks/useToastMutation" | ||
import { assertNotNullOrUndefined } from "@/shared-module/common/utils/nullability" | ||
|
||
interface ClaimCodeProps { | ||
codeGiveawayId: string | ||
onClaimed: () => void | ||
} | ||
|
||
const ClaimCode: React.FC<ClaimCodeProps> = ({ codeGiveawayId, onClaimed }) => { | ||
const { t } = useTranslation() | ||
|
||
const claimCodeMutation = useToastMutation( | ||
() => claimCodeFromCodeGiveaway(assertNotNullOrUndefined(codeGiveawayId)), | ||
{ notify: false }, | ||
) | ||
|
||
return ( | ||
<> | ||
{claimCodeMutation.isError && ( | ||
<ErrorBanner error={claimCodeMutation.error} variant="readOnly" /> | ||
)} | ||
<Button | ||
onClick={async () => { | ||
await claimCodeMutation.mutateAsync() | ||
onClaimed() | ||
}} | ||
variant="primary" | ||
size="medium" | ||
disabled={claimCodeMutation.isPending} | ||
> | ||
{t("claim-code")} | ||
</Button> | ||
</> | ||
) | ||
} | ||
|
||
export default ClaimCode |
77 changes: 77 additions & 0 deletions
77
services/course-material/src/components/ContentRenderer/moocfi/CodeGiveAway/index.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,77 @@ | ||
import styled from "@emotion/styled" | ||
import { useQuery } from "@tanstack/react-query" | ||
import { useTranslation } from "react-i18next" | ||
|
||
import { BlockRendererProps } from "../.." | ||
import InnerBlocks from "../../util/InnerBlocks" | ||
|
||
import ClaimCode from "./ClaimCode" | ||
|
||
import { getCodeGiveawayStatus } from "@/services/backend" | ||
import ErrorBanner from "@/shared-module/common/components/ErrorBanner" | ||
import { assertNotNullOrUndefined } from "@/shared-module/common/utils/nullability" | ||
|
||
interface CodeGiveawayBlockProps { | ||
code_giveaway_id: string | undefined | null | ||
} | ||
|
||
const Wrapper = styled.div` | ||
border: 1px solid #ccc; | ||
border-radius: 5px; | ||
padding: 1rem; | ||
` | ||
|
||
const CodeGiveawayBlock: React.FC< | ||
React.PropsWithChildren<BlockRendererProps<CodeGiveawayBlockProps>> | ||
> = (props) => { | ||
const { t } = useTranslation() | ||
|
||
const codeGiveawayId = props.data.attributes.code_giveaway_id | ||
|
||
const codeGiveawayStatusQuery = useQuery({ | ||
queryKey: ["fetchCodeGiveawayStatus", codeGiveawayId], | ||
queryFn: () => getCodeGiveawayStatus(assertNotNullOrUndefined(codeGiveawayId)), | ||
enabled: !!codeGiveawayId, | ||
}) | ||
|
||
if (!codeGiveawayId) { | ||
return <ErrorBanner variant="readOnly" error={t("error-no-code-giveaway-id")} /> | ||
} | ||
|
||
if (codeGiveawayStatusQuery.isLoading) { | ||
return null | ||
} | ||
|
||
if (codeGiveawayStatusQuery.isError) { | ||
return <ErrorBanner error={codeGiveawayStatusQuery.error} variant="readOnly" /> | ||
} | ||
|
||
if ( | ||
codeGiveawayStatusQuery.data?.tag === "Disabled" || | ||
codeGiveawayStatusQuery.data?.tag === "NotEligible" || | ||
!codeGiveawayStatusQuery.data | ||
) { | ||
return null | ||
} | ||
|
||
if (codeGiveawayStatusQuery.data?.tag === "Eligible") { | ||
return ( | ||
<Wrapper> | ||
<InnerBlocks parentBlockProps={props} /> | ||
<ClaimCode | ||
codeGiveawayId={codeGiveawayId} | ||
onClaimed={() => codeGiveawayStatusQuery.refetch()} | ||
/> | ||
</Wrapper> | ||
) | ||
} | ||
|
||
return ( | ||
<Wrapper> | ||
<InnerBlocks parentBlockProps={props} /> | ||
<p>{t("your-code-code", { code: codeGiveawayStatusQuery.data.given_code })}</p> | ||
</Wrapper> | ||
) | ||
} | ||
|
||
export default CodeGiveawayBlock |
68 changes: 0 additions & 68 deletions
68
services/course-material/src/components/ContentRenderer/moocfi/CodeGiveaway.tsx
This file was deleted.
Oops, something went wrong.
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
4 changes: 2 additions & 2 deletions
4
...fb83c88ca543b071e71dfe39aa31a9ffecda.json → ...df20dbcc60f879124b6462b8c7dc21eaf260.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.