-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial implimentation to render a block based on condition * refactor BlockPlaceholderWrapper * conditional block implemmentation and blockwrapper refactoring * conditional block implementation fixed, lacking translations only * use translation in conditional block * fix translations
- Loading branch information
Showing
23 changed files
with
344 additions
and
43 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
113 changes: 113 additions & 0 deletions
113
services/cms/src/blocks/ConditionalBlock/ConditionalBlockEditor.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,113 @@ | ||
import styled from "@emotion/styled" | ||
import { useQuery } from "@tanstack/react-query" | ||
import { InnerBlocks, InspectorControls } from "@wordpress/block-editor" | ||
import { BlockEditProps } from "@wordpress/blocks" | ||
import React, { useContext, useState } from "react" | ||
import { useTranslation } from "react-i18next" | ||
|
||
import PageContext from "../../contexts/PageContext" | ||
import { fetchCourseInstances } from "../../services/backend/course-instances" | ||
import { fetchCourseModulesByCourseId } from "../../services/backend/courses" | ||
import CheckBox from "../../shared-module/components/InputFields/CheckBox" | ||
import { assertNotNullOrUndefined } from "../../shared-module/utils/nullability" | ||
import BlockPlaceholderWrapper from "../BlockPlaceholderWrapper" | ||
|
||
import { ConditionAttributes } from "." | ||
|
||
const ALLOWED_NESTED_BLOCKS = ["core/heading", "core/buttons", "core/button", "core/paragraph"] | ||
|
||
const Wrapper = styled.div` | ||
margin-left: 1rem; | ||
margin-right: 1rem; | ||
height: auto; | ||
` | ||
const Text = styled.p` | ||
padding-bottom: 1rem; | ||
` | ||
|
||
const ConditionalBlockEditor: React.FC< | ||
React.PropsWithChildren<BlockEditProps<ConditionAttributes>> | ||
> = ({ attributes, clientId, setAttributes }) => { | ||
const { t } = useTranslation() | ||
const courseId = useContext(PageContext)?.page.course_id | ||
const courseModules = useQuery({ | ||
queryKey: [`/courses/${courseId}/modules`], | ||
queryFn: () => fetchCourseModulesByCourseId(assertNotNullOrUndefined(courseId)), | ||
enabled: !!courseId, | ||
}) | ||
|
||
const courseInstances = useQuery({ | ||
queryKey: [`/courses/${courseId}/course-instances`], | ||
queryFn: () => fetchCourseInstances(assertNotNullOrUndefined(courseId)), | ||
enabled: !!courseId, | ||
}) | ||
const [requiredModules, setRequiredModules] = useState<string[]>(attributes.module_completion) | ||
const [requiredInstanceEnrollment, setRequiredInstanceEnrollment] = useState<string[]>( | ||
attributes.instance_enrollment, | ||
) | ||
return ( | ||
<BlockPlaceholderWrapper | ||
id={clientId} | ||
title={t("conditional-block")} | ||
explanation={t("conditional-block-explanation")} | ||
> | ||
<InspectorControls> | ||
{courseModules.data && ( | ||
<Wrapper> | ||
<Text>{t("module-completion-condition")}</Text> | ||
{courseModules.data.map((mod) => { | ||
return ( | ||
<CheckBox | ||
key={mod.id} | ||
label={mod.name ?? "Default"} | ||
value={mod.id} | ||
onChange={() => { | ||
const previuoslyChecked = requiredModules.some((modId) => modId == mod.id) | ||
const newRequiredModules = requiredModules.filter((i) => i != mod.id) | ||
if (!previuoslyChecked) { | ||
newRequiredModules.push(mod.id) | ||
} | ||
setAttributes({ module_completion: newRequiredModules }) | ||
setRequiredModules(newRequiredModules) | ||
}} | ||
checked={requiredModules.some((modId) => modId == mod.id)} | ||
></CheckBox> | ||
) | ||
})} | ||
</Wrapper> | ||
)} | ||
{courseInstances.data && ( | ||
<Wrapper> | ||
<Text>{t("course-instance-enrollment-condition")}</Text> | ||
{courseInstances.data.map((inst) => { | ||
return ( | ||
<CheckBox | ||
key={inst.id} | ||
label={inst.name ?? "Default"} | ||
value={inst.id} | ||
onChange={() => { | ||
const previuoslyChecked = requiredInstanceEnrollment.some( | ||
(instId) => instId == inst.id, | ||
) | ||
const newRequiredInstEnrl = requiredInstanceEnrollment.filter( | ||
(i) => i != inst.id, | ||
) | ||
if (!previuoslyChecked) { | ||
newRequiredInstEnrl.push(inst.id) | ||
} | ||
setAttributes({ instance_enrollment: newRequiredInstEnrl }) | ||
setRequiredInstanceEnrollment(newRequiredInstEnrl) | ||
}} | ||
checked={requiredInstanceEnrollment.some((instId) => instId == inst.id)} | ||
></CheckBox> | ||
) | ||
})} | ||
</Wrapper> | ||
)} | ||
</InspectorControls> | ||
<InnerBlocks allowedBlocks={ALLOWED_NESTED_BLOCKS} /> | ||
</BlockPlaceholderWrapper> | ||
) | ||
} | ||
|
||
export default ConditionalBlockEditor |
11 changes: 11 additions & 0 deletions
11
services/cms/src/blocks/ConditionalBlock/ConditionalBlockSave.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,11 @@ | ||
import { InnerBlocks } from "@wordpress/block-editor" | ||
|
||
const ConditionalBlockSave: React.FC = () => { | ||
return ( | ||
<div> | ||
<InnerBlocks.Content /> | ||
</div> | ||
) | ||
} | ||
|
||
export default ConditionalBlockSave |
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 { BlockConfiguration } from "@wordpress/blocks" | ||
|
||
import { MOOCFI_CATEGORY_SLUG } from "../../utils/Gutenberg/modifyGutenbergCategories" | ||
|
||
import ConditionalBlockEditor from "./ConditionalBlockEditor" | ||
import ConditionalBlockSave from "./ConditionalBlockSave" | ||
|
||
export interface ConditionAttributes { | ||
module_completion: string[] | ||
instance_enrollment: string[] | ||
} | ||
|
||
const ConditionalBlockConfiguration: BlockConfiguration<ConditionAttributes> = { | ||
title: "ConditionalBlock", | ||
description: "Conditionally shown block", | ||
category: MOOCFI_CATEGORY_SLUG, | ||
attributes: { | ||
module_completion: { | ||
type: "array", | ||
default: [], | ||
}, | ||
instance_enrollment: { | ||
type: "array", | ||
default: [], | ||
}, | ||
}, | ||
edit: ConditionalBlockEditor, | ||
save: ConditionalBlockSave, | ||
} | ||
|
||
export default ConditionalBlockConfiguration |
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
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
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.