Loading...
: (
- onClick(props.id, props.setActive)}>
- {props.name}
+
+ {props.id}
)
}
export default function Tabs(props) {
- const [active, setActive] = useState("form");
- const [className, setClassName] = useState({ form: "tab active", static: "tab", classForm: "tab" });
- const setTabContentClassName = props.setTabContentClassName;
-
- useEffect(() => {
- switch (active) {
- case "form":
- setClassName({ form: "tab active", static: "tab", classForm: "tab" });
- setTabContentClassName({ form: "tab-content active", static: "tab-content", classForm: "tab-content" });
- break;
- case "static":
- setClassName({ form: "tab", static: "tab active", classForm: "tab" });
- setTabContentClassName({ form: "tab-content", static: "tab-content active", classForm: "tab-content" });
- break;
- case "classForm":
- setClassName({ form: "tab", static: "tab", classForm: "tab active" });
- setTabContentClassName({ form: "tab-content", static: "tab-content", classForm: "tab-content active" });
- break;
- default:
- setClassName({ form: "tab active", static: "tab", classForm: "tab" });
- setTabContentClassName({ form: "tab-content active", static: "tab-content", classForm: "tab-content" });
- }
- }, [active, setTabContentClassName])
- const tabList = {
- form: { name: "form", className: "tab active" },
- static: { name: "static", className: "tab" },
- classForm: { name: "classForm", className: "tab" }
- }
+ const TAB_ID = ["form", "static", "classForm"];
return (
- {Object.entries(tabList).map(([id, obj]) => )}
+ {TAB_ID.map(id => )}
)
}
\ No newline at end of file
diff --git a/task-01/src/libs/contactAPI.js b/task-01/src/libs/contactAPI.js
new file mode 100644
index 0000000..cdabdfa
--- /dev/null
+++ b/task-01/src/libs/contactAPI.js
@@ -0,0 +1,14 @@
+import fetchAPI from "./fetchAPI";
+
+const baseUrl = 'http://localhost:3000/contact';
+
+export function setContactData(contactData, userID) {
+ const url = `${baseUrl}/${userID}`;
+ console.log("url", url);
+ return fetchAPI("PUT", url, contactData);
+}
+
+export function deleteContactData(userID) {
+ const url=`${baseUrl}/${userID}`;
+ return fetchAPI("PUT", url, {id: userID})
+}
\ No newline at end of file
diff --git a/task-01/src/libs/dataFlow.js b/task-01/src/libs/dataFlow.js
deleted file mode 100644
index f7bbfaf..0000000
--- a/task-01/src/libs/dataFlow.js
+++ /dev/null
@@ -1,66 +0,0 @@
-export async function loadData(setIsLoading){
- const response = await fetch('http://localhost:3000/user')
- .then(response =>{
- if(response.ok) {
- return response.json();
- }
- })
- .then(data => {
- if(data){
- let d = data[0];
- return d;
- }
- })
- .catch(error => {
- console.log("Error fetching data: ",error);
- })
- .finally(() => {
- setIsLoading(false);
- });
- return response;
-}
-
-export async function submitData(e, userData){
- e.preventDefault();
-
- const requestOptions = {
- method: 'PUT',
- headers: { 'Content-Type': 'application/json' },
- body: Array(JSON.stringify(userData))
- };
-
- fetch('http://localhost:3000/user/1', requestOptions)
- .then(response => {
- return response.json();
- });
-
-}
-
-export async function submitContact(e, contactData){
- e.preventDefault();
-
- const requestOptions = {
- method: 'PUT',
- headers: { 'Content-Type': 'application/json' },
- body: Array(JSON.stringify(contactData))
- };
-
- fetch('http://localhost:3000/contact/1', requestOptions)
- .then(response => {
- return response.json();
- });
-}
-
-export async function deleteContactData(){
-
- const requestOptions = {
- method: 'PUT',
- headers: { 'Content-Type': 'application/json' },
- body: Array(JSON.stringify({id: 1}))
- }
-
- fetch('http://localhost:3000/contact/1', requestOptions)
- .then(response => {
- return response.json();
- });
-}
\ No newline at end of file
diff --git a/task-01/src/libs/fetchAPI.js b/task-01/src/libs/fetchAPI.js
new file mode 100644
index 0000000..64ade20
--- /dev/null
+++ b/task-01/src/libs/fetchAPI.js
@@ -0,0 +1,20 @@
+export default async function fetchAPI(method, url, body = null) {
+
+ const requestOptions = {
+ method: method,
+ headers: { 'Content-Type': 'application/json' },
+ body: body ? JSON.stringify(body) : null
+ }
+
+ console.log("requestOptions", requestOptions);
+ const response = await fetch(url, requestOptions)
+ .then(response => { return response.json() })
+ .then(data => { if (data) { return data[0] } })
+ .catch(error => {
+ console.log("Error fetching data: ", error);
+ })
+
+ return response;
+
+
+}
\ No newline at end of file
diff --git a/task-01/src/libs/formAPI.js b/task-01/src/libs/formAPI.js
new file mode 100644
index 0000000..a72664b
--- /dev/null
+++ b/task-01/src/libs/formAPI.js
@@ -0,0 +1,14 @@
+import fetchAPI from "./fetchAPI";
+
+const baseUrl = 'http://localhost:3000/user';
+
+export function editUser(e, userData, userID) {
+ e.preventDefault();
+ const url = `${baseUrl}/${userID}`;
+ return fetchAPI("PUT", url, userData);
+}
+
+export async function loadUser(){
+ const url = baseUrl;
+ return fetchAPI("GET", url);
+}
\ No newline at end of file