-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewuser.tsx
210 lines (186 loc) · 5.41 KB
/
newuser.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
import Head from 'next/head';
import Header from '../components/Header';
import Footer from '../components/Footer';
import React from 'react';
import styled from 'styled-components';
import Button from '../components/Button';
import { useRouter } from 'next/router';
import upload from '../lib/uploadUserData';
import { ArtistInterface } from '../lib/ArtistClass';
import fetchData from '../lib/fetchData';
import { Dispatch, SetStateAction, useState } from 'react';
import { nanoid } from 'nanoid';
import Picture from '../components/Picture';
import DatePick from '../components/DatePick';
type NewUserProps = {
onSetArtists: Dispatch<SetStateAction<ArtistInterface[] | undefined>>;
};
export default function NewUserPage({
onSetArtists,
}: NewUserProps): JSX.Element {
const [isloading, setLoading] = useState<boolean>(false);
//state that keeps track of the chosen dates in the datepicker. Goes as prop to DatePick
const [dates, setDates] = useState<string[]>([]);
//state array that keeps track of the selected image files
const [selectedImages, setSelectedImages] = useState<Blob[]>([]);
// function for checking the selected img-files and updating the state-array
function changeImage(event: React.SyntheticEvent): void {
const imgList = (event.target as HTMLInputElement).files;
if (imgList && imgList.length > 0) {
setSelectedImages([...selectedImages, imgList[0]]);
}
}
function handleDelete(name: string): void {
setSelectedImages(selectedImages.filter((image) => image.name !== name));
}
const router = useRouter();
async function handleSubmit(event: React.SyntheticEvent): Promise<void> {
event.preventDefault();
setLoading(true);
const url = await upload(event, selectedImages, dates);
fetchData('/api', onSetArtists);
setLoading(false);
router.push(`/${url}`);
}
return (
<>
<Head>
<title>Wannado</title>
<link rel='shortcut icon' href='/favicon.ico' />
</Head>
<Header heading={'Add New Artist'} />
<StyledForm onSubmit={handleSubmit}>
<StyledLabel>
<span>Artistname:</span>
<input name='artistname' required />
</StyledLabel>
<StyledLabel>
<span>City:</span>
<input name='city' required />
</StyledLabel>
<StyledLabel>
<span>Street:</span>
<input name='streetname' required />
<input name='number' placeholder='#' type='number' required />
</StyledLabel>
{selectedImages.length === 4 ? (
<StyledButtonBar>
<StyledDatePick
dates={dates}
onSetDates={setDates}
inline={false}
/>
<StyledSubmit
name={`${isloading ? 'Uploading' : 'Sumbit'}`}
inactive={isloading}
disabled={isloading}
></StyledSubmit>
</StyledButtonBar>
) : (
<StyledButtonBar>
<StyledDatePick
dates={dates}
onSetDates={setDates}
inline={false}
/>
<StyledImgInput>
Add Images
<input
onChange={changeImage}
name='pics'
type='file'
accept='image/png, image/jpeg'
required
/>
</StyledImgInput>
</StyledButtonBar>
)}
{selectedImages.length <= 0 ? (
<StyledPlaceholder>
To submit your data, add 4 images from your collection
</StyledPlaceholder>
) : (
<StyledGalery>
{selectedImages.map((image) => (
<div style={{ position: 'relative' }} key={nanoid()}>
<Picture source={URL.createObjectURL(image)} />
<StyledDelete onClick={() => handleDelete(image.name)}>
X
</StyledDelete>
</div>
))}
</StyledGalery>
)}
</StyledForm>
<Footer />
</>
);
}
const StyledForm = styled.form`
display: flex;
flex-direction: column;
padding: 35px 40px 0px 40px;
gap: 20px;
color: rgba(217, 217, 217, 1);
height: 80vh;
`;
const StyledPlaceholder = styled.h3`
margin-top: auto;
margin-bottom: auto;
`;
const StyledGalery = styled.div`
grid-column-end: span 2;
display: grid;
grid-template-columns: 1fr 1fr;
align-items: flex-start;
align-content: space-between;
justify-items: center;
gap: 20px;
margin-top: 20px;
`;
const StyledLabel = styled.label`
display: grid;
grid-template-columns: 100px 195px;
& ~ & ~ & {
grid-template-columns: 100px 160px 35px;
}
`;
const StyledButtonBar = styled.div`
display: grid;
grid-template-columns: 1fr 1fr;
justify-items: center;
`;
const StyledImgInput = styled.label`
border: 1px solid #ccc;
padding: 10px;
color: rgba(217, 217, 217, 1);
input[type='file'] {
display: none;
}
`;
const StyledDatePick = styled(DatePick)`
background-color: transparent;
border: #ccc 1px solid;
padding: 10px;
width: 100px;
&::placeholder {
color: rgba(217, 217, 217, 1);
font-family: Roboto;
}
`;
const StyledSubmit = styled(Button)`
width: 100px;
height: 37px;
`;
const StyledDelete = styled.button`
position: absolute;
top: -7px;
right: -7px;
border: none;
background-color: #d93378;
opacity: 80%;
color: rgba(217, 217, 217, 1);
height: 20px;
width: 20px;
border-radius: 5px;
`;