commit 7495b9eb08fa3bd26c5de1d1f29d7c1a38726ba4
Author: plundrichov <monika.plundrichova@yoso.fi>
Date:   Wed Jul 1 15:57:20 2020 +0200

    re #58 refactoring (api - app, calendar)

diff --git a/client/src/App.js b/client/src/App.js
index c7397b5..3a81207 100644
--- a/client/src/App.js
+++ b/client/src/App.js
@@ -12,48 +12,16 @@ import Login from './Login';
 // import Komponenta1 from './Komponenta1';
 // import Komponenta2 from './Komponenta2';
 import { BrowserRouter, Route, Switch } from "react-router-dom";
+import * as api_fetch from './api'
 
 function App() {
 
-  useEffect( () => {
-    getData();
+  useEffect(() => {
+    api_fetch.getCurrentProfile().then(currentProfile => {
+    setUserName(currentProfile);
+    });
   }, []);
 
-  const getData = async () => {
-    try {
-    const response = await fetch(
-      'http://devcz.yoso.fi:8090/ymanager/users/current/profile', {
-        headers: {
-          Authorization: 1
-        }
-      }
-    );
-
-    if (response.ok) {
-    const data = await response.json();
-    setUserName({
-      name: data.firstName + ' ' + data.lastName,
-      role: data.role,
-      id: data.id,
-      holiday: data.vacationCount,
-      sickday: data.sickDayCount
-    })
-  } else {
-    if(response.status === 400) {
-      alert('error 400 GET DATA APP')
-   }
-      else if (response.status === 500) {
-         alert ('error 500 GET DATA APP')
-      }
-      else {
-         alert('error GET DATA APP')
-      }
-  }
-} catch (e) {
-  console.log(e)
-  alert('error catch GET DATA APP')
-  }
-}
 
   const [userName, setUserName] = useState([
     {
diff --git a/client/src/Calendar.js b/client/src/Calendar.js
index d9451cd..1b3fcdb 100644
--- a/client/src/Calendar.js
+++ b/client/src/Calendar.js
@@ -7,100 +7,28 @@ import '@fullcalendar/daygrid/main.css'
 import interactionPlugin from '@fullcalendar/interaction';
 import Popup from "reactjs-popup";
 import moment from 'moment';
+import * as api_fetch from './api'
 
 
 function Calendar(props) {
   
   useEffect( () => {
     if (props.userName.id !== undefined) {
-      props.userName.role === 'EMPLOYER' ? getDataAdmin() : getData();
+      props.userName.role === 'EMPLOYER'
+        ?
+          api_fetch.getAdminCalendar().then(adminCalendar => {
+            props.setRequest(adminCalendar)
+          })
+        :
+          api_fetch.getUserCalendar(props.userName, todayTwo).then(userCalendar => {
+            props.setRequest(userCalendar)
+          });
     }
   }, [props.userName.id]);
 
   // LOAD DATA from server to calendar **** EMPLOYEE ****
-  const getData = async () => {
-    try {
-    const response = await fetch(
-      `http://devcz.yoso.fi:8090/ymanager/user/${props.userName.id}/calendar?from=${todayTwo}&status=ACCEPTED&status=REJECTED`, {
-        headers: {
-          'Accept': 'application/json',
-          Authorization: 6
-        },
-        method: 'GET',
-      }
-    );
-
-    if (response.ok) {
-    const data = await response.json();
-    
-    props.setRequest(data.filter(day => {
-      return day.status !== 'PENDING'
-    }).map(day => {
-
-      const newDate = day.date.split("/").join("-");
-
-      return ({
-      title: props.userName.name,
-      start: newDate,
-      backgroundColor: day.status === 'REJECTED' ? 'red' : 'green'
-      })
-    }))
-  } else {
-    if(response.status === 400) {
-      alert('error 400 LOADING DATA (CALENDAR, EMPLOYEE)')
-   }
-      else if (response.status === 500) {
-         alert ('error 500 LOADING DATA (CALENDAR, EMPLOYEE)')
-      }
-      else {
-         alert('error LOADING DATA (CALENDAR, EMPLOYEE)')
-      }
-  }
-  } catch (e) {
-    alert('error catch LOADING DATA (CALENDAR, EMPLOYEE)')
-  }
-  }
+  
   // LOAD DATA from server to calendar **** EMPLOYER ****
-  const getDataAdmin = async () => {
-    try {
-    const response = await fetch(
-      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=ACCEPTED', {
-        headers: {
-          'Accept': 'application/json',
-          Authorization: 1
-        },
-        method: 'GET',
-      }
-    );
-
-    if (response.ok) {
-    const data = await response.json();
-    
-    props.setRequest(data.map(day => {
-
-      const newDate = day.date.split("/").join("-");
-
-      return ( {
-      title: day.firstName + ' ' + day.lastName,
-      start: newDate
-      })
-    }))
-  } else {
-    if(response.status === 400) {
-      alert('error 400 LOADING DATA (CALENDAR, EMPLOYER)')
-   }
-      else if (response.status === 500) {
-         alert ('error 500 LOADING DATA (CALENDAR, EMPLOYER))')
-      }
-      else {
-         alert('error LOADING DATA (CALENDAR, EMPLOYER)')
-      }
-  }
-  } catch (e) {
-    alert('error catch LOADING DATA (CALENDAR, EMPLOYER)')
-  }
-}
-
 
   //states
   const [isOpen, setOpen] = useState(false)
@@ -117,129 +45,64 @@ function Calendar(props) {
   today = today.toISOString().split('T')[0]
   const todayTwo = today.split("-").join("/")
 
-
 // ********************* ADD EVENT - EMPLOYEE **************************
 
-  const addEvent = async (e) => {
-    e.preventDefault();
+  
+// ********************* ADD EVENT ADMIN - EMPLOYER **************************
+
+const addEventAdmin = async (e) => {
+  e.preventDefault();
 
-  // setting an object
-    const newDate = whatDate.split("-").join("/");
-      
-    try {
-  // send accepted request to server
-      const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
-        headers: {
-          Authorization: 6,
-          'Content-Type': 'application/json',
-        },
-        method: 'POST',
-  // object which is sent to server
-        body: JSON.stringify({
-          type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
-          date: newDate,
-          from: typeRadio === 'sickday' ? null : "00:00",
-          to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
-        }),
-      });
-      if (response.ok) {
+// setting an object
+  const newDate = whatDate.split("-").join("/");
 
-      const response = await fetch(
-        'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
-          headers: {
-            Authorization: 1
-          },
-        }
-  
-       );
-      const data = await response.json();
+  const peps = {
+    type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
+    date: newDate,
+    from: typeRadio === 'sickday' ? null : "00:00",
+    to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
+  };
 
-      props.setUser(data.map(request => {
-        const a = request.date;
-        const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
+  api_fetch.tvojefunkce(peps)
+    
+  try {
+// send accepted request to server
+    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
+      headers: {
+        Authorization: 1,
+        'Content-Type': 'application/json',
+      },
+      method: 'POST',
+// object which is sent to server
+      body: JSON.stringify(peps),
+    });
+    if (response.ok) {
   
-        return (
-          {
-            title: request.firstName + ' ' + request.lastName,
-            id: request.id,
-            type: request.type,
-            start: b,
-            end: null,
-            status: request.status
-        })
-      }))
-
-  } else {
 
-    if(response.status === 400) {
-    alert('error 400 ADD EVENT - EMPLOYEE')
+  const userProps = {
+    title: props.userName.name,
+    start: whatDate
+  
+}
+//concat new request to current ones
+    props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
+} else {
+  if(response.status === 400) {
+    alert('error 400 ADD EVENT ADMIN - EMPLOYER')
  }
     else if (response.status === 500) {
-       alert ('error 500 ADD EVENT - EMPLOYEE')
+       alert ('error 500 ADD EVENT ADMIN - EMPLOYER')
     }
     else {
-       alert('error ADD EVENT - EMPLOYEE')
-    }
-    
-  }
-    } catch (e) {
-      alert('error catch ADD EVENT - EMPLOYEE')
+       alert('error ADD EVENT ADMIN - EMPLOYER')
     }
-
-    setOpen(false)}
-
-  
-// ********************* ADD EVENT ADMIN - EMPLOYER **************************
-
-  const addEventAdmin = async (e) => {
-    e.preventDefault();
-
-  // setting an object
-    const newDate = whatDate.split("-").join("/");
-      
-    try {
-  // send accepted request to server
-      const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
-        headers: {
-          Authorization: 1,
-          'Content-Type': 'application/json',
-        },
-        method: 'POST',
-  // object which is sent to server
-        body: JSON.stringify({
-          type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
-          date: newDate,
-          from: typeRadio === 'sickday' ? null : "00:00",
-          to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
-        }),
-      });
-      if (response.ok) {
-    
-
-    const userProps = {
-      title: props.userName.name,
-      start: whatDate
-    
-  }
-  //concat new request to current ones
-      props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
-  } else {
-    if(response.status === 400) {
-      alert('error 400 ADD EVENT ADMIN - EMPLOYER')
-   }
-      else if (response.status === 500) {
-         alert ('error 500 ADD EVENT ADMIN - EMPLOYER')
-      }
-      else {
-         alert('error ADD EVENT ADMIN - EMPLOYER')
-      }
+}
+  } catch (e) {
+    alert('error catch ADD EVENT ADMIN - EMPLOYER')
   }
-    } catch (e) {
-      alert('error catch ADD EVENT ADMIN - EMPLOYER')
-    }
 
 
-    setOpen(false)}
+  setOpen(false)}
     
 
   return (
@@ -267,7 +130,10 @@ function Calendar(props) {
     >
     <div className="calendar-form">
       {/* <form onSubmit={(e) => addEvent(e)}> */}
-      <form onSubmit={props.userName.role === 'EMPLOYER' ? (e) => addEventAdmin(e) : (e) => addEvent(e) }>
+      <form onSubmit={props.userName.role === 'EMPLOYER' 
+      ? (e) => addEventAdmin(e).then(eventAdmin => { props.setUser(eventAdmin)}) 
+      : (e) => api_fetch.addEvent(e).then(userEvent => { props.setUser(userEvent)})
+      }>
         <h2>Choose an option</h2>
         <div className="calendar-radio">
           <input checked={
diff --git a/client/src/api.js b/client/src/api.js
new file mode 100644
index 0000000..9c0c615
--- /dev/null
+++ b/client/src/api.js
@@ -0,0 +1,194 @@
+// ******************** GET DATA APP getCurrentProfile, [userName, setUserName] ********************
+
+export const getCurrentProfile = async () => {
+
+    try {
+    const response = await fetch(
+      'http://devcz.yoso.fi:8090/ymanager/users/current/profile', {
+        headers: {
+          Authorization: 1
+        }
+      }
+    );
+
+    if (response.ok) {
+        const data = await response.json();
+        return {
+        name: data.firstName + ' ' + data.lastName,
+        role: data.role,
+        id: data.id,
+        holiday: data.vacationCount,
+        sickday: data.sickDayCount
+    }
+    } else {
+        if(response.status === 400) {
+        alert('error 400 GET DATA APP (getCurrentProfile)')
+    }
+        else if (response.status === 500) {
+            alert ('error 500 GET DATA APP (getCurrentProfile)')
+        }
+        else {
+            alert('error GET DATA APP (getCurrentProfile)')
+        }
+    }
+
+} catch (e) {
+  alert('error catch GET DATA APP (getCurrentProfile)')
+  }
+}
+
+// ******************** LOAD DATA to CALENDAR - EMPLOYEE ********************
+export const getUserCalendar = async (userName, fromDate ) => {
+  try {
+  const response = await fetch(
+    `http://devcz.yoso.fi:8090/ymanager/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
+      headers: {
+        'Accept': 'application/json',
+        Authorization: 6
+      },
+      method: 'GET',
+    }
+  );
+
+  if (response.ok) {
+  const data = await response.json();
+  
+  return data.filter(day => {
+    return day.status !== 'PENDING'
+  }).map(day => {
+
+    const newDate = day.date.split("/").join("-");
+
+    return ({
+    title: userName.name,
+    start: newDate,
+    backgroundColor: day.status === 'REJECTED' ? 'red' : 'green'
+    })
+  })
+} else {
+  if(response.status === 400) {
+    alert('error 400 LOADING DATA (CALENDAR, EMPLOYEE)')
+ }
+    else if (response.status === 500) {
+       alert ('error 500 LOADING DATA (CALENDAR, EMPLOYEE)')
+    }
+    else {
+       alert('error LOADING DATA (CALENDAR, EMPLOYEE)')
+    }
+}
+} catch (e) {
+  alert('error catch LOADING DATA (CALENDAR, EMPLOYEE)')
+}
+}
+
+// ******************** LOAD DATA to CALENDAR - EMPLOYER ********************
+export const getAdminCalendar = async () => {
+    try {
+    const response = await fetch(
+      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=ACCEPTED', {
+        headers: {
+          'Accept': 'application/json',
+          Authorization: 1
+        },
+        method: 'GET',
+      }
+    );
+
+    if (response.ok) {
+    const data = await response.json();
+    
+    return data.map(day => {
+
+      const newDate = day.date.split("/").join("-");
+
+      return ( {
+      title: day.firstName + ' ' + day.lastName,
+      start: newDate
+      })
+    })
+  } else {
+    if(response.status === 400) {
+      alert('error 400 LOADING DATA (CALENDAR, EMPLOYER)')
+   }
+      else if (response.status === 500) {
+         alert ('error 500 LOADING DATA (CALENDAR, EMPLOYER))')
+      }
+      else {
+         alert('error LOADING DATA (CALENDAR, EMPLOYER)')
+      }
+  }
+  } catch (e) {
+    alert('error catch LOADING DATA (CALENDAR, EMPLOYER)')
+  }
+}
+
+// ******************** ADD EVENT to CALENDAR - EMPLOYEE ********************
+export const addEvent = async (e) => {
+    e.preventDefault();
+
+  // setting an object
+    const newDate = whatDate.split("-").join("/");
+      
+    try {
+  // send accepted request to server
+      const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
+        headers: {
+          Authorization: 6,
+          'Content-Type': 'application/json',
+        },
+        method: 'POST',
+  // object which is sent to server
+        body: JSON.stringify({
+          type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
+          date: newDate,
+          from: typeRadio === 'sickday' ? null : "00:00",
+          to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
+        }),
+      });
+      if (response.ok) {
+
+      const response = await fetch(
+        'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
+          headers: {
+            Authorization: 1
+          },
+        }
+  
+       );
+      const data = await response.json();
+
+      return data.map(request => {
+        const a = request.date;
+        const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
+  
+        return (
+          {
+            title: request.firstName + ' ' + request.lastName,
+            id: request.id,
+            type: request.type,
+            start: b,
+            end: null,
+            status: request.status
+        })
+      })
+
+  } else {
+
+    if(response.status === 400) {
+    alert('error 400 ADD EVENT - EMPLOYEE')
+ }
+    else if (response.status === 500) {
+       alert ('error 500 ADD EVENT - EMPLOYEE')
+    }
+    else {
+       alert('error ADD EVENT - EMPLOYEE')
+    }
+    
+  }
+    } catch (e) {
+      alert('error catch ADD EVENT - EMPLOYEE')
+    }
+
+    setOpen(false)}
+    
+//
\ No newline at end of file
diff --git a/client/src/index.js b/client/src/index.js
index 13eceb0..f5185c1 100644
--- a/client/src/index.js
+++ b/client/src/index.js
@@ -4,8 +4,6 @@ import './index.css';
 import App from './App';
 import * as serviceWorker from './serviceWorker';
 
-window.$nameRequest = 'Angreštécie';
-
 ReactDOM.render(
   <React.StrictMode>
     <App />
