@@ -2,10 +2,9 @@ import React, { useState } from 'react';
22import { useIntl } from 'react-intl' ;
33import { TextInput } from '@strapi/design-system/TextInput' ;
44import { Stack } from '@strapi/design-system/Stack' ;
5- import { Box } from '@strapi/design-system/Box' ;
65import { Button } from '@strapi/design-system/Button' ;
76import { Textarea } from '@strapi/design-system' ;
8- import { TwoColsLayout } from '@strapi/design-system' ;
7+ import { auth } from '@strapi/helper-plugin'
98
109
1110export default function Index ( {
@@ -18,17 +17,16 @@ export default function Index({
1817 attribute,
1918} ) {
2019 const { formatMessage } = useIntl ( ) ;
21- const [ content , setContent ] = useState ( '' ) ;
2220 const [ prompt , setPrompt ] = useState ( '' ) ;
2321 const [ err , setErr ] = useState ( '' ) ;
2422
25- const aiClick = async ( ) => {
23+ const generateText = async ( ) => {
2624 try {
27- const response = await fetch ( 'https://api.openai.com/v1/completions' , {
25+ const response = await fetch ( `/ai-text-generation/generate-text` , {
2826 method : 'POST' ,
2927 headers : {
3028 'Content-Type' : 'application/json' ,
31- 'Authorization' : `Bearer ${ accessToken } ` //functionality to be added
29+ 'Authorization' : `Bearer ${ auth . getToken ( ) } `
3230 } ,
3331 body : JSON . stringify ( {
3432 'model' : 'text-davinci-001' ,
@@ -46,7 +44,9 @@ export default function Index({
4644 }
4745
4846 const result = await response . json ( ) ;
49- onChange ( { target : { name, value : result . choices [ 0 ] . text , type : attribute . type } } )
47+ const parsedResult = result . choices [ 0 ] . text . replace ( / (?: \r \n | \r | \n ) / g, '' ) ;
48+
49+ onChange ( { target : { name, value : parsedResult , type : attribute . type } } )
5050 } catch ( err ) {
5151 setErr ( err . message ) ;
5252 }
@@ -80,7 +80,7 @@ export default function Index({
8080 { value }
8181 </ Textarea >
8282 < Stack horizontal spacing = { 4 } >
83- < Button onClick = { aiClick } > Generate</ Button >
83+ < Button onClick = { ( ) => generateText ( ) } > Generate</ Button >
8484 < Button onClick = { ( ) => clearGeneratedText ( ) } > Clear</ Button >
8585 </ Stack >
8686 </ Stack >
0 commit comments