App.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import TextCard from "./TextCard";
  2. import "./App.css";
  3. import { useState, useEffect } from "react";
  4. const API_BASE_LINK = "https://copy.lucason.top";
  5. // Get the data from the server, RETURN JS OBJECT
  6. async function getListFromServer() {
  7. // make a GET request to the server
  8. // this doesnt technically need to be a request since theres no content or headers, but whatever
  9. let request = new Request(API_BASE_LINK + "/api/text", {
  10. method: "GET",
  11. });
  12. // fetch the data from the server
  13. let response = await fetch(request);
  14. console.log("GET Response: ", response);
  15. if (response.ok) {
  16. // get the JS object from the response
  17. // yes i know that says response.json, it returns a JS object for some reason
  18. const obj = await response.json();
  19. console.log("OBJECT FROM SERVER: ", obj);
  20. return obj;
  21. } else {
  22. // WEEE WOOO WEEEE WOOOO
  23. alert("ERROR: SERVER RETURNED " + response.status);
  24. }
  25. }
  26. async function deleteItem(id) {
  27. console.log("ID to delete: ", id);
  28. console.log("ID in JSON: " + JSON.stringify(id));
  29. // POST request that includes json of the ID (should just be a number)
  30. let request = new Request(API_BASE_LINK + "/api/remove", {
  31. method: "POST",
  32. headers: {
  33. "Content-Type": "application/json",
  34. },
  35. body: JSON.stringify({ id }),
  36. });
  37. console.log("go");
  38. console.log(
  39. "DELETE Response: ",
  40. await fetch(request).then((result) => {
  41. return result.text();
  42. }),
  43. );
  44. console.log("go2");
  45. // reload so the up-to-date info is loaded
  46. location.reload();
  47. }
  48. async function onSubmit() {
  49. const encoder = new TextEncoder();
  50. // get the value of the nametext element, delete the input after
  51. let textelement = document.getElementById("nametext");
  52. textelement.setAttribute("value", "");
  53. // encode it into an array of utf-8 numbers or whatever
  54. const text = encoder.encode(textelement.value);
  55. console.log("Normal Text: ", text);
  56. console.log("JSON Text: ", JSON.stringify(text));
  57. // make a POST request that sends JSON to the server
  58. let request = new Request(API_BASE_LINK + "/api/add", {
  59. method: "POST",
  60. headers: {
  61. "Content-Type": "application/json",
  62. },
  63. body: JSON.stringify(text),
  64. });
  65. console.log(
  66. "Result: ",
  67. await fetch(request).then((result) => {
  68. return result.text();
  69. }),
  70. );
  71. // reload so the up-to-date info is loaded
  72. location.reload();
  73. }
  74. function App() {
  75. const [textList, setTextList] = useState([]);
  76. useEffect(() => {
  77. // get the JSON data from the server, store it in result
  78. getListFromServer().then((result) => {
  79. const decoder = new TextDecoder();
  80. // init the array to return, push each item from the data object
  81. let returnArray = [];
  82. for (let i in result) {
  83. console.log(result);
  84. console.log(result[i]);
  85. // convert the object array to a JS array
  86. var utf8Array = [];
  87. for (let j in result[i]) {
  88. utf8Array.push(result[i][j]);
  89. }
  90. // make it a Uint8Array so it can be decoded
  91. utf8Array = new Uint8Array(utf8Array);
  92. console.log("UTF8 ARRAY: " + utf8Array);
  93. // decode and add to return array
  94. let iString = decoder.decode(utf8Array);
  95. returnArray.push(iString);
  96. }
  97. // set textlist variable
  98. setTextList(returnArray);
  99. });
  100. }, [setTextList]);
  101. return (
  102. <div className="h-screen flex flex-col items-center justify-center">
  103. {/*center vertically ^^^ */}
  104. {/*center horizontally vvv */}
  105. <div className="w-screen flex flex-col justify-center items-center">
  106. <div className="max-w-sm max-h-svh">
  107. <div className="flex flex-col items-center border border-black p-2 m-1">
  108. <p className="m-3 mb-5 justify-self-center text-black">
  109. Copy Paste App
  110. </p>
  111. <div className="flex flex-row">
  112. <input
  113. className="border border-black p-1 m-1 bg-white text-black"
  114. id="nametext"
  115. type="text"
  116. placeholder="Put text here"
  117. />
  118. <button
  119. className="submitbutton border border-black p-1 m-1 bg-white text-black"
  120. type="submit"
  121. onClick={onSubmit}
  122. >
  123. Submit
  124. </button>
  125. </div>
  126. </div>
  127. <div className="flex flex-col items-stretch border border-black p-2 m-1">
  128. {textList.map((value, index) => {
  129. return (
  130. <TextCard
  131. key={index}
  132. text={value}
  133. id={index}
  134. deleteFunction={deleteItem}
  135. />
  136. );
  137. })}
  138. </div>
  139. </div>
  140. </div>
  141. </div>
  142. );
  143. }
  144. export default App;