Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Task form #1

Merged
merged 1 commit into from
Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 95 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container">
<form action="" id="taskForm">
<div class="inputBox">
<label for="title">Title</label>
<input type="text" name="title" placeholder="Title" id="title">
</div>
<div class="inputBox">
<label for="purpose">Purpose</label>
<input type="text" id="purpose" name="purpose" placeholder="Purpose">
</div>
<div class="inputBox">
<label for="featureUrl">Feature URL</label>
<input type="text" id="featureUrl" name="featureUrl" placeholder="Feature URL">
</div>
<div class="inputBox">
<label for="dev">Dev</label>
<input type="radio" id="dev" name="type" value="dev">
<label for="group">Group</label>
<input type="radio" id="group" name="type" value="group">
</div>
<div class="inputBox">
<label for="links">Links</label>
<input type="text" name="links" id="links">
</div>
<div class="inputBox">
<label for="startedOn">Started on</label>
<input type="date" name="startedOn" id="startedOn">
</div>
<div class="inputBox">
<label for="endsOn">Ends on</label>
<input type="date" name="endsOn" id="endsOn">
</div>
<div class="inputBox">
<label for="status">Status</label>
<input type="text" placeholder="Status" id="status" name="status">
</div>
<div class="inputBox">
<label for="ownerId">Owner Id</label>
<input type="text" placeholder="Owner Id" id="ownerId" name="ownerId">
</div>
<div class="inputBox">
<label for="percentCompleted">Percent completed</label>
<input type="number" id="percentCompleted" name="percentCompleted" placeholder="Percent completed">
</div>
<div class="inputBox">
<label for="completionAward">Completion award</label>
<div class="inputBox">
<label for="completionAwardGold">Gold</label>
<input type="number" id="completionAwardGold" name="completionAwardGold" placeholder="Number of gold coins">
</div>
<div class="inputBox">
<label for="completionAwardSilver">Silver</label>
<input type="number" id="completionAwardSilver" name="completionAwardSilver" placeholder="Number of silver coins">
</div>
<div class="inputBox">
<label for="completionAwardBronze">Bronze</label>
<input type="number" id="completionAwardBronze" name="completionAwardBronze" placeholder="Number of bronze coins">
</div>
</div>
<div class="inputBox">
<label for="lossRate">Loss rate</label>
<div class="inputBox">
<label for="lossRateGold">Gold</label>
<input type="number" id="lossRateGold" name="lossRateGold" placeholder="Number of gold coins">
</div>
<div class="inputBox">
<label for="lossRateSilver">Silver</label>
<input type="number" id="lossRateSilver" name="lossRateSilver" placeholder="Number of silver coins">
</div>
<div class="inputBox">
<label for="lossRateBronze">Bronze</label>
<input type="number" id="lossRateBronze" name="lossRateBronze" placeholder="Number of bronze coins">
</div>
</div>
<div class="inputBox">
<label for="isNoteworthy">Is noteworthy</label>
<input type="checkbox" value="true" id="isNoteworthy" name="isNoteworthy">
</div>
<div class="inputBox">
<input type="submit" value="Submit" >
</div>
</form>

<script src="./script.js"></script>
</div>
</body>
</html>
81 changes: 81 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const BASE_URL = 'https://staging-api.realdevsquad.com';

function getObjectOfFormData(formId) {
const object = {};
const data = new FormData(formId);

data.forEach((value, key) => {
if (!Reflect.has(object, key)) {
object[key] = value;
return;
}
if (!Array.isArray(object[key])) {
object[key] = [object[key]];
}
object[key].push(value);
});

return object;
}

taskForm.onsubmit = async (e) => {
e.preventDefault();
const {
title,
purpose,
featureUrl,
type,
links,
endsOn,
startedOn,
status,
ownerId,
percentCompleted,
completionAwardGold,
completionAwardSilver,
completionAwardBronze,
lossRateGold,
lossRateSilver,
lossRateBronze,
isNoteworthy,
} = getObjectOfFormData(taskForm)

const dataToBeSent = {
title,
purpose,
featureUrl,
type,
links: (Array.isArray(links))? links : [links],
endsOn,
startedOn,
status,
ownerId,
percentCompleted,
completionAward: {
gold: completionAwardGold,
silver: completionAwardSilver,
bronze: completionAwardBronze
},
lossRate: {
gold: lossRateGold,
silver: lossRateSilver,
bronze: lossRateBronze,
},
isNoteworthy: isNoteworthy || false,
}
try {
const response = await fetch(`${BASE_URL}/tasks`, {
method: 'POST',
body: JSON.stringify(dataToBeSent),
headers: {
'Content-type': 'application/json'
}
});

const result = await response.json();

alert(result.message);
} catch (error) {
alert(`Error: ${error}`)
}
};