-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectCreationPage.tsx
225 lines (207 loc) · 5.91 KB
/
ProjectCreationPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import {
Box,
Button,
Card,
CardBody,
FormControl,
FormLabel,
Heading,
Input,
Textarea,
VStack,
useToast,
Flex,
HStack,
Tag,
TagLabel,
TagCloseButton,
} from '@chakra-ui/react'
import { BACKEND_URL } from '../../constants'
import Header from '../../components/Header'
import axios, { AxiosError } from 'axios'
import { useNavigate } from 'react-router-dom'
import { useState } from 'react'
export interface Project {
role: string | undefined
name: string
description: string
id: number
vendorUID: number
pricePerImage: number
tags: Array<string>
isArchived: boolean
}
export default function ProjectCreationPage() {
const [projectName, setProjectName] = useState('')
const [description, setDescription] = useState('')
const [pricePerImage, setPricePerImage] = useState(0)
const [tag, setTag] = useState('')
const [tags, setTags] = useState<string[]>([])
const navigate = useNavigate()
const toast = useToast()
const handleAddTag = () => {
if (tag.trim() !== '' && !tags.includes(tag.toUpperCase())) {
setTags([...tags, tag.toUpperCase()])
setTag('')
}
}
const handleRemoveTag = (indexToRemove: number) => {
setTags(tags.filter((_, index) => index !== indexToRemove))
}
const submitProject = async () => {
if (projectName === '' || description === '') {
toast({
title: 'Error',
description: 'Please fill out all required fields.',
status: 'error',
})
return
}
if (pricePerImage <= 0) {
toast({
title: 'Error',
description: 'Price per image must be greater than 0.',
status: 'error',
})
return
}
try {
// make post request to create project
const formData = new FormData()
formData.append('projectName', projectName)
formData.append('description', description)
formData.append('pricePerImage', pricePerImage.toString())
formData.append('tags', JSON.stringify(tags))
const token = sessionStorage.getItem('jwt')
const response = await axios.post(`${BACKEND_URL}/projects`, formData, {
headers: {
'Content-Type': 'multipart/form-data',
Authorization: `Bearer ${token}`,
},
})
if (response.status === 201 || response.status === 200) {
toast({
title: 'Project created successfully!',
status: 'success',
})
navigate('/dashboard')
} else {
throw new Error('Failed to create project.')
}
} catch (error) {
const description =
error instanceof AxiosError && error.response?.data?.error
? error.response.data.error
: 'Failed to create project.'
toast({
title: 'Error',
description,
status: 'error',
})
}
}
return (
<Box
width="100vw"
height="100vh"
display="flex"
flexDirection="column"
justifyContent="flex-start"
alignItems="flex-start"
overflowY="auto"
>
<Header />
<Box>
<Heading alignSelf="flex-start" paddingLeft="40px" paddingTop="20px">
Create a New Project
</Heading>
</Box>
<VStack
spacing={5}
width="100%"
maxWidth="800px"
alignItems="flex-start"
padding="40px"
>
<Card width="100%">
<CardBody>
<FormControl isRequired>
<FormLabel>Project Name</FormLabel>
<Input
type="text"
placeholder="Project Name"
value={projectName}
onChange={e => setProjectName(e.target.value)}
/>
</FormControl>
</CardBody>
<CardBody>
<FormControl isRequired>
<FormLabel>Description</FormLabel>
<Textarea
placeholder="Describe your project"
value={description}
onChange={e => setDescription(e.target.value)}
/>
</FormControl>
</CardBody>
</Card>
<Card width="100%">
<CardBody>
<FormControl isRequired>
<FormLabel>Price per Image (USD)</FormLabel>
<Input
type="number"
value={pricePerImage}
onChange={e => {
const value = e.target.value as unknown as number
if (value >= 0) {
setPricePerImage(value)
} else {
toast({
title: 'Error',
description: 'Price per image must be greater than 0.',
status: 'error',
})
throw new Error('Price per image must be greater than 0.')
}
}}
/>
</FormControl>
</CardBody>
</Card>
<Card width="100%">
<CardBody>
<FormControl>
<FormLabel>Tags</FormLabel>
<Flex>
<Input
type="text"
value={tag}
onChange={e => setTag(e.target.value)}
placeholder="Provide a descriptive tag"
/>
<Button colorScheme="blue" ml={2} onClick={handleAddTag}>
Add
</Button>
</Flex>
</FormControl>
<HStack mt={4} spacing={2}>
{tags.map((tag, index) => (
<Tag key={index} variant="solid" size="sm">
<TagLabel>{tag}</TagLabel>
<TagCloseButton onClick={() => handleRemoveTag(index)} />
</Tag>
))}
</HStack>
</CardBody>
</Card>
<Box>
<Button colorScheme="blue" width="150px" onClick={submitProject}>
Create Project
</Button>
</Box>
</VStack>
</Box>
)
}