ENCOA-274

This commit is contained in:
Carlos-Mesquita
2024-12-11 15:28:38 +00:00
parent 7538392e44
commit efba1939e5
12 changed files with 344 additions and 130 deletions

View File

@@ -1,4 +1,4 @@
import React, { useCallback, useState } from "react";
import React, { useCallback, useRef, useState } from "react";
import Dropdown from "../Shared/SettingsDropdown";
import Input from "@/components/Low/Input";
import { generate } from "../Shared/Generate";
@@ -6,6 +6,8 @@ import GenerateBtn from "../Shared/GenerateBtn";
import { LevelSectionSettings, WritingSectionSettings } from "@/stores/examEditor/types";
import useExamEditorStore from "@/stores/examEditor";
import { WritingExercise } from "@/interfaces/exam";
import clsx from "clsx";
import { FaFileUpload } from "react-icons/fa";
interface Props {
@@ -15,69 +17,155 @@ interface Props {
level?: boolean;
}
const WritingComponents: React.FC<Props> = ({localSettings, updateLocalAndScheduleGlobal, level}) => {
const { currentModule } = useExamEditorStore();
const WritingComponents: React.FC<Props> = ({ localSettings, updateLocalAndScheduleGlobal, level }) => {
const { currentModule, dispatch } = useExamEditorStore();
const {
difficulty,
focusedSection,
type,
academic_url
} = useExamEditorStore((store) => store.modules["writing"]);
const generatePassage = useCallback((sectionId: number) => {
generate(
sectionId,
currentModule,
"writing",
{
method: 'GET',
queryParams: {
difficulty,
...(localSettings.writingTopic && { topic: localSettings.writingTopic })
}
},
(data: any) => [{
prompt: data.question
}]
);
if (type === "academic" && academic_url !== undefined && sectionId == 1) {
generate(
sectionId,
currentModule,
"writing",
{
method: 'POST',
queryParams: {
difficulty,
type: type!
},
files: {
file: academic_url!,
}
},
(data: any) => [{
prompt: data.question
}]
)
} else {
generate(
sectionId,
currentModule,
"writing",
{
method: 'GET',
queryParams: {
difficulty,
type: type!,
...(localSettings.writingTopic && { topic: localSettings.writingTopic })
}
},
(data: any) => [{
prompt: data.question
}]
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [localSettings.writingTopic, difficulty]);
}, [localSettings.writingTopic, difficulty, academic_url]);
const onTopicChange = useCallback((writingTopic: string) => {
updateLocalAndScheduleGlobal({ writingTopic });
}, [updateLocalAndScheduleGlobal]);
const fileInputRef = useRef<HTMLInputElement>(null);
const triggerFileInput = () => {
fileInputRef.current?.click();
};
const handleFileUpload = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (file) {
const blobUrl = URL.createObjectURL(file);
if (academic_url !== undefined) {
URL.revokeObjectURL(academic_url);
}
dispatch({ type: "UPDATE_MODULE", payload: { updates: { academic_url: blobUrl } } });
}
};
return (
<>
<Dropdown
title="Generate Instructions"
{type === "academic" && focusedSection === 1 && <Dropdown
title="Upload Image"
module={"writing"}
open={localSettings.isWritingTopicOpen}
setIsOpen={(isOpen: boolean) => updateLocalAndScheduleGlobal({ isWritingTopicOpen: isOpen }, false)}
contentWrapperClassName={level ? `border border-ielts-writing`: ''}
open={localSettings.isImageUploadOpen}
setIsOpen={(isOpen: boolean) => updateLocalAndScheduleGlobal({ isImageUploadOpen: isOpen }, false)}
contentWrapperClassName={level ? `border border-ielts-writing` : ''}
>
<div className="flex flex-row gap-2 items-center px-2 pb-4">
<div className="flex flex-col flex-grow gap-4 px-2">
<label className="font-normal text-base text-mti-gray-dim">Topic (Optional)</label>
<Input
<div className="flex flex-row p-2 gap-4">
<span className="bg-gray-100 px-3.5 py-2.5 rounded-lg border border-gray-300 text-mti-gray-dim">
Upload a graph, chart or diagram
</span>
<input
type="file"
accept="image/png, image/jpeg"
onChange={handleFileUpload}
style={{ display: 'none' }}
ref={fileInputRef}
/>
<button
key={`section-${focusedSection}`}
type="text"
placeholder="Topic"
name="category"
onChange={onTopicChange}
roundness="full"
value={localSettings.writingTopic}
/>
</div>
<div className="flex self-end h-16 mb-1">
<GenerateBtn
genType="writing"
module={"writing"}
sectionId={focusedSection}
generateFnc={generatePassage}
/>
className={clsx(
"flex items-center w-[140px] px-4 py-2 text-white rounded-xl transition-colors duration-300 text-lg disabled:cursor-not-allowed",
`bg-ielts-writing/70 border border-ielts-writing hover:bg-ielts-writing disabled:bg-ielts-writing/40`,
)}
onClick={triggerFileInput}
>
<div className="flex flex-row">
<FaFileUpload className="mr-2" size={24} />
<span>Upload</span>
</div>
</button>
</div>
</div>
</Dropdown>
</Dropdown>}
{
type !== "academic" || (type === "academic" && academic_url !== undefined) && <Dropdown
title="Generate Instructions"
module={"writing"}
open={localSettings.isWritingTopicOpen}
setIsOpen={(isOpen: boolean) => updateLocalAndScheduleGlobal({ isWritingTopicOpen: isOpen }, false)}
contentWrapperClassName={level ? `border border-ielts-writing` : ''}
>
<div className="flex flex-row gap-2 items-center px-2 pb-4">
{type !== "academic" ?
<div className="flex flex-col flex-grow gap-4 px-2">
<label className="font-normal text-base text-mti-gray-dim">Topic (Optional)</label>
<Input
key={`section-${focusedSection}`}
type="text"
placeholder="Topic"
name="category"
onChange={onTopicChange}
roundness="full"
value={localSettings.writingTopic}
/>
</div>
:
<div className="flex flex-col flex-grow gap-4 px-2">
<span className="bg-gray-100 px-3.5 py-2.5 rounded-lg border border-gray-300 text-mti-gray-dim">
Generate instructions based on the uploaded image.
</span>
</div>
}
<div className="flex self-end h-16 mb-1">
<GenerateBtn
genType="writing"
module={"writing"}
sectionId={focusedSection}
generateFnc={generatePassage}
/>
</div>
</div>
</Dropdown>
}
</>
);
};