Skip to content

Commit 4c73b83

Browse files
Merge pull request #43 from EOS-uiux-Solutions/image-upload
Add image upload functionality
2 parents b5dd659 + 27afb87 commit 4c73b83

File tree

4 files changed

+25
-30
lines changed

4 files changed

+25
-30
lines changed

src/components/Dragdrop.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React, { useEffect, useState } from 'react'
22
import { useDropzone } from 'react-dropzone'
33

4-
const Dragdrop = () => {
4+
const Dragdrop = ({ setAttachments }) => {
55
const [files, setFiles] = useState([])
66

77
const { getRootProps, getInputProps } = useDropzone({
88
multiple: true,
99
onDrop: (acceptedFiles) => {
10+
setAttachments(acceptedFiles)
1011
setFiles((files) => [
1112
...files,
1213
...acceptedFiles.map((file) =>

src/pages/NewStory.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { useLayoutEffect, useState, useEffect, useContext } from 'react'
22
import { useForm } from 'react-hook-form'
3-
43
import MarkdownEditor from '../components/MarkdownEditor'
54
import { filterDescriptionText } from '../utils/filterText'
65
import { trackPromise, usePromiseTracker } from 'react-promise-tracker'
@@ -49,6 +48,8 @@ const NewStory = () => {
4948

5049
const [screenSize, setScreenSize] = useState(0)
5150

51+
const [attachments, setAttachments] = useState()
52+
5253
useLayoutEffect(() => {
5354
function updateScreenSize() {
5455
setScreenSize(window.innerWidth)
@@ -123,8 +124,13 @@ const NewStory = () => {
123124
setDescriptionError(true)
124125
return
125126
}
126-
data.description = filterDescriptionText(description)
127-
await userStory.createStory(data)
127+
data.Description = filterDescriptionText(description)
128+
const formData = new FormData()
129+
formData.append('data', JSON.stringify(data))
130+
attachments.forEach((file) => {
131+
formData.append('files.Attachment', file)
132+
})
133+
await userStory.createStory(formData)
128134
navigate('/')
129135
}
130136

@@ -148,7 +154,7 @@ const NewStory = () => {
148154
<input
149155
className='input-default'
150156
type='text'
151-
name='title'
157+
name='Title'
152158
autoComplete='off'
153159
ref={register({ required: true })}
154160
/>
@@ -188,7 +194,7 @@ const NewStory = () => {
188194
<label htmlFor='category'>Category</label>
189195
<select
190196
className='select-default'
191-
name='category'
197+
name='Category'
192198
ref={register({ required: true })}
193199
>
194200
<option defaultValue={true} value=''>
@@ -209,7 +215,7 @@ const NewStory = () => {
209215
<label htmlFor='priority'>Priority</label>
210216
<select
211217
className='select-default'
212-
name='priority'
218+
name='Priority'
213219
ref={register({ required: true })}
214220
>
215221
<option defaultValue={true} value=''>
@@ -243,7 +249,7 @@ const NewStory = () => {
243249
/>
244250
{descriptionError && <FormError type='emptyDescription' />}
245251
</div>
246-
<Dragdrop />
252+
<Dragdrop setAttachments={setAttachments} />
247253
<div className='flex flex-row flex-center'>
248254
<Button type='submit' className='btn btn-default'>
249255
Submit

src/pages/Story.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ const Story = (props) => {
233233
/>
234234
)}
235235
</div>
236+
<div>
237+
{story.Attachment?.map((obj) => (
238+
<img src={obj.url} alt='attachment' />
239+
))}
240+
</div>
236241
<Comments storyId={storyId} />
237242
</div>
238243
</div>

src/services/user_story.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,8 @@ import {
55
} from './gql_fragments'
66

77
const userStory = {
8-
createStory: ({ description, title, category, product, priority }) => {
9-
const createQuery = {
10-
query: `mutation {
11-
createUserStory(
12-
input: {
13-
data: {
14-
Description: "${description}"
15-
Title: "${title}"
16-
Category: ${category}
17-
product: "${product}"
18-
Priority: ${priority}
19-
}
20-
}
21-
) {
22-
userStory {
23-
createdAt
24-
}
25-
}
26-
}
27-
`
28-
}
29-
return apiCall('/graphql', createQuery)
8+
createStory: (data) => {
9+
return apiCall('/user-stories', data)
3010
},
3111
checkAuthor: (userId, storyId) => {
3212
return apiCall('/checkAuthor', {
@@ -112,6 +92,9 @@ const userStory = {
11292
id
11393
username
11494
}
95+
Attachment {
96+
url
97+
}
11598
}
11699
}
117100
${BASIC_STORY_INFO_FRAGMENT}

0 commit comments

Comments
 (0)