-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.html
More file actions
191 lines (179 loc) · 5.7 KB
/
create.html
File metadata and controls
191 lines (179 loc) · 5.7 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Worker Profile</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
padding: 0;
background-color: #f9f9f9;
}
.form-container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background: #fff;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input, textarea, select {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
padding: 10px 20px;
background: #007BFF;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background: #0056b3;
}
.bestworks-container {
margin-top: 20px;
}
.bestwork-item {
margin-bottom: 15px;
padding: 10px;
background: #f1f1f1;
border-radius: 4px;
}
.bestwork-item button {
background: #FF4D4D;
}
.bestwork-item button:hover {
background: #cc0000;
}
</style>
</head>
<body>
<div class="form-container">
<h2>Add Worker Profile</h2>
<form id="worker-form">
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<label for="profilePicture">Profile Picture URL</label>
<input type="text" id="profilePicture" name="profilePicture" required>
</div>
<div class="form-group">
<label for="category">Category</label>
<input type="text" id="category" name="category" required>
</div>
<div class="form-group">
<label for="subcategories">Subcategories</label>
<input type="text" id="subcategories" name="subcategories" placeholder="e.g., Residential, Commercial">
</div>
<div class="form-group">
<label for="city">City</label>
<input type="text" id="city" name="city" required>
</div>
<div class="form-group">
<label for="state">State</label>
<input type="text" id="state" name="state" required>
</div>
<div class="form-group">
<label for="about">About</label>
<textarea id="about" name="about" rows="3" required></textarea>
</div>
<div class="form-group">
<label for="badges">badges</label>
<textarea id="badges" name="badges" rows="3" required></textarea>
</div>
<!-- Best Works Section -->
<div class="form-group bestworks-container">
<label>Best Works</label>
<div id="bestworks-list"></div>
<button type="button" id="add-bestwork">Add Best Work</button>
</div>
<button type="submit">Add Worker</button>
</form>
</div>
<script>
const bestworksList = document.getElementById('bestworks-list');
const addBestWorkButton = document.getElementById('add-bestwork');
// Function to add a new Best Work input set
function addBestWork() {
const bestWorkDiv = document.createElement('div');
bestWorkDiv.className = 'bestwork-item';
bestWorkDiv.innerHTML = `
<div class="form-group">
<label>Title</label>
<input type="text" class="bestwork-title" required>
</div>
<div class="form-group">
<label>Image URL</label>
<input type="text" class="bestwork-image" required>
</div>
<button type="button" onclick="removeBestWork(this)">Remove</button>
`;
bestworksList.appendChild(bestWorkDiv);
}
// Function to remove a Best Work input set
function removeBestWork(button) {
button.parentElement.remove();
}
// Add Best Work event listener
addBestWorkButton.addEventListener('click', addBestWork);
// Handle form submission
document.getElementById('worker-form').addEventListener('submit', async (e) => {
e.preventDefault();
const bestWorks = Array.from(bestworksList.children).map((work) => ({
title: work.querySelector('.bestwork-title').value,
imageUrl: work.querySelector('.bestwork-image').value,
}));
const formData = {
name: document.getElementById('name').value,
profilePicture: document.getElementById('profilePicture').value,
category: document.getElementById('category').value,
subcategories: document.getElementById('subcategories').value.split(',').map((s) => s.trim()),
location: {
city: document.getElementById('city').value,
state: document.getElementById('state').value,
},
about: document.getElementById('about').value,
bestWorks,
badges: document.getElementById('badges').value.split(',').map((s) => s.trim()),
};
try {
const response = await fetch('http://localhost:3000/api/workers', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData),
});
if (response.ok) {
const result = await response.json();
alert('Worker added successfully');
document.getElementById('worker-form').reset();
bestworksList.innerHTML = ''; // Clear Best Works list
} else {
const error = await response.json();
alert(`Error: ${error.error}`);
}
} catch (error) {
console.error('Error adding worker:', error);
alert('Failed to add worker. Please try again.');
}
});
</script>
</body>
</html>