Skip to content

Commit 4b461d9

Browse files
UI - Draft API request chagnes
1 parent 6eb0fa6 commit 4b461d9

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

frontend/src/api/api.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ export const historyMessageFeedback = async (messageId: string, feedback: string
371371
return response
372372
}
373373

374-
export const sectionGenerate = async (options: SectionGenerateRequest): Promise<Response> => {
374+
export const sectionGenerate = async (options: SectionGenerateRequest[]): Promise<Response> => {
375375
// set timeout to 10 seconds
376376
const abortController = new AbortController()
377377
const abortSignal = abortController.signal
@@ -380,10 +380,13 @@ export const sectionGenerate = async (options: SectionGenerateRequest): Promise<
380380
abortController.abort()
381381
}, 10000)
382382

383-
let body = JSON.stringify({
384-
sectionTitle: options.sectionTitle,
385-
sectionDescription: options.sectionDescription
386-
})
383+
// let body = JSON.stringify({
384+
// sectionTitle: options.sectionTitle,
385+
// sectionDescription: options.sectionDescription
386+
// })
387+
388+
let body = JSON.stringify(options)
389+
387390

388391
const response = await fetch('/section/generate', {
389392
method: 'POST',

frontend/src/components/DraftCards/SectionCard.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ const SectionCard = ({ sectionIdx }: SectionCardProps) => {
142142

143143
async function fetchSectionContent(sectionTitle: string, sectionDescription: string) {
144144
setIsLoading(true)
145-
const sectionGenerateRequest: SectionGenerateRequest = { sectionTitle, sectionDescription }
145+
const sectionGenerateRequest = [{'sectionTitle' : sectionTitle, 'sectionDescription': sectionDescription }]
146146

147147
const response = await sectionGenerate(sectionGenerateRequest)
148148
const responseBody = await response.json()
@@ -164,11 +164,11 @@ const SectionCard = ({ sectionIdx }: SectionCardProps) => {
164164
setIsLoading(false)
165165
}
166166

167-
useEffect(() => {
168-
if (sectionContent === '' && !isLoading && !isManuallyCleared) {
169-
fetchSectionContent(sectionTitle, sectionDescription)
170-
}
171-
}, [sectionContent, isLoading, isManuallyCleared])
167+
// useEffect(() => {
168+
// if (sectionContent === '' && !isLoading && !isManuallyCleared) {
169+
// fetchSectionContent(sectionTitle, sectionDescription)
170+
// }
171+
// }, [sectionContent, isLoading, isManuallyCleared])
172172

173173
return (
174174
<Stack className={classes.sectionCard}>

frontend/src/pages/draft/Draft.tsx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useContext } from 'react'
1+
import { useContext, useEffect } from 'react'
22
import styles from './Draft.module.css'
33
import { useLocation, useNavigate } from 'react-router-dom'
44
import TitleCard from '../../components/DraftCards/TitleCard'
@@ -7,12 +7,26 @@ import { Document, Packer, Paragraph, TextRun } from 'docx'
77
import { saveAs } from 'file-saver'
88
import { AppStateContext } from '../../state/AppProvider'
99
import { CommandBarButton, Stack } from '@fluentui/react'
10+
import { sectionGenerate, SectionGenerateRequest } from '../../api'
11+
12+
// Define the type for the section
13+
interface Section {
14+
title: string;
15+
description: string;
16+
}
17+
18+
// Define the type for the request object
19+
interface RequestObject {
20+
sectionTitle: string;
21+
sectionDescription: string;
22+
}
1023

1124
const Draft = (): JSX.Element => {
1225
const appStateContext = useContext(AppStateContext)
1326
const location = useLocation()
1427
const navigate = useNavigate()
1528

29+
1630
// get draftedDocument from context
1731
const draftedDocument = appStateContext?.state.draftedDocument
1832
const sections = draftedDocument?.sections ?? []
@@ -23,6 +37,33 @@ const Draft = (): JSX.Element => {
2337
navigate('/')
2438
}
2539

40+
41+
// Fetch function with type annotations
42+
async function fetchAllSectionContent(req: RequestObject[]): Promise<void> {
43+
console.log("req", req);
44+
try {
45+
const response = await sectionGenerate(req);
46+
const responseBody = await response.json();
47+
console.log("responseBody", responseBody);
48+
} catch (error) {
49+
console.error("Error fetching section content:", error);
50+
}
51+
}
52+
// Function to generate the API request array
53+
const generateAPIRequest = (sections: Section[]): RequestObject[] => {
54+
return sections.map((section) => ({
55+
sectionTitle: section.title,
56+
sectionDescription: section.description,
57+
}));
58+
};
59+
60+
useEffect(()=>{
61+
if(sections.length>0){
62+
const requestArray = generateAPIRequest(sections);
63+
fetchAllSectionContent(requestArray)
64+
}
65+
},[])
66+
2667
const exportToWord = () => {
2768
const doc = new Document({
2869
sections: [

0 commit comments

Comments
 (0)