@@ -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 }
@@ -58,31 +58,32 @@ export default function Index({
5858 }
5959
6060 return (
61- < Box >
62- < Stack spacing = { 1 } >
63- < Stack spacing = { 1 } >
64- < TextInput
65- placeholder = "Please write a prompt for content to generate"
66- label = "Prompt" name = "Prompt"
67- onChange = { e => setPrompt ( e . target . value ) } value = { prompt } />
61+ < Stack spacing = { 1 } >
62+ < TextInput
63+ placeholder = "Please write a prompt for content to generate"
64+ label = "Prompt"
65+ name = "Prompt"
66+ onChange = { ( e ) => setPrompt ( e . target . value ) }
67+ value = { prompt }
68+ />
69+ < Stack padding = { 4 } spacing = { 2 } >
70+ < Textarea
71+ placeholder = "Generated text"
72+ label = "Content"
73+ name = "content"
74+ onChange = { ( e ) =>
75+ onChange ( {
76+ target : { name, value : e . target . value , type : attribute . type } ,
77+ } )
78+ }
79+ >
80+ { value }
81+ </ Textarea >
82+ < Stack horizontal spacing = { 4 } >
83+ < Button onClick = { ( ) => generateText ( ) } > Generate</ Button >
84+ < Button onClick = { ( ) => clearGeneratedText ( ) } > Clear</ Button >
6885 </ Stack >
69- < Box as = "p" padding = { 4 } >
70- < Textarea
71- placeholder = "Generated text"
72- label = "Content"
73- name = "content"
74- onChange = { e => onChange ( { target : { name, value : e . target . value , type : attribute . type } } ) } >
75- { value }
76- </ Textarea >
77- </ Box >
7886 </ Stack >
79- < Box padding = { 4 } >
80- < TwoColsLayout
81- startCol = { < Button onClick = { aiClick } > Generate</ Button > }
82- endCol = { < Button onClick = { ( ) => clearGeneratedText ( ) } > Clear</ Button >
83- }
84- />
85- </ Box >
86- </ Box >
87+ </ Stack >
8788 )
8889}
0 commit comments