Skip to content

Commit 7aaaf17

Browse files
committed
feat: 400 status handling
1 parent 3cc5964 commit 7aaaf17

File tree

5 files changed

+67
-18
lines changed

5 files changed

+67
-18
lines changed

api/shorten.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ function POST_shortcode($db)
1515

1616
// check for existing record
1717
if (recordExists($db, "url", $url)) {
18-
sendErrorCode(400);
19-
return [null, null];
18+
return ["shortcode" => null];
2019
} else {
2120
// create record in database
2221
$db->query(
@@ -87,6 +86,10 @@ function GET_longurl($db)
8786
http_response_code(201);
8887
header("Content-Type: application/json; charset=utf-8");
8988
echo json_encode($data);
89+
} else {
90+
http_response_code(400);
91+
header("Content-Type: application/json; charset=utf-8");
92+
echo json_encode("A shortcode for this URL already exists.");
9093
}
9194
break;
9295
}

public/css/styles.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ span.response-item {
122122
color: var(--green);
123123
}
124124

125+
code.response-status {
126+
opacity: 0;
127+
transition: opacity 0.2s;
128+
129+
&.visible {
130+
opacity: 1;
131+
}
132+
}
133+
125134
span.status-code {
126135
padding: 0.2em 0.4em;
127136
color: #000000;

public/js/actions/post.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@
22

33
// ** POST ACTION
44
/** @param {string} endpoint @param {string} url
5-
* @returns {Promise<[PostResponse, number]>}
5+
* @returns {Promise<[PostResponse | null, number, string]>}
66
*/
77
export async function postURL(endpoint, url) {
88
const response = await fetch(endpoint, {
99
method: "POST",
1010
headers: { "Content-Type": "application/x-www-form-urlencoded" },
1111
body: `url=${encodeURIComponent(url)}`,
1212
});
13+
14+
const statusMessages = new Map([
15+
[200, "Retrieved shortcode."],
16+
[201, "Created shortcode."],
17+
[204, "Deleted shortcode."],
18+
[400, "A shortcode for this URL already exists."],
19+
[404, "Shortcode not found."],
20+
[500, "error"],
21+
]);
22+
1323
const status = response.status;
1424
const data = await response.json();
15-
return [data, status];
25+
const message = /** @type {string} */ (statusMessages.get(status));
26+
return [data, status, message];
1627
}
1728

1829
// ** ELEMENT VARIABLES
@@ -31,6 +42,14 @@ export const shortcodeSubmitButton = /** @type {HTMLButtonElement} */ (
3142
document.querySelector('form[method="post"] input[type="submit"]')
3243
);
3344

34-
export const statusCodeSpan = /** @type {HTMLSpanElement } */ (
45+
export const statusResponse = /** @type {Element} */ (
46+
document.querySelector("code.response-status")
47+
);
48+
49+
export const statusLabel = /** @type {HTMLSpanElement } */ (
3550
document.querySelector("span.status-code")
3651
);
52+
53+
export const statusMessage = /** @type {HTMLParagraphElement} */ (
54+
document.querySelector("p.status-message")
55+
);

public/js/app.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const invalidUrlMessage = document.querySelector("p.invalid-url-message");
3838

3939
// ** CSS CLASS MAP FOR STATUS CODES
4040
/** @type {Map<number, string>} */
41-
const statusCssClass = new Map([
41+
const statusMap = new Map([
4242
[200, "success"],
4343
[201, "success"],
4444
[204, "success"],
@@ -53,20 +53,35 @@ POST.shortcodeForm?.addEventListener("submit", async (event) => {
5353
const input = /** @type {HTMLInputElement} */ (
5454
POST.shortcodeForm?.elements[0]
5555
);
56-
const [data, status] = await POST.postURL("/shorten", input.value);
56+
let [data, status, message] = await POST.postURL("/shorten", input.value);
5757
console.log(data);
58-
for (let i = 0; i < Object.keys(data).length; i++) {
59-
POST.responseItemSpans[i].textContent = /** @type {string} */ (
60-
data[`${POST.responseItemSpans[i].id}`]
58+
POST.statusLabel.classList.remove("success", "error");
59+
POST.responseContainer.classList.remove("visible");
60+
61+
if (statusMap.get(status) === "error") {
62+
POST.statusLabel.classList.add(
63+
/** @type {string} */ (statusMap.get(status)),
64+
);
65+
POST.statusLabel.textContent = `${status}`;
66+
POST.statusMessage.textContent = /** @type {string} */ (message);
67+
POST.statusMessage.classList.remove("hide");
68+
POST.statusResponse.classList.add("visible");
69+
} else {
70+
data = /** @type {PostResponse} */ (data);
71+
for (let i = 0; i < Object.keys(data).length; i++) {
72+
POST.responseItemSpans[i].textContent = /** @type {string} */ (
73+
data[`${POST.responseItemSpans[i].id}`]
74+
);
75+
}
76+
POST.statusLabel.classList.add(
77+
/** @type {string} */ (statusMap.get(status)),
6178
);
79+
POST.statusLabel.textContent = `${status}`;
80+
POST.statusMessage.textContent = /** @type {string} */ (message);
81+
POST.statusResponse.classList.add("visible");
82+
POST.responseContainer.classList.add("visible");
6283
}
63-
// add status code color and add response code as text
64-
POST.statusCodeSpan?.classList.add(
65-
/** @type {string} */ (statusCssClass.get(status)),
66-
);
67-
POST.statusCodeSpan.textContent = `${status}`;
68-
// make response visible
69-
POST.responseContainer.classList.add("visible");
84+
7085
input.value = "";
7186
input.focus();
7287
});

views/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ <h2>Create a Shortcode</h2>
4848
</form>
4949
<p class="invalid-url-message">Invalid URL.</p>
5050

51+
<code class="response-status">
52+
<p>Status: <span class="status-code"></span></p>
53+
<p class="status-message hide"></p>
54+
</code>
5155
<div class="response-contents">
5256
<code>
53-
<p>Status: <span class="status-code"></span></p>
5457
{ <br />
5558
&nbsp;&nbsp;id: <span class="response-item" id="id"></span>
5659
<br />

0 commit comments

Comments
 (0)