commit ebfe634710ac58738858d2736095348064363941
Author: plundrichov <monika.plundrichova@yoso.fi>
Date:   Wed Jul 8 09:12:25 2020 +0200

    re #58 client part(especially rebuilt api.js, all fetches, little edits in YourRequests/UpcomingRequests and fixed bugs in calendar and overview (add event, save, post, numbers)

diff --git a/client/src/App.js b/client/src/App.js
index dd99f97..412b0d1 100644
--- a/client/src/App.js
+++ b/client/src/App.js
@@ -16,18 +16,17 @@ function App() {
 
   useEffect(() => {
     api_fetch.getCurrentProfile().then(currentProfile => {
-    setUserName(currentProfile);
+      setUserName(currentProfile);
     }).catch(reason => {
-      alert(reason)
+        alert(reason)
     });
   }, []);
 
-
   const [userName, setUserName] = useState([
-    {
-    name: 'Golstoj Hyhrenciv'
-  }
-]);
+    { name: 'Golstoj Hyhrenciv' }
+  ]);
+
+
 
   return (
     <BrowserRouter>
@@ -35,13 +34,17 @@ function App() {
         <Nav userName={userName} />
         <div className="container">
         <Switch>
-          <Route path="/" exact component={() => <Home userName={userName}/>}/>
-          
-          <Route path="/setting">{userName.role === 'EMPLOYER' ? <Setting/>:
-           <div className="permissionText column">
-             <p>You don't have permission to access on this server.</p>
-            </div>}</Route> 
-
+          <Route path="/" exact component={() => <Home userName={userName} setUserName={setUserName}/>}/>
+          <Route path="/setting">
+            {userName.role === 'EMPLOYER'
+            ?
+              <Setting/> 
+            :
+              <div className="permissionText column">
+                <p>You don't have permission to access on this server.</p>
+              </div>
+            }
+          </Route> 
           <Route path="/logout"><LogOut/></Route>
           <Route path="/login"><Login/></Route>
         </Switch>  
@@ -61,18 +64,39 @@ const Home = (props) => {
     setUser(dataUserRequest);
   }, []);
 
+  // OverviewAdmin state 
+  const [employees, setEmployees] = useState([
+    {
+       name: 'Sadam',
+       id: 0,
+       sickday: 10,
+       holiday: 10,
+       takenSickday: 2
+    }
+ ]);
+
   function getDataUserRequest() {
     return ([]);
   }
   
   return (
-  <div className="container">
-    <div className="main-content">
-      {props.userName.role === 'EMPLOYER' ? <UpcomingRequests userRequest={userRequest} setUser={setUser} acceptedRequest={acceptedRequest} setRequest={setRequest} /> : <YourRequests userRequest={userRequest} setUser={setUser} acceptedRequest={acceptedRequest} setRequest={setRequest} userName={props.userName}/>}
-      <Calendar setUser={setUser} userRequest={userRequest} acceptedRequest={acceptedRequest} setRequest={setRequest} userName={props.userName}/> 
+    <div className="container">
+      <div className="main-content">
+        {props.userName.role === 'EMPLOYER'
+        ? 
+          <UpcomingRequests userRequest={userRequest} setUser={setUser} acceptedRequest={acceptedRequest} setRequest={setRequest} setEmployees={setEmployees}/>
+        : 
+          <YourRequests userRequest={userRequest} setUser={setUser} acceptedRequest={acceptedRequest} setRequest={setRequest} userName={props.userName}/>
+        }
+        <Calendar setUser={setUser} userRequest={userRequest} acceptedRequest={acceptedRequest} setRequest={setRequest} userName={props.userName} setEmployees={setEmployees} setUserName={props.setUserName}/> 
+      </div>
+      {props.userName.role === 'EMPLOYER'
+      ?
+        <OverviewAdmin employees={employees} setEmployees={setEmployees} />
+      :
+        <Overview userName={props.userName} employees={employees}  />
+      }
     </div>
-    {props.userName.role === 'EMPLOYER' ? <OverviewAdmin /> : <Overview userName={props.userName} />}
-  </div>
   )
 };
 
diff --git a/client/src/Calendar.js b/client/src/Calendar.js
index 484b8b0..8644807 100644
--- a/client/src/Calendar.js
+++ b/client/src/Calendar.js
@@ -54,6 +54,7 @@ function Calendar(props) {
 const addEvent = async (e) => {
   e.preventDefault();
   
+  try {
   // setting an object
   const newDate = whatDate.split("-").join("/");
 
@@ -64,13 +65,24 @@ const addEvent = async (e) => {
     to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
   }
 
-  api_fetch.addEventApi(dataAddEventEmployee).catch(reason => {
-    alert(reason)
-  });
+  await api_fetch.addEventApi(dataAddEventEmployee);
+    if (typeRadio === 'holiday') {
+      props.setUserName({
+        ...props.userName,
+        holiday: props.userName.holiday - whatTime
+      })
+    } else if (typeRadio === 'sickday') {
+      props.setUserName({
+        ...props.userName,
+        takenSickday: props.userName.takenSickday + 1
+      })
+    }
     
     setOpen(false)
+  } catch (e) {
+    alert(e)
   }
- 
+}
 // ********************* ADD EVENT ADMIN - EMPLOYER **************************
 
 const addEventAdmin = async (e) => {
@@ -94,13 +106,18 @@ const addEventAdmin = async (e) => {
   //concat new request to current ones
       props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
   }).catch(reason => {
-    alert(reason)
+      alert(reason)
   });
 
   setOpen(false)
+
+  api_fetch.getUsersOverview().then(usersOverview => {
+    props.setEmployees(usersOverview);
+    }).catch(reason => {
+    alert(reason)
+ });
 }
     
-
   return (
     <div className="calendar">
 
diff --git a/client/src/Overview.js b/client/src/Overview.js
index 92b6acb..f789896 100644
--- a/client/src/Overview.js
+++ b/client/src/Overview.js
@@ -22,7 +22,7 @@ const OffsAvailable = (props) => {
                      </tr>
                      <tr>
                         <td>{props.userName.name}</td>
-                        <td className="td-center">{props.userName.sickday}</td>
+                        <td className="td-center">{props.userName.takenSickday + '/' + props.userName.sickday}</td>
                         <td className="td-center">{props.userName.holiday}</td>
                      </tr>
                   </tbody>
diff --git a/client/src/OverviewAdmin.js b/client/src/OverviewAdmin.js
index 75173c4..769f925 100644
--- a/client/src/OverviewAdmin.js
+++ b/client/src/OverviewAdmin.js
@@ -3,25 +3,25 @@ import './App.css';
 import * as api_fetch from './api'
 
 
-const OverviewAdmin = () => {
+const OverviewAdmin = (props) => {
 
    useEffect(() => {
       api_fetch.getUsersOverview().then(usersOverview => {
-         setEmployees(usersOverview);
+         props.setEmployees(usersOverview);
          }).catch(reason => {
          alert(reason)
       });
    }, []);
 
    // states
-   const [employees, setEmployees] = useState([
-      {
-         name: 'Sadam',
-         id: 0,
-         sickday: 10,
-         holiday: 10
-      }
-   ]);
+   // const [employees, setEmployees] = useState([
+   //    {
+   //       name: 'Sadam',
+   //       id: 0,
+   //       sickday: 10,
+   //       holiday: 10
+   //    }
+   // ]);
 
    const [isEdit, setEdit] = useState(false);
    const [editedUserId, setEditedUserId] = useState();
@@ -30,7 +30,7 @@ const OverviewAdmin = () => {
 
    // functions
    function changeSickdays(newValue) {
-      const newEmployees = employees.map(employee => {
+      const newEmployees = props.employees.map(employee => {
          if (editedUserId === employee.id) {
             return {
                ...employee,
@@ -40,11 +40,11 @@ const OverviewAdmin = () => {
             return employee
          }
       })
-      setEmployees(newEmployees);
+      props.setEmployees(newEmployees);
    }
 
    function changeHoliday(newValue) {
-      const newEmployees = employees.map(employee => {
+      const newEmployees = props.employees.map(employee => {
          if (editedUserId === employee.id) {
             return {
                ...employee,
@@ -54,16 +54,16 @@ const OverviewAdmin = () => {
             return employee
          }
       })
-      setEmployees(newEmployees);
+      props.setEmployees(newEmployees);
    }
 
    const submitEdit = async (e) => {
       
       setEdit(isEdit === true ? false : true);
-      setPrevEdit(employees);
+      setPrevEdit(props.employees);
       e.preventDefault();
 
-      const found = employees.find(employee => editedUserId === employee.id);
+      const found = props.employees.find(employee => editedUserId === employee.id);
       const foundPrevEdit = prevEdit.find(employee => editedUserId === employee.id);
 
       const dataOverviewObject = {
@@ -79,7 +79,7 @@ const OverviewAdmin = () => {
    }
 
    const cancel = () => {
-      setEmployees(prevEdit)
+      props.setEmployees(prevEdit)
       setEdit(false)
    }
 
@@ -88,7 +88,7 @@ const OverviewAdmin = () => {
    const editEmployee = (employeeId, e) => {
       setEdit(true)
       setEditedUserId(employeeId)
-      setPrevEdit(employees)
+      setPrevEdit(props.employees)
       
      e.preventDefault();
    }
@@ -112,15 +112,15 @@ const OverviewAdmin = () => {
                   <th>Holiday</th>
                   <th></th>
                </tr>
-                  {employees.map(employee => (
+                  {props.employees.map(employee => (
                <tr>
-                  <td>{employee.name} {editedUserId}</td>
+                  <td>{employee.name}</td>
                   {/* SickDays Input */}
                   <td className="td-center">
                      {isEdit === true && editedUserId === employee.id ? (
                         <input className="offsInput" type="number" min="0" value={employee.sickday} onChange={(e) => changeSickdays(e.target.value)}/>
                      ) : (
-                        employee.sickday
+                        employee.takenSickday + '/' + employee.sickday
                      )}
                   </td>
                   {/* Holiday Input */}
diff --git a/client/src/UpcomingRequests.js b/client/src/UpcomingRequests.js
index 5402b20..95936b9 100644
--- a/client/src/UpcomingRequests.js
+++ b/client/src/UpcomingRequests.js
@@ -1,5 +1,6 @@
-import React, { useEffect } from 'react';
+import React, { useState, useEffect } from 'react';
 import './App.css';
+import moment from 'moment';
 import * as api_fetch from './api'
 
 function UpcomingRequests(props) {
@@ -21,10 +22,10 @@ function UpcomingRequests(props) {
           {
             title: request.firstName + ' ' + request.lastName,
             id: request.id,
-            type: request.type,
-            start: b,
+            type: convertVacationType(request.type),
+            start: moment(b).format("D.M.YYYY"),
             end: null,
-            status: request.status
+            status: request.status = request.status.toLowerCase()
         })
       }))
     }).catch(reason => {
@@ -32,49 +33,66 @@ function UpcomingRequests(props) {
     });
 }
 
+function convertVacationType(vacationType) {
+  switch (vacationType) {
+    case 'SICK_DAY' :
+      return 'sickday';
+    case 'HOLIDAY':
+      return 'holiday';
+  }
+}
+
 
 
   // send accepted request to server
   const acceptRequest = async (user) => {
+    try {
 
-    const acceptedRequests = {
-      id: user.id,
-      status: 'ACCEPTED',
-    }
+      const acceptedRequests = {
+        id: user.id,
+        status: 'ACCEPTED',
+      }
     
-    api_fetch.sendAcceptedRequest(acceptedRequests).then((data) => {
-      
-    const userProps = {
-          title: user.title,
-          id: 0,
+      await api_fetch.sendAcceptedRequest(acceptedRequests).then(() => {
+        
+        const userProps = {
+              title: user.title,
+              id: 0,
+              type: user.type, 
           type: user.type, 
-          start: user.start
-      }
-      //concat new request to current ones
+              type: user.type, 
+              start: user.start
+        }
+        //concat new request to current ones
           props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
-      //request accept button
+        //request accept button
           props.setUser((pendingRequest) => pendingRequest.filter((item) => item !== user));
-    }).catch(response => {
-      alert(response)
-    })
+      })
+    } catch (e) {
+      throw 'error catch GET DATA APP (getCurrentProfile)'
+    }
   }
 
- 
 
   //send rejected request to server
   const declineRequest = async (user) => {
+    try{
 
-    const rejectedRequest = {
-      id: user.id,
-      status: 'REJECTED',
-    }
+      const rejectedRequest = {
+        id: user.id,
+        status: 'REJECTED',
+      }
 
-    api_fetch.sendRejectedRequest(rejectedRequest).then((data) => {
-      //request cancel button
+      await api_fetch.sendRejectedRequest(rejectedRequest);
+      
       props.setUser((acceptedRequest) => acceptedRequest.filter((item) => item !== user))
-    }).catch(reason => {
-      alert(reason)
-    });
+
+      const usersOverview = await api_fetch.getUsersOverview();
+      props.setEmployees(usersOverview);
+
+  } catch (e) {
+    alert(e)
+    }
   }
 
   return (
@@ -84,6 +102,8 @@ function UpcomingRequests(props) {
       <div className="offs-items column">
         <div className="offs-item row">
           <table>
+            {/* {props.userRequest.length > 0 
+            ? */}
             <tbody>
               <tr>
                 <th>Name</th>
@@ -102,6 +122,12 @@ function UpcomingRequests(props) {
               </tr>
               ))}
             </tbody>
+            {/* :
+            <tbody>
+              <p>There are no requests.</p>
+            </tbody>
+             } */}
+            
           </table>
         </div>
       </div>
diff --git a/client/src/YourRequests.js b/client/src/YourRequests.js
index 99ac928..42ca2ef 100644
--- a/client/src/YourRequests.js
+++ b/client/src/YourRequests.js
@@ -1,5 +1,6 @@
 import React, { useEffect } from 'react';
 import './App.css';
+import moment from 'moment';
 import * as api_fetch from './api'
 
 function YourRequests(props) {
@@ -21,9 +22,9 @@ function YourRequests(props) {
         {
           title: props.userName.name,
           id: request.id,
-          start: b,
-          status: request.status,
-          type: request.type
+          start: moment(b).format("D.M.YYYY"),
+          status: request.status = request.status.toLowerCase(),
+          type: convertVacationType(request.type)
         }
       )
     }))
@@ -31,6 +32,23 @@ function YourRequests(props) {
       alert(reason)
     });
 }
+
+function convertVacationType(vacationType) {
+  // vacationType =  'SICK_DAY' ? 'sickday' : 'holiday'
+
+  // if (vacationType = 'SICK_DAY') {
+  // return: 'sickday',
+  // } else {
+  //   return: 'holiday'
+  // }
+
+  switch (vacationType) {
+    case 'SICK_DAY' :
+      return 'sickday';
+    case 'HOLIDAY':
+      return 'holiday';
+  }
+}
   
 
   return (
@@ -40,6 +58,8 @@ function YourRequests(props) {
       <div className="offs-items column">
         <div className="offs-item row">
           <table>
+            {props.userRequest.length > 0 ?
+            
             <tbody>
               <tr>
                 <th>Name</th>
@@ -48,7 +68,7 @@ function YourRequests(props) {
                 <th>Status</th>    
               </tr>
               {props.userRequest.map(user => (
-              <tr key={user.id}>
+                <tr key={user.id}>
                 <td>{user.title}</td>
                 <td>{user.type}</td>    
                 <td>{user.end ? user.start + " - " + user.end : user.start}</td>
@@ -56,6 +76,10 @@ function YourRequests(props) {
               </tr>
               ))}
             </tbody>
+            :
+            <tbody>
+              <div>No requests</div>
+            </tbody>}
           </table>
         </div>
       </div>
diff --git a/client/src/api.js b/client/src/api.js
index d0e6d59..09748c3 100644
--- a/client/src/api.js
+++ b/client/src/api.js
@@ -1,93 +1,108 @@
+
 const http = 'http://devcz.yoso.fi:8090/ymanager';
 
 // ******************** GET DATA APP getCurrentProfile ********************
 
 export const getCurrentProfile = async () => {
 
-    try {
-    const response = await fetch(
+  let response;
+
+  try {
+    response = await fetch(
       `${http}/users/current/profile`, {
         headers: {
           Authorization: 1
         }
       }
-    );
+    );    
+  } catch (e) {
+    throw 'Server is not available'
+    }
 
-    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
+  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,
+      takenSickday: data.takenSickDayCount
     }
-    } else {
-        if(response.status === 400) {
-          throw 'error 400 GET DATA APP (getCurrentProfile)'
-        }
-        else if (response.status === 500) {
-          throw 'error 500 GET DATA APP (getCurrentProfile)'
-        }
-        else {
-          throw 'error GET DATA APP (getCurrentProfile)'
-        }
+  } else {
+      switch (response.status) {
+        case 401:
+          throw new Error('Not authenticated.')
+        case 500:
+          throw new Error('Internal server error.')
+        default:
+          throw new Error(response.statusText)
+      }
     }
-
-} catch (e) {
-  throw 'error catch GET DATA APP (getCurrentProfile)'
-  }
 }
 
 // ******************** LOAD DATA to CALENDAR - EMPLOYEE ********************
 export const getUserCalendar = async (userName, fromDate ) => {
+
+  let response;
+
   try {
-  const response = await fetch(
-    `${http}/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
-      headers: {
-        'Accept': 'application/json',
-        Authorization: 6
-      },
-      method: 'GET',
-    }
-  );
+    response = await fetch(
+      `${http}/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
+        headers: {
+          'Accept': 'application/json',
+          Authorization: 6
+        },
+        method: 'GET',
+      }
+    );
+  } catch (e) {
+    throw 'Server is not available'
+  }
 
   if (response.ok) {
-  const data = await response.json();
+    const data = await response.json();
   
-  return data.filter(day => {
-    return day.status !== 'PENDING'
-  }).map(day => {
+    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'
+      title: userName.name,
+      start: newDate,
+      backgroundColor: day.status === 'REJECTED' ? 'red' : 'green'
     })
   })
-} else {
-    if(response.status === 400) {
-      throw 'error 400 LOADING DATA (CALENDAR, EMPLOYEE)'
-    }
-    else if (response.status === 500) {
-      throw 'error 500 LOADING DATA (CALENDAR, EMPLOYEE)'
-    }
-    else {
-      throw 'error LOADING DATA (CALENDAR, EMPLOYEE)'
+  } else {
+      switch (response.status) {
+        case 400:
+          throw new Error('Bad request. Check query parameters.')
+        case 401:
+          throw new Error('Not authenticated.')
+        case 403:
+          throw new Error('Not authorized.')
+        case 404:
+          throw new Error('User with given ID does not exist.')
+        case 500:
+          throw new Error('Internal server error.')
+        default:
+          throw new Error(response.statusText)
+      }
     }
 }
-} catch (e) {
-  throw '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', {
+
+  let response;
+
+  try {
+    response = await fetch(
+      `${http}/users/requests/vacation?status=ACCEPTED`, {
         headers: {
           'Accept': 'application/json',
           Authorization: 1
@@ -95,8 +110,11 @@ export const getAdminCalendar = async () => {
         method: 'GET',
       }
     );
+  } catch (e) {
+    throw 'Server is not available'
+  }
 
-    if (response.ok) {
+  if (response.ok) {
     const data = await response.json();
     
     return data.map(day => {
@@ -109,26 +127,29 @@ export const getAdminCalendar = async () => {
       })
     })
   } else {
-      if(response.status === 400) {
-        throw 'error 400 LOADING DATA (CALENDAR, EMPLOYER)'
-      }
-      else if (response.status === 500) {
-        throw 'error 500 LOADING DATA (CALENDAR, EMPLOYER))'
-      }
-      else {
-        throw 'error LOADING DATA (CALENDAR, EMPLOYER)'
+      switch (response.status) {
+        case 400:
+          throw new Error('Bad request. Check query parameters.')
+        case 401:
+          throw new Error('Not authenticated.')
+        case 403:
+          throw new Error('Not authorized.')
+        case 500:
+          throw new Error('Internal server error.')
+        default:
+          throw new Error(response.statusText)
       }
     }
-  } catch (e) {
-    throw 'error catch LOADING DATA (CALENDAR, EMPLOYER)'
-  }
 }
 
 // ******************** ADD EVENT to CALENDAR - EMPLOYEE ********************
+
 export async function addEventApi(dataAddEventEmployee) {
-try {
+  let response;
+
+  try {
   // send accepted request to server
-    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
+    response = await fetch(`${http}/user/calendar/create`, {
       headers: {
         Authorization: 6,
         'Content-Type': 'application/json',
@@ -137,15 +158,19 @@ try {
   // object which is sent to server
   body: JSON.stringify(dataAddEventEmployee),
     });
-    if (response.ok) {
+  } catch (e) {
+    throw 'Server is not available'
+  }
+
+  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();
+    response = await fetch(
+    `${http}/users/requests/vacation?status=PENDING`, {
+      headers: {
+        Authorization: 1
+      },
+    });
+    const data = await response.json();
       
     return data.map(request => {
       const a = request.date;
@@ -163,98 +188,111 @@ try {
     })
 
   } else {
-    
-    if(response.status === 400) {
-      throw 'error 400 ADD EVENT - EMPLOYEE'
-    }
-    else if (response.status === 500) {
-      throw 'error 500 ADD EVENT - EMPLOYEE'
+      switch (response.status) {
+        case 400:
+          throw new Error('Bad request. Check request body.')
+        case 401:
+          throw new Error('Not authenticated.')
+        case 403:
+          throw new Error('Not authorized.')
+        case 500:
+          throw new Error('Internal server error.')
+        default:
+          throw new Error(response.statusText)
+      }    
     }
-    else {
-      throw 'error ADD EVENT - EMPLOYEE'
-    }
-  }
-} catch (e) {
-    throw 'error catch ADD EVENT - EMPLOYEE'
-  }
 }
       
 // ******************** ADD EVENT to CALENDAR - EMPLOYER ********************
+
 export async function addEventApiAdmin(dataAddEventAdmin) {
+  let response;
+
   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',
+  // send accepted request to server
+    response = await fetch(`${http}/user/calendar/create`, {
+      headers: {
+        Authorization: 1,
+        'Content-Type': 'application/json',
+    },
+      method: 'POST',
     // object which is sent to server
-          body: JSON.stringify(dataAddEventAdmin),
-        });
-        if (response.ok) {
-          return;
-    } else {
-      if(response.status === 400) {
-        throw('error 400 ADD EVENT ADMIN - EMPLOYER')
-     }
-        else if (response.status === 500) {
-           throw ('error 500 ADD EVENT ADMIN - EMPLOYER')
-        }
-        else {
-           throw('error ADD EVENT ADMIN - EMPLOYER')
-        }
-    }
+      body: JSON.stringify(dataAddEventAdmin),
+    });
   } catch (e) {
-      throw('error catch ADD EVENT ADMIN - EMPLOYER')
-    }
+    throw 'Server is not available'
+  }
+
+  if (!response.ok) {
+    switch (response.status) {
+      case 400:
+        throw new Error('Bad request. Check request body.')
+      case 401:
+        throw new Error('Not authenticated.')
+      case 403:
+        throw new Error('Not authorized.')
+      case 500:
+        throw new Error('Internal server error.')
+      default:
+        throw new Error(response.statusText)
+    }    
+  }
 }
 
 // ******************** GET DATA to OVERVIEW - EMPLOYER ********************
+
 export const getUsersOverview = async () => {
+  let response;
+
   try {
-  const response = await fetch (
-     'http://devcz.yoso.fi:8090/ymanager/users', {
+  response = await fetch (
+    `${http}/users`, {
       headers: {
         Authorization: 1          }
     }
   );
+  } catch (e) {
+    throw 'Server is not available'
+  }
   
-if (response.ok) {
+  if (response.ok) {
 
-  const data = await response.json();
-  return data.map(user => {
+    const data = await response.json();
+    return data.map(user => {
 
-     return (
-        {
+     return ({
            name: user.firstName + ' ' + user.lastName,
            id: user.id,
            sickday: user.sickDayCount,
            holiday: user.vacationCount,
+           takenSickday: user.takenSickDayCount,
            role: user.role
         })
-  })
-}  else {
-      if(response.status === 400) {
-        throw 'error 400 GET DATA (OVERVIEW, EMPLOYER)'
-      }
-      else if (response.status === 500) {
-        throw 'error 500 GET DATA (OVERVIEW, EMPLOYER)'
-      }
-      else {
-        throw 'error GET DATA (OVERVIEW, EMPLOYER)'
-        }
-      }
-  } catch (e) {
-      throw 'error catch GET DATA (OVERVIEW, EMPLOYER)'
+    })
+  } else {
+    switch (response.status) {
+      case 400:
+        throw new Error('Bad request. Check query parameters.')
+      case 401:
+        throw new Error('Not authenticated.')
+      case 403:
+        throw new Error('Not authorized.')
+      case 500:
+        throw new Error('Internal server error.')
+      default:
+        throw new Error(response.statusText)
+    }    
   }
 }
 
 // ******************** SAVE DATA to OVERVIEW - EMPLOYER ********************
+
 export async function saveDataOverview(dataOverviewObject) {
+  let response;
+
   try {
     // send accepted request to server
-        const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/settings', {
+        response = await fetch(`${http}/user/settings`, {
           headers: {
             Authorization: 1,
             'Content-Type': 'application/json',
@@ -264,155 +302,166 @@ export async function saveDataOverview(dataOverviewObject) {
     // object which is sent to server
           body: JSON.stringify(dataOverviewObject),        
         });
-        console.log(response.status)
-       if (response.status === 400) {
-        throw 'error 400 SAVE DATA (OVERVIEW, EMPLOYER)'
-          }
-      else if (response.status === 500) {
-        throw 'error 500 SAVE DATA (OVERVIEW, EMPLOYER)'
-      }
-      else if (!response.ok) {
-        throw 'error SAVE DATA (OVERVIEW, EMPLOYER)'
-      }
-
   } catch (e) {
-    throw 'error catch SAVE DATA (OVERVIEW, EMPLOYER'
+    throw 'Server is not available'
   }
+
+  if (!response.ok) {
+    switch (response.status) {
+      case 400:
+        throw new Error('Bad request. Check query parameters.')
+      case 401:
+        throw new Error('Not authenticated.')
+      case 403:
+        throw new Error('Not authorized.')
+      case 500:
+        throw new Error('Internal server error.')
+      default:
+        throw new Error(response.statusText)
+    }
+  }    
 }
 
 // ******************** LOAD DATA to SETTING - EMPLOYER ********************
 export const getSettingData = async () =>  {
+  let response;
+
   try {
     const response = await fetch(
-      'http://devcz.yoso.fi:8090/ymanager/settings', {
+      `${http}/settings`, {
         headers: {
           Authorization: 1
         }
       });
-
-      if (response.ok) {
-      const data = await response.json();
-      return {
-        sickday: data.sickDayCount,
-      }
-    } else {
-        if(response.status === 400) {
-          throw 'error 400 LOADING DATA (SETTING, EMPLOYER)'
-        }
-        else if (response.status === 500) {
-           throw 'error 500 LOADING DATA (SETTING, EMPLOYER)'
-        }
-        else {
-           throw 'error LOADING DATA (SETTING, EMPLOYER)'
-        }
-      }
   } catch (e) {
-    throw 'error catch LOADING DATA (SETTING, EMPLOYER)'
+    throw 'Server is not available'
+    }
+
+  if (response.ok) {
+    const data = await response.json();
+    return {
+      sickday: data.sickDayCount,
+    }
+  } else {
+    switch (response.status) {
+      case 500:
+        throw new Error('Internal server error.')
+      default:
+        throw new Error(response.statusText)
+      }            
     }
+
 }
 
 // ******************** SAVE DATA to SETTING - EMPLOYER ********************
 export async function saveDataSetting(dataSettingObject) {
+  let response;
+
   try {
-    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/settings', {
+    response = await fetch(`${http}/settings`, {
       headers: {
-        'Authorization': 6,
+        'Authorization': 1,
         'Content-Type': 'application/json'
       },
       method: 'POST',
       body: JSON.stringify(dataSettingObject),
     });
+  } catch (e) {
+    throw 'Server is not available'
+  }
 
+  if (!response.ok) {
     switch (response.status) {
-      case 200:
-        throw '...'
+      case 401:
+        throw new Error('Not authenticated.')
+      case 403:
+        throw new Error('Not authorized.')
       case 500:
-        throw ''
+        throw new Error('Internal server error.')
       default:
-        throw response.statusText
-
-    }
-
-    if(response.status === 400) {
-      throw 'error 400 SAVE DATA (OVERVIEW, EMPLOYER)'
-    }
-    else if (response.status === 500) {
-      throw 'error 500 SAVE DATA (OVERVIEW, EMPLOYER)'
-    }
-    else if (!response.ok) {
-      throw 'error SAVE DATA (OVERVIEW, EMPLOYER)'
-      }
-  } catch (e) {
-      throw 'error catch SAVE DATA (OVERVIEW, EMPLOYER)'
-    }
+        throw new Error(response.statusText)
+    }    
+  }
 }
 
 // ****************** LOAD DATA to YOUR REQUESTS - EMPLOYEE ******************
 
 export async function loadYourRequests() {
+  let response;
+  
   try {
-    const response = await fetch(
-      'http://devcz.yoso.fi:8090/ymanager/user/6/calendar?from=2020/06/24&status=PENDING', {
+    response = await fetch(
+      `${http}/user/6/calendar?from=2020/06/24&status=PENDING`, {
         headers: {
           Authorization: 6
         },
       }
     );
+  } catch (e) {
+    throw 'Server is not available'
+  }
 
   if (response.ok) {
     const data = await response.json();
     return data;
   } else {
-    if(response.status === 400) {
-      alert('error 400 GET DATA (YOUR REQUEST)')
-   }
-      else if (response.status === 500) {
-         alert ('error 500 GET DATA (YOUR REQUEST)')
-      }
-      else {
-         alert('error GET DATA (YOUR REQUEST)')
-      }
-  }
-} catch (e) {
-  console.log(e)
-  alert('error catch GET DATA (YOUR REQUEST)')
-  }
+      switch (response.status) {
+        case 400:
+          throw new Error('Bad request. Check query parameters.')
+        case 401:
+          throw new Error('Not authenticated.')
+        case 403:
+          throw new Error('Not authorized.')
+        case 500:
+          throw new Error('Internal server error.')
+        default:
+          throw new Error(response.statusText)
+      }    
+    }
 }
 
-// ****************** LOAD DATA - UPCOMING REQUESTS - EMPLOYER ******************
+// ****************** LOAD DATA - UPCOMING REQUESTS - EMPLOYER ****************** //tady
 export async function loadAdminRequests() {
+  let response;
+
   try {
-    const response = await fetch(
-      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
+    response = await fetch(
+      `${http}/users/requests/vacation?status=PENDING`, {
         headers: {
           Authorization: 1
         }
       },
     );
-
-     if (response.ok) {
+  } catch (e) {
+    throw 'Server is not available'
+    } 
+  
+  if (response.ok) {
     const data = await response.json();
       return data;
   } else {
-    if(response.status === 400) {
-      alert('error 400 GET DATA (UPCOMING REQUESTS)')
-   }
-      else if (response.status === 500) {
-         alert ('error 500 GET DATA (UPCOMING REQUESTS)')
-      }
-      else {
-         alert('error GET DATA (UPCOMING REQUESTS)')
-      }
+      switch (response.status) {
+        case 400:
+          throw new Error('Bad request. Check query parameters.')
+        case 401:
+          throw new Error('Not authenticated.')
+        case 403:
+          throw new Error('Not authorized.')
+        case 500:
+          throw new Error('Internal server error.')
+        default:
+          throw new Error(response.statusText)
+      }   
     }
-} catch (e) {
-  alert('error catch GET DATA (UPCOMING REQUESTS)')
-  } 
 }
 
 // ************** SEND ACCEPTED DATA - UPCOMING REQUESTS - EMPLOYER **************
+
 export async function sendAcceptedRequest(acceptedRequests) {
+  let response;
+
   try {
-    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/requests?type=VACATION', {
+    response = await fetch(`${http}/user/requests?type=VACATION`, {
       headers: {
         Authorization: 1,
         'Content-Type': 'application/json',
@@ -420,31 +469,35 @@ export async function sendAcceptedRequest(acceptedRequests) {
       method: 'PUT',
       body: JSON.stringify(acceptedRequests),
     });
-
-    if (response.ok) {
-    return;
-
-    } else {
-      if(response.status === 400) {
-        alert('error 400 SEND ACCEPTED DATA (UPCOMING REQUESTS)')
-     }
-        else if (response.status === 500) {
-           alert ('error 500 SEND ACCEPTED DATA (UPCOMING REQUESTS)')
-        }
-        else {
-           alert('error SEND ACCEPTED DATA (UPCOMING REQUESTS)')
-        }
-    }
   } catch (e) {
-    alert('error catch SEND ACCEPTED DATA (UPCOMING REQUESTS)')
+    throw 'Server is not available'
     }
+
+  if (!response.ok) {
+    switch (response.status) {
+      case 400:
+        throw new Error('Bad request. Check query parameters and request body.')
+      case 401:
+        throw new Error('Not authenticated.')
+      case 403:
+        throw new Error('Not authorized.')
+      case 404:
+        throw new Error('Neither vacation nor authorization request with given ID exists.')
+      case 500:
+        throw new Error('Internal server error.')
+      default:
+        throw new Error(response.statusText)
+    }   
+  }
 }
 
 // ************** SEND REJECTED DATA - UPCOMING REQUESTS - EMPLOYER **************
 
 export async function sendRejectedRequest(rejectedRequest) {
+  let response;
+  
   try {
-    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/requests?type=VACATION', {
+    response = await fetch(`${http}/user/requests?type=VACATION`, {
       headers: {
         Authorization: 1,
         'Content-Type': 'application/json',
@@ -452,24 +505,24 @@ export async function sendRejectedRequest(rejectedRequest) {
       method: 'PUT',
       body: JSON.stringify(rejectedRequest),
     });
-
-  if (response.ok) {    
-    return;
-
-  } else {
-    if(response.status === 400) {
-      alert('error 400 SEND REJECTED DATA (UPCOMING REQUESTS)')
-   }
-      else if (response.status === 500) {
-         alert ('error 500 SEND REJECTED DATA (UPCOMING REQUESTS)')
-      }
-      else {
-         alert('error SEND REJECTED DATA (UPCOMING REQUESTS)')
-      }
-  }
-} catch (e) {
-  alert('error catch SEND REJECTED DATA (UPCOMING REQUESTS)')
+  } catch (e) {
+    throw 'Server is not available'
+    }
+  
+  if (!response.ok) {
+    switch (response.status) {
+      case 400:
+        throw new Error('Bad request. Check query parameters and request body.')
+      case 401:
+        throw new Error('Not authenticated.')
+      case 403:
+        throw new Error('Not authorized.')
+      case 404:
+        throw new Error('Neither vacation nor authorization request with given ID exists.')
+      case 500:
+        throw new Error('Internal server error.')
+      default:
+        throw new Error(response.statusText)
+    }    
   }
 }
-
-
