Skip to content

Commit 78e692b

Browse files
authored
Merge pull request #4 from malgamves/updates
Updates: Frontend Code, API Token
2 parents 65edd81 + d1e102c commit 78e692b

File tree

14 files changed

+291
-119
lines changed

14 files changed

+291
-119
lines changed

.gitignore

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
############################
2+
# OS X
3+
############################
4+
5+
.DS_Store
6+
.AppleDouble
7+
.LSOverride
8+
Icon
9+
.Spotlight-V100
10+
.Trashes
11+
._*
12+
13+
14+
############################
15+
# Linux
16+
############################
17+
18+
*~
19+
20+
21+
############################
22+
# Windows
23+
############################
24+
25+
Thumbs.db
26+
ehthumbs.db
27+
Desktop.ini
28+
$RECYCLE.BIN/
29+
*.cab
30+
*.msi
31+
*.msm
32+
*.msp
33+
34+
35+
############################
36+
# Packages
37+
############################
38+
39+
*.7z
40+
*.csv
41+
*.dat
42+
*.dmg
43+
*.gz
44+
*.iso
45+
*.jar
46+
*.rar
47+
*.tar
48+
*.zip
49+
*.com
50+
*.class
51+
*.dll
52+
*.exe
53+
*.o
54+
*.seed
55+
*.so
56+
*.swo
57+
*.swp
58+
*.swn
59+
*.swm
60+
*.out
61+
*.pid
62+
63+
64+
############################
65+
# Logs and databases
66+
############################
67+
68+
.tmp
69+
*.log
70+
*.sql
71+
*.sqlite
72+
*.sqlite3
73+
74+
75+
############################
76+
# Misc.
77+
############################
78+
79+
*#
80+
ssl
81+
.idea
82+
nbproject
83+
public/uploads/*
84+
!public/uploads/.gitkeep
85+
86+
############################
87+
# Node.js
88+
############################
89+
90+
lib-cov
91+
lcov.info
92+
pids
93+
logs
94+
results
95+
node_modules
96+
.node_history
97+
98+
############################
99+
# Tests
100+
############################
101+
102+
testApp
103+
coverage
104+
105+
############################
106+
# Strapi
107+
############################
108+
109+
.env
110+
license.txt
111+
exports
112+
*.cache
113+
dist
114+
build
115+
.strapi-updater.json

admin/src/components/AITextIcon/index.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

admin/src/components/Input/index.js

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import React, { useState } from 'react';
22
import { useIntl } from 'react-intl';
33
import { TextInput } from '@strapi/design-system/TextInput';
44
import { Stack } from '@strapi/design-system/Stack';
5-
import { Box } from '@strapi/design-system/Box';
65
import { Button } from '@strapi/design-system/Button';
76
import { Textarea } from '@strapi/design-system';
8-
import { TwoColsLayout } from '@strapi/design-system';
7+
import { auth } from '@strapi/helper-plugin'
98

109

1110
export 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
}

admin/src/components/PluginIcon/index.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,28 @@
55
*/
66

77
import React from 'react';
8-
import Puzzle from '@strapi/icons/Puzzle';
8+
import styled from 'styled-components';
9+
import { Icon } from '@strapi/design-system/Icon';
10+
import { Flex } from '@strapi/design-system/Flex';
11+
import Pencil from '@strapi/icons/Pencil';
912

10-
const PluginIcon = () => <Puzzle />;
1113

12-
export default PluginIcon;
14+
const IconBox = styled(Flex)`
15+
/* Hard code color values */
16+
/* to stay consistent between themes */
17+
background-color: #f0f0ff; /* primary100 */
18+
border: 1px solid #d9d8ff; /* primary200 */
19+
svg > path {
20+
fill: #4945ff; /* primary600 */
21+
}
22+
`;
23+
24+
const PluginIcon = () => {
25+
return (
26+
<IconBox justifyContent="center" alignItems="center" width={7} height={6} hasRadius aria-hidden>
27+
<Icon as={Pencil} />
28+
</IconBox>
29+
);
30+
};
31+
32+
export default PluginIcon;

admin/src/index.js

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,14 @@
11
import { prefixPluginTranslations } from '@strapi/helper-plugin';
22
import pluginPkg from '../../package.json';
33
import pluginId from './pluginId';
4-
import Initializer from './components/Initializer';
54
import PluginIcon from './components/PluginIcon';
6-
import AITextIcon from './components/PluginIcon';
75

86

97
const name = pluginPkg.strapi.name;
108

119
export default {
1210
register(app) {
13-
// app.addMenuLink({
14-
// to: `/plugins/${pluginId}`,
15-
// icon: PluginIcon,
16-
// intlLabel: {
17-
// id: `${pluginId}.plugin.name`,
18-
// defaultMessage: name,
19-
// },
20-
// Component: async () => {
21-
// const component = await import(/* webpackChunkName: "[request]" */ './pages/App');
22-
23-
// return component;
24-
// },
25-
// permissions: [
26-
// // Uncomment to set the permissions of the plugin here
27-
// // {
28-
// // action: '', // the action name should be plugin::plugin-name.actionType
29-
// // subject: null,
30-
// // },
31-
// ],
32-
// });
33-
// app.registerPlugin({
34-
// id: pluginId,
35-
// initializer: Initializer,
36-
// isReady: false,
37-
// name,
38-
// });
11+
3912
app.customFields.register({
4013
name: "text-ai",
4114
pluginId: "ai-text-generation", // the custom field is created by a color-picker plugin
@@ -48,7 +21,7 @@ export default {
4821
id: "ai-text-generation.text-ai.description",
4922
defaultMessage: "Let AI do your writing!",
5023
},
51-
icon: AITextIcon, // don't forget to create/import your icon component
24+
icon: PluginIcon, // don't forget to create/import your icon component
5225
components: {
5326
Input: async () => import(/* webpackChunkName: "input-component" */ "./components/Input"),
5427
},
@@ -61,15 +34,10 @@ export default {
6134
{
6235
sectionTitle: { // Add a "Format" settings section
6336
id: 'ai-text-generation.text-ai.api.details',
64-
defaultMessage: 'API Details',
37+
defaultMessage: 'Text Details',
6538
},
6639
items: [ // Add settings items to the section
6740
{
68-
/*
69-
Add a "Color format" dropdown
70-
to choose between 2 different format options
71-
for the color value: hexadecimal or RGBA
72-
*/
7341
intlLabel: {
7442
id: 'ai-text-generation.text-ai.key',
7543
defaultMessage: 'Key',

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"kind": "plugin",
99
"displayName": "AI Text Generation"
1010
},
11-
"dependencies": {},
11+
"dependencies": {
12+
"axios": "^1.2.2"
13+
},
1214
"author": {
1315
"name": "A Strapi developer"
1416
},

server/controllers/ai-controller.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
module.exports = ({ strapi }) => ({
4+
async generate(ctx) {
5+
ctx.body = await strapi
6+
.plugin('ai-text-generation')
7+
.service('openAi')
8+
.generateText(ctx.request.body.prompt);
9+
},
10+
});

server/controllers/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
const myController = require('./my-controller');
3+
const aiController = require('./ai-controller');
44

55
module.exports = {
6-
myController,
6+
aiController,
77
};

server/controllers/my-controller.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)