Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 9f045397

Přidáno uživatelem plundrichov před více než 4 roky(ů)

re #58 Refactoring, every fetch moved to api.js

Zobrazit rozdíly:

client/src/App.css
13 13
    flex-direction: row;
14 14
}
15 15

  
16
/* --------------APP------------------*/
17

  
18
.permissionText {
19
    text-align: center;
20
    justify-content: center;
21
    left: 50%;
22
    top: 50%;
23
    transform: translate(-50%, -50%);
24
    position: absolute;
25
    font-size: 24px;
26
    font-weight: bold;
27
    color: rgb(255, 255, 255);
28
    width: 400px;
29
    height: 400px;
30
    border-radius: 50%;
31
    background-color: rgba(236, 236, 236, 0.192);
32
    padding: 20px;
33
}
34

  
16 35
/* ------------- Navbar --------------- */
17 36

  
18 37
.top-nav {
......
260 279
}
261 280
.login-container h1 {
262 281
    color: white;
263
    font-size: 110px;
282
    font-size: 90px;
264 283
}
265 284
/* ------------- SETTING -------------------*/
266 285

  
client/src/App.js
9 9
import Setting from './Setting';
10 10
import LogOut from './LogOut';
11 11
import Login from './Login';
12
// import Komponenta1 from './Komponenta1';
13
// import Komponenta2 from './Komponenta2';
14 12
import { BrowserRouter, Route, Switch } from "react-router-dom";
15 13
import * as api_fetch from './api'
16 14

  
......
19 17
  useEffect(() => {
20 18
    api_fetch.getCurrentProfile().then(currentProfile => {
21 19
    setUserName(currentProfile);
20
    }).catch(reason => {
21
      alert(reason)
22 22
    });
23 23
  }, []);
24 24

  
......
36 36
        <div className="container">
37 37
        <Switch>
38 38
          <Route path="/" exact component={() => <Home userName={userName}/>}/>
39
          <Route path="/setting"><Setting/></Route>
39
          
40
          <Route path="/setting">{userName.role === 'EMPLOYER' ? <Setting/>:
41
           <div className="permissionText column">
42
             <p>You don't have permission to access on this server.</p>
43
            </div>}</Route> 
44

  
40 45
          <Route path="/logout"><LogOut/></Route>
41 46
          <Route path="/login"><Login/></Route>
42 47
        </Switch>  
......
59 64
  function getDataUserRequest() {
60 65
    return ([]);
61 66
  }
62

  
63
  // const [data, setData] = useState([])
64
  // const [todo, setTodo] = useState([])
65

  
67
  
66 68
  return (
67 69
  <div className="container">
68 70
    <div className="main-content">
......
76 78

  
77 79
export default App;
78 80

  
79
  /* <Komponenta1 todo={todo} setTodo={setTodo}/>
80
  <Komponenta2 todo={todo} setTodo={setTodo}/> */
81

  
82

  
83
 
client/src/Calendar.js
16 16
    if (props.userName.id !== undefined) {
17 17
      props.userName.role === 'EMPLOYER'
18 18
        ?
19
          api_fetch.getAdminCalendar().then(adminCalendar => {
20
            props.setRequest(adminCalendar)
19
          api_fetch.getAdminCalendar().then(adminCalendar => { 
20
            props.setRequest(adminCalendar);
21
          }).catch(reason => {
22
            alert(reason)
21 23
          })
22 24
        :
23 25
          api_fetch.getUserCalendar(props.userName, todayTwo).then(userCalendar => {
24
            props.setRequest(userCalendar)
26
            props.setRequest(userCalendar);
27
          }).catch(reason => {
28
            alert(reason)
25 29
          });
26 30
    }
27 31
  }, [props.userName.id]);
28 32

  
29
  // LOAD DATA from server to calendar **** EMPLOYEE ****
30
  
31
  // LOAD DATA from server to calendar **** EMPLOYER ****
32

  
33
  //states
33
//states
34 34
  const [isOpen, setOpen] = useState(false)
35 35

  
36 36
  const [whatDate, setDate] = useState('')
......
41 41

  
42 42
  var today = new Date();
43 43

  
44
  // setting date to right format
44
// setting date to right format
45 45
  today = today.toISOString().split('T')[0]
46 46
  const todayTwo = today.split("-").join("/")
47 47

  
48
// LOAD DATA from server to calendar **** EMPLOYEE ****
49
  
50
// LOAD DATA from server to calendar **** EMPLOYER ****
51

  
48 52
// ********************* ADD EVENT - EMPLOYEE **************************
49 53

  
54
const addEvent = async (e) => {
55
  e.preventDefault();
50 56
  
57
  // setting an object
58
  const newDate = whatDate.split("-").join("/");
59

  
60
  const dataAddEventEmployee = {
61
    type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
62
    date: newDate,
63
    from: typeRadio === 'sickday' ? null : "00:00",
64
    to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
65
  }
66

  
67
  api_fetch.addEventApi(dataAddEventEmployee).catch(reason => {
68
    alert(reason)
69
  });
70
    
71
    setOpen(false)
72
  }
73
 
51 74
// ********************* ADD EVENT ADMIN - EMPLOYER **************************
52 75

  
53 76
const addEventAdmin = async (e) => {
......
56 79
// setting an object
57 80
  const newDate = whatDate.split("-").join("/");
58 81

  
59
  const peps = {
82
  const dataAddEventAdmin = {
60 83
    type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
61 84
    date: newDate,
62 85
    from: typeRadio === 'sickday' ? null : "00:00",
63 86
    to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
64 87
  };
65 88

  
66
  api_fetch.tvojefunkce(peps)
67
    
68
  try {
69
// send accepted request to server
70
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
71
      headers: {
72
        Authorization: 1,
73
        'Content-Type': 'application/json',
74
      },
75
      method: 'POST',
76
// object which is sent to server
77
      body: JSON.stringify(peps),
78
    });
79
    if (response.ok) {
80
  
81

  
82
  const userProps = {
83
    title: props.userName.name,
84
    start: whatDate
85
  
86
}
87
//concat new request to current ones
88
    props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
89
} else {
90
  if(response.status === 400) {
91
    alert('error 400 ADD EVENT ADMIN - EMPLOYER')
92
 }
93
    else if (response.status === 500) {
94
       alert ('error 500 ADD EVENT ADMIN - EMPLOYER')
95
    }
96
    else {
97
       alert('error ADD EVENT ADMIN - EMPLOYER')
98
    }
99
}
100
  } catch (e) {
101
    alert('error catch ADD EVENT ADMIN - EMPLOYER')
89
  api_fetch.addEventApiAdmin(dataAddEventAdmin).then(() => {
90
    const userProps = {
91
      title: props.userName.name, 
92
      start: whatDate      
102 93
  }
94
  //concat new request to current ones
95
      props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
96
  }).catch(reason => {
97
    alert(reason)
98
  });
103 99

  
104

  
105
  setOpen(false)}
100
  setOpen(false)
101
}
106 102
    
107 103

  
108 104
  return (
......
131 127
    <div className="calendar-form">
132 128
      {/* <form onSubmit={(e) => addEvent(e)}> */}
133 129
      <form onSubmit={props.userName.role === 'EMPLOYER' 
134
      ? (e) => addEventAdmin(e).then(eventAdmin => { props.setUser(eventAdmin)}) 
135
      : (e) => api_fetch.addEvent(e).then(userEvent => { props.setUser(userEvent)})
130
      ? (e) => addEventAdmin(e)
131
      : (e) => addEvent(e)
136 132
      }>
137 133
        <h2>Choose an option</h2>
138 134
        <div className="calendar-radio">
client/src/OverviewAdmin.js
1 1
import React, { useState, useEffect } from 'react';
2 2
import './App.css';
3
import * as api_fetch from './api'
3 4

  
4 5

  
5 6
const OverviewAdmin = () => {
6 7

  
7
   useEffect( () => {
8
      getData();
8
   useEffect(() => {
9
      api_fetch.getUsersOverview().then(usersOverview => {
10
         setEmployees(usersOverview);
11
         }).catch(reason => {
12
         alert(reason)
13
      });
9 14
   }, []);
10 15

  
11
   const getData = async () => {
12
      try {
13
      const response = await fetch (
14
         'http://devcz.yoso.fi:8090/ymanager/users', {
15
          headers: {
16
            Authorization: 1          }
17
        }
18
      );
19
      
20
   if (response.ok) {
21

  
22
      const data = await response.json();
23
      setEmployees(data.map(user => {
24

  
25
         return (
26
            {
27
               name: user.firstName + ' ' + user.lastName,
28
               id: user.id,
29
               sickday: user.sickDayCount,
30
               holiday: user.vacationCount,
31
               role: user.role
32
            })
33
      }))
34
   }  else {
35
         if(response.status === 400) {
36
            alert('error 400 GET DATA (OVERVIEW, EMPLOYER)')
37
         }
38
            else if (response.status === 500) {
39
               alert ('error 500 GET DATA (OVERVIEW, EMPLOYER)')
40
            }
41
            else {
42
               alert('error GET DATA (OVERVIEW, EMPLOYER)')
43
            }
44
   }
45
} catch (e) {
46
   console.log(e)
47
   alert('spatne')
48
   }
49
}
50
   
16
   // states
51 17
   const [employees, setEmployees] = useState([
52 18
      {
53 19
         name: 'Sadam',
......
59 25

  
60 26
   const [isEdit, setEdit] = useState(false);
61 27
   const [editedUserId, setEditedUserId] = useState();
62
   
63 28
   const [prevEdit, setPrevEdit] = useState();
29
   
64 30

  
31
   // functions
65 32
   function changeSickdays(newValue) {
66 33
      const newEmployees = employees.map(employee => {
67 34
         if (editedUserId === employee.id) {
......
91 58
   }
92 59

  
93 60
   const submitEdit = async (e) => {
94
      try {
61
      
95 62
      setEdit(isEdit === true ? false : true);
96 63
      setPrevEdit(employees);
97 64
      e.preventDefault();
98 65

  
99 66
      const found = employees.find(employee => editedUserId === employee.id);
100 67
      const foundPrevEdit = prevEdit.find(employee => editedUserId === employee.id);
101
      console.log(found)
102

  
103

  
104
   // send accepted request to server
105
       const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/settings', {
106
         headers: {
107
           Authorization: 1,
108
           'Content-Type': 'application/json',
109
         },
110
         method: 'PUT',
111

  
112
   // object which is sent to server
113
         body: JSON.stringify({
114
           id: found.id,
115
           vacationCount: Number(found.holiday) - foundPrevEdit.holiday,
116
           sickDayCount: Number(found.sickday),
117
           role: found.role
118
         }),        
119
       });
120
       console.log(response.status)
121
      if (response.status === 400) {
122
         alert('error 400 SAVE DATA (OVERVIEW, EMPLOYER)')
123
      }
124
         else if (response.status === 500) {
125
            alert('error 500 SAVE DATA (OVERVIEW, EMPLOYER)')
126
         }
127
            else if (!response.ok) {
128
               alert('error SAVE DATA (OVERVIEW, EMPLOYER)')
129
            }
130
                   
131
      } catch (e) {
132
         alert('error catch SAVE DATA (OVERVIEW, EMPLOYER')
68

  
69
      const dataOverviewObject = {
70
         id: found.id,
71
         vacationCount: Number(found.holiday) - foundPrevEdit.holiday,
72
         sickDayCount: Number(found.sickday),
73
         role: found.role
133 74
      }
134
   }
135 75

  
76
      api_fetch.saveDataOverview(dataOverviewObject).catch(reason => {
77
         alert(reason)
78
       });
79
   }
136 80

  
137 81
   const cancel = () => {
138 82
      setEmployees(prevEdit)
......
148 92
      
149 93
     e.preventDefault();
150 94
   }
151

  
152
   console.log(isEdit, editedUserId)
153 95
   
154 96
      return (
155 97
   <div>
client/src/Setting.js
1 1
import React, { useState, useEffect } from 'react';
2 2
import './App.css';
3 3
import { Link } from 'react-router-dom';
4
import * as api_fetch from './api'
4 5

  
5 6

  
6 7
function Setting() {
......
8 9
  const [sickdays, setSickdays] = useState([]);
9 10

  
10 11
  useEffect( () => {
11
    getData();
12
    api_fetch.getSettingData().then(settingData => {
13
      setSetting(settingData);
14
    }).catch(reason => {
15
      alert(reason)
16
    })
12 17
  }, []);
13 18

  
14
  const getData = async () =>  {
15
    try {
16
      const response = await fetch(
17
        'http://devcz.yoso.fi:8090/ymanager/settings', {
18
          headers: {
19
            Authorization: 1
20
          }
21
        }
22
        );
23

  
24
        if (response.ok) {
25
        const data = await response.json();
26
        setSetting({
27
          sickday: data.sickDayCount,
28
        })
29
      } else {
30
        if(response.status === 400) {
31
          alert('error 400')
32
       }
33
          else if (response.status === 500) {
34
             alert ('error 500')
35
          }
36
          else {
37
             alert('spatne neco jinyho')
38
          }
39
      }
40
    } catch (e) {
41
      console.log(e)
42
      alert('spatne vsechno')
43
      }
44
  }
45

  
46 19
  const submitSetting = async (e) => {
47 20
    e.preventDefault();
48
    try {
49
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/settings', {
50
      headers: {
51
        'Authorization': 1,
52
        'Content-Type': 'application/json'
53
      },
54
      method: 'POST',
55
      body: JSON.stringify({
56
        "sickDayCount": Number(setting.sickday),
57
        "notification": "2019/12/01 12:00:00"
58
      }),
59
    });
60

  
61

  
62
    if (response.ok) {
63
  } else {
64
    if(response.status === 400) {
65
      alert('error 400')
66
   }
67
      else if (response.status === 500) {
68
         alert ('error 500')
69
      }
70
      else {
71
         alert('spatne neco jinyho')
72
      }
73
  }
74
} catch (e) {
75
  console.log(e)
76
  alert('spatne vsechno')
77
  }
21
    
22
    const dataSettingObject = {
23
      "sickDayCount": Number(setting.sickday),
24
      "notification": "2019/12/01 12:00:00"  
25
    }
78 26

  
27
    api_fetch.saveDataSetting(dataSettingObject).catch(reason => {
28
      alert(reason)
29
    })
79 30
  }
80

  
81

  
82

  
31
  // states
83 32
  const [setting, setSetting] = useState(
84 33
    {sickday: 5,
85 34
    holiday: 0
86 35
    }
87 36
  ) 
88 37

  
38
  //functions
89 39
  function changeSickday(newValue) {
90 40
    setSetting(
91 41
       {sickday: newValue,
92 42
        holiday: setting.holiday
93
      }
94
    )
43
    })
95 44
  }
96 45

  
97 46
  function changeHoliday(newValue) {
98 47
    setSetting(
99 48
      {sickday: setting.sickday,
100 49
       holiday: newValue
101
      }
102
    )
50
    })
103 51
  }
104 52

  
105 53
  return (
client/src/UpcomingRequests.js
1 1
import React, { useEffect } from 'react';
2 2
import './App.css';
3

  
3
import * as api_fetch from './api'
4 4

  
5 5
function UpcomingRequests(props) {
6 6

  
......
10 10

  
11 11
  // get requests from server
12 12
  const getData = async () => {
13
    try {
14
    const response = await fetch(
15
      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
16
        headers: {
17
          Authorization: 1
18
        }
19
      },
20
    );
21

  
22
     if (response.ok) {
23
    const data = await response.json();
24
    props.setUser(data.map(request => {
13
   
14
    api_fetch.loadAdminRequests().then((data) => {
15
      props.setUser(data.map(request => {
25 16
      
26
      const a = request.date;
27
      const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
28

  
29
      return (
30
        {
31
          title: request.firstName + ' ' + request.lastName,
32
          id: request.id,
33
          type: request.type,
34
          start: b,
35
          end: null,
36
          status: request.status
37
      })
38
    }))
39
  } else {
40
    if(response.status === 400) {
41
      alert('error 400 GET DATA (UPCOMING REQUESTS)')
42
   }
43
      else if (response.status === 500) {
44
         alert ('error 500 GET DATA (UPCOMING REQUESTS)')
45
      }
46
      else {
47
         alert('error GET DATA (UPCOMING REQUESTS)')
48
      }
49
  }
50
} catch (e) {
51
  console.log(e)
52
  alert('error catch GET DATA (UPCOMING REQUESTS)')
53
  }    
17
        const a = request.date;
18
        const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
19
  
20
        return (
21
          {
22
            title: request.firstName + ' ' + request.lastName,
23
            id: request.id,
24
            type: request.type,
25
            start: b,
26
            end: null,
27
            status: request.status
28
        })
29
      }))
30
    }).catch(reason => {
31
      alert(reason)
32
    });
54 33
}
55 34

  
35

  
36

  
56 37
  // send accepted request to server
57 38
  const acceptRequest = async (user) => {
58
    try {
59
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/requests?type=VACATION', {
60
      headers: {
61
        Authorization: 1,
62
        'Content-Type': 'application/json',
63
      },
64
      method: 'PUT',
65
      body: JSON.stringify({
66
        id: user.id,
67
        status: 'ACCEPTED',
68
      }),
69
    });
70 39

  
71
    if (response.ok) {
72
    const userProps = {
73
      title: user.title,
74
      id: 0,
75
      type: user.type, 
76
      start: user.start
77
  }
78
  //concat new request to current ones
79
      props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
80
  //request accept button
81
      props.setUser((pendingRequest) => pendingRequest.filter((item) => item !== user));
82

  
83
    } else {
84
      if(response.status === 400) {
85
        alert('error 400 SEND ACCEPTED DATA (UPCOMING REQUESTS)')
86
     }
87
        else if (response.status === 500) {
88
           alert ('error 500 SEND ACCEPTED DATA (UPCOMING REQUESTS)')
89
        }
90
        else {
91
           alert('error SEND ACCEPTED DATA (UPCOMING REQUESTS)')
92
        }
93
    }
94
  } catch (e) {
95
    alert('error catch SEND ACCEPTED DATA (UPCOMING REQUESTS)')
40
    const acceptedRequests = {
41
      id: user.id,
42
      status: 'ACCEPTED',
96 43
    }
44
    
45
    api_fetch.sendAcceptedRequest(acceptedRequests).then((data) => {
46
      
47
    const userProps = {
48
          title: user.title,
49
          id: 0,
50
          type: user.type, 
51
          start: user.start
52
      }
53
      //concat new request to current ones
54
          props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
55
      //request accept button
56
          props.setUser((pendingRequest) => pendingRequest.filter((item) => item !== user));
57
    }).catch(response => {
58
      alert(response)
59
    })
97 60
  }
98 61

  
62
 
63

  
99 64
  //send rejected request to server
100 65
  const declineRequest = async (user) => {
101
    try {
102
      const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/requests?type=VACATION', {
103
        headers: {
104
          Authorization: 1,
105
          'Content-Type': 'application/json',
106
        },
107
        method: 'PUT',
108
        body: JSON.stringify({
109
          id: user.id,
110
          status: 'REJECTED',
111
        }),
112
      });
113

  
114
    if (response.ok) {    
115
  //request cancel button
116
      props.setUser((acceptedRequest) => acceptedRequest.filter((item) => item !== user))
117 66

  
118
    } else {
119
      if(response.status === 400) {
120
        alert('error 400 SEND REJECTED DATA (UPCOMING REQUESTS)')
121
     }
122
        else if (response.status === 500) {
123
           alert ('error 500 SEND REJECTED DATA (UPCOMING REQUESTS)')
124
        }
125
        else {
126
           alert('error SEND REJECTED DATA (UPCOMING REQUESTS)')
127
        }
128
    }
129
  } catch (e) {
130
    console.log(e)
131
    alert('error catch SEND REJECTED DATA (UPCOMING REQUESTS)')
67
    const rejectedRequest = {
68
      id: user.id,
69
      status: 'REJECTED',
132 70
    }
71

  
72
    api_fetch.sendRejectedRequest(rejectedRequest).then((data) => {
73
      //request cancel button
74
      props.setUser((acceptedRequest) => acceptedRequest.filter((item) => item !== user))
75
    }).catch(reason => {
76
      alert(reason)
77
    });
133 78
  }
134 79

  
135 80
  return (
client/src/YourRequests.js
1 1
import React, { useEffect } from 'react';
2 2
import './App.css';
3
import * as api_fetch from './api'
3 4

  
4 5
function YourRequests(props) {
5 6

  
......
9 10

  
10 11
  // get requests from server
11 12
  const getData = async () => {
12
    try {
13
      const response = await fetch(
14
        'http://devcz.yoso.fi:8090/ymanager/user/6/calendar?from=2020/06/24&status=PENDING', {
15
          headers: {
16
            Authorization: 6
17
          },
18
        }
19
      );
20 13

  
21
    if (response.ok) {
22
      const data = await response.json();
14
    api_fetch.loadYourRequests().then((data) => {
15

  
23 16
      props.setUser(data.map(request => {
24 17
        const a = request.date;
25 18
        const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
......
34 27
        }
35 28
      )
36 29
    }))
37
    } else {
38
      if(response.status === 400) {
39
        alert('error 400 GET DATA (YOUR REQUEST)')
40
     }
41
        else if (response.status === 500) {
42
           alert ('error 500 GET DATA (YOUR REQUEST)')
43
        }
44
        else {
45
           alert('error GET DATA (YOUR REQUEST)')
46
        }
47
    }
48
  } catch (e) {
49
    console.log(e)
50
    alert('error catch GET DATA (YOUR REQUEST)')
51
  }
30
    }).catch(reason => {
31
      alert(reason)
32
    });
52 33
}
53
        
34
  
54 35

  
55 36
  return (
56 37
    <div className="offs-request column">
......
67 48
                <th>Status</th>    
68 49
              </tr>
69 50
              {props.userRequest.map(user => (
70
              <tr>
51
              <tr key={user.id}>
71 52
                <td>{user.title}</td>
72 53
                <td>{user.type}</td>    
73 54
                <td>{user.end ? user.start + " - " + user.end : user.start}</td>
client/src/api.js
1
// ******************** GET DATA APP getCurrentProfile, [userName, setUserName] ********************
1
const http = 'http://devcz.yoso.fi:8090/ymanager';
2

  
3
// ******************** GET DATA APP getCurrentProfile ********************
2 4

  
3 5
export const getCurrentProfile = async () => {
4 6

  
5 7
    try {
6 8
    const response = await fetch(
7
      'http://devcz.yoso.fi:8090/ymanager/users/current/profile', {
9
      `${http}/users/current/profile`, {
8 10
        headers: {
9 11
          Authorization: 1
10 12
        }
......
22 24
    }
23 25
    } else {
24 26
        if(response.status === 400) {
25
        alert('error 400 GET DATA APP (getCurrentProfile)')
26
    }
27
          throw 'error 400 GET DATA APP (getCurrentProfile)'
28
        }
27 29
        else if (response.status === 500) {
28
            alert ('error 500 GET DATA APP (getCurrentProfile)')
30
          throw 'error 500 GET DATA APP (getCurrentProfile)'
29 31
        }
30 32
        else {
31
            alert('error GET DATA APP (getCurrentProfile)')
33
          throw 'error GET DATA APP (getCurrentProfile)'
32 34
        }
33 35
    }
34 36

  
35 37
} catch (e) {
36
  alert('error catch GET DATA APP (getCurrentProfile)')
38
  throw 'error catch GET DATA APP (getCurrentProfile)'
37 39
  }
38 40
}
39 41

  
......
41 43
export const getUserCalendar = async (userName, fromDate ) => {
42 44
  try {
43 45
  const response = await fetch(
44
    `http://devcz.yoso.fi:8090/ymanager/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
46
    `${http}/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
45 47
      headers: {
46 48
        'Accept': 'application/json',
47 49
        Authorization: 6
......
66 68
    })
67 69
  })
68 70
} else {
69
  if(response.status === 400) {
70
    alert('error 400 LOADING DATA (CALENDAR, EMPLOYEE)')
71
 }
71
    if(response.status === 400) {
72
      throw 'error 400 LOADING DATA (CALENDAR, EMPLOYEE)'
73
    }
72 74
    else if (response.status === 500) {
73
       alert ('error 500 LOADING DATA (CALENDAR, EMPLOYEE)')
75
      throw 'error 500 LOADING DATA (CALENDAR, EMPLOYEE)'
74 76
    }
75 77
    else {
76
       alert('error LOADING DATA (CALENDAR, EMPLOYEE)')
78
      throw 'error LOADING DATA (CALENDAR, EMPLOYEE)'
77 79
    }
78 80
}
79 81
} catch (e) {
80
  alert('error catch LOADING DATA (CALENDAR, EMPLOYEE)')
82
  throw 'error catch LOADING DATA (CALENDAR, EMPLOYEE)'
81 83
}
82 84
}
83 85

  
......
107 109
      })
108 110
    })
109 111
  } else {
110
    if(response.status === 400) {
111
      alert('error 400 LOADING DATA (CALENDAR, EMPLOYER)')
112
   }
112
      if(response.status === 400) {
113
        throw 'error 400 LOADING DATA (CALENDAR, EMPLOYER)'
114
      }
113 115
      else if (response.status === 500) {
114
         alert ('error 500 LOADING DATA (CALENDAR, EMPLOYER))')
116
        throw 'error 500 LOADING DATA (CALENDAR, EMPLOYER))'
115 117
      }
116 118
      else {
117
         alert('error LOADING DATA (CALENDAR, EMPLOYER)')
119
        throw 'error LOADING DATA (CALENDAR, EMPLOYER)'
118 120
      }
119
  }
121
    }
120 122
  } catch (e) {
121
    alert('error catch LOADING DATA (CALENDAR, EMPLOYER)')
123
    throw 'error catch LOADING DATA (CALENDAR, EMPLOYER)'
122 124
  }
123 125
}
124 126

  
125 127
// ******************** ADD EVENT to CALENDAR - EMPLOYEE ********************
126
export const addEvent = async (e) => {
127
    e.preventDefault();
128

  
129
  // setting an object
130
    const newDate = whatDate.split("-").join("/");
131
      
132
    try {
128
export async function addEventApi(dataAddEventEmployee) {
129
try {
133 130
  // send accepted request to server
134
      const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
131
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
132
      headers: {
133
        Authorization: 6,
134
        'Content-Type': 'application/json',
135
      },
136
      method: 'POST',
137
  // object which is sent to server
138
  body: JSON.stringify(dataAddEventEmployee),
139
    });
140
    if (response.ok) {
141
      
142
      const response = await fetch(
143
      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
135 144
        headers: {
136
          Authorization: 6,
137
          'Content-Type': 'application/json',
145
          Authorization: 1
138 146
        },
139
        method: 'POST',
140
  // object which is sent to server
141
        body: JSON.stringify({
142
          type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
143
          date: newDate,
144
          from: typeRadio === 'sickday' ? null : "00:00",
145
          to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
146
        }),
147 147
      });
148
      if (response.ok) {
148
      const data = await response.json();
149
      
150
    return data.map(request => {
151
      const a = request.date;
152
      const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
149 153

  
150
      const response = await fetch(
151
        'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
154
      return (
155
        {
156
          title: request.firstName + ' ' + request.lastName,
157
          id: request.id,
158
          type: request.type,
159
          start: b,
160
          end: null,
161
          status: request.status
162
        })
163
    })
164

  
165
  } else {
166
    
167
    if(response.status === 400) {
168
      throw 'error 400 ADD EVENT - EMPLOYEE'
169
    }
170
    else if (response.status === 500) {
171
      throw 'error 500 ADD EVENT - EMPLOYEE'
172
    }
173
    else {
174
      throw 'error ADD EVENT - EMPLOYEE'
175
    }
176
  }
177
} catch (e) {
178
    throw 'error catch ADD EVENT - EMPLOYEE'
179
  }
180
}
181
      
182
// ******************** ADD EVENT to CALENDAR - EMPLOYER ********************
183
export async function addEventApiAdmin(dataAddEventAdmin) {
184
  try {
185
    // send accepted request to server
186
        const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
152 187
          headers: {
153
            Authorization: 1
188
            Authorization: 1,
189
            'Content-Type': 'application/json',
154 190
          },
191
          method: 'POST',
192
    // object which is sent to server
193
          body: JSON.stringify(dataAddEventAdmin),
194
        });
195
        if (response.ok) {
196
          return;
197
    } else {
198
      if(response.status === 400) {
199
        throw('error 400 ADD EVENT ADMIN - EMPLOYER')
200
     }
201
        else if (response.status === 500) {
202
           throw ('error 500 ADD EVENT ADMIN - EMPLOYER')
155 203
        }
156
  
157
       );
158
      const data = await response.json();
204
        else {
205
           throw('error ADD EVENT ADMIN - EMPLOYER')
206
        }
207
    }
208
  } catch (e) {
209
      throw('error catch ADD EVENT ADMIN - EMPLOYER')
210
    }
211
}
159 212

  
160
      return data.map(request => {
161
        const a = request.date;
162
        const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
213
// ******************** GET DATA to OVERVIEW - EMPLOYER ********************
214
export const getUsersOverview = async () => {
215
  try {
216
  const response = await fetch (
217
     'http://devcz.yoso.fi:8090/ymanager/users', {
218
      headers: {
219
        Authorization: 1          }
220
    }
221
  );
163 222
  
164
        return (
165
          {
166
            title: request.firstName + ' ' + request.lastName,
167
            id: request.id,
168
            type: request.type,
169
            start: b,
170
            end: null,
171
            status: request.status
223
if (response.ok) {
224

  
225
  const data = await response.json();
226
  return data.map(user => {
227

  
228
     return (
229
        {
230
           name: user.firstName + ' ' + user.lastName,
231
           id: user.id,
232
           sickday: user.sickDayCount,
233
           holiday: user.vacationCount,
234
           role: user.role
172 235
        })
173
      })
236
  })
237
}  else {
238
      if(response.status === 400) {
239
        throw 'error 400 GET DATA (OVERVIEW, EMPLOYER)'
240
      }
241
      else if (response.status === 500) {
242
        throw 'error 500 GET DATA (OVERVIEW, EMPLOYER)'
243
      }
244
      else {
245
        throw 'error GET DATA (OVERVIEW, EMPLOYER)'
246
        }
247
      }
248
  } catch (e) {
249
      throw 'error catch GET DATA (OVERVIEW, EMPLOYER)'
250
  }
251
}
174 252

  
175
  } else {
253
// ******************** SAVE DATA to OVERVIEW - EMPLOYER ********************
254
export async function saveDataOverview(dataOverviewObject) {
255
  try {
256
    // send accepted request to server
257
        const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/settings', {
258
          headers: {
259
            Authorization: 1,
260
            'Content-Type': 'application/json',
261
          },
262
          method: 'PUT',
263
 
264
    // object which is sent to server
265
          body: JSON.stringify(dataOverviewObject),        
266
        });
267
        console.log(response.status)
268
       if (response.status === 400) {
269
        throw 'error 400 SAVE DATA (OVERVIEW, EMPLOYER)'
270
          }
271
      else if (response.status === 500) {
272
        throw 'error 500 SAVE DATA (OVERVIEW, EMPLOYER)'
273
      }
274
      else if (!response.ok) {
275
        throw 'error SAVE DATA (OVERVIEW, EMPLOYER)'
276
      }
277

  
278
  } catch (e) {
279
    throw 'error catch SAVE DATA (OVERVIEW, EMPLOYER'
280
  }
281
}
282

  
283
// ******************** LOAD DATA to SETTING - EMPLOYER ********************
284
export const getSettingData = async () =>  {
285
  try {
286
    const response = await fetch(
287
      'http://devcz.yoso.fi:8090/ymanager/settings', {
288
        headers: {
289
          Authorization: 1
290
        }
291
      });
292

  
293
      if (response.ok) {
294
      const data = await response.json();
295
      return {
296
        sickday: data.sickDayCount,
297
      }
298
    } else {
299
        if(response.status === 400) {
300
          throw 'error 400 LOADING DATA (SETTING, EMPLOYER)'
301
        }
302
        else if (response.status === 500) {
303
           throw 'error 500 LOADING DATA (SETTING, EMPLOYER)'
304
        }
305
        else {
306
           throw 'error LOADING DATA (SETTING, EMPLOYER)'
307
        }
308
      }
309
  } catch (e) {
310
    throw 'error catch LOADING DATA (SETTING, EMPLOYER)'
311
    }
312
}
313

  
314
// ******************** SAVE DATA to SETTING - EMPLOYER ********************
315
export async function saveDataSetting(dataSettingObject) {
316
  try {
317
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/settings', {
318
      headers: {
319
        'Authorization': 6,
320
        'Content-Type': 'application/json'
321
      },
322
      method: 'POST',
323
      body: JSON.stringify(dataSettingObject),
324
    });
325

  
326
    switch (response.status) {
327
      case 200:
328
        throw '...'
329
      case 500:
330
        throw ''
331
      default:
332
        throw response.statusText
333

  
334
    }
176 335

  
177 336
    if(response.status === 400) {
178
    alert('error 400 ADD EVENT - EMPLOYEE')
179
 }
337
      throw 'error 400 SAVE DATA (OVERVIEW, EMPLOYER)'
338
    }
180 339
    else if (response.status === 500) {
181
       alert ('error 500 ADD EVENT - EMPLOYEE')
340
      throw 'error 500 SAVE DATA (OVERVIEW, EMPLOYER)'
182 341
    }
183
    else {
184
       alert('error ADD EVENT - EMPLOYEE')
342
    else if (!response.ok) {
343
      throw 'error SAVE DATA (OVERVIEW, EMPLOYER)'
344
      }
345
  } catch (e) {
346
      throw 'error catch SAVE DATA (OVERVIEW, EMPLOYER)'
185 347
    }
186
    
348
}
349

  
350
// ****************** LOAD DATA to YOUR REQUESTS - EMPLOYEE ******************
351

  
352
export async function loadYourRequests() {
353
  try {
354
    const response = await fetch(
355
      'http://devcz.yoso.fi:8090/ymanager/user/6/calendar?from=2020/06/24&status=PENDING', {
356
        headers: {
357
          Authorization: 6
358
        },
359
      }
360
    );
361

  
362
  if (response.ok) {
363
    const data = await response.json();
364
    return data;
365
  } else {
366
    if(response.status === 400) {
367
      alert('error 400 GET DATA (YOUR REQUEST)')
368
   }
369
      else if (response.status === 500) {
370
         alert ('error 500 GET DATA (YOUR REQUEST)')
371
      }
372
      else {
373
         alert('error GET DATA (YOUR REQUEST)')
374
      }
375
  }
376
} catch (e) {
377
  console.log(e)
378
  alert('error catch GET DATA (YOUR REQUEST)')
187 379
  }
188
    } catch (e) {
189
      alert('error catch ADD EVENT - EMPLOYEE')
380
}
381

  
382
// ****************** LOAD DATA - UPCOMING REQUESTS - EMPLOYER ******************
383
export async function loadAdminRequests() {
384
  try {
385
    const response = await fetch(
386
      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
387
        headers: {
388
          Authorization: 1
389
        }
390
      },
391
    );
392

  
393
     if (response.ok) {
394
    const data = await response.json();
395
      return data;
396
  } else {
397
    if(response.status === 400) {
398
      alert('error 400 GET DATA (UPCOMING REQUESTS)')
399
   }
400
      else if (response.status === 500) {
401
         alert ('error 500 GET DATA (UPCOMING REQUESTS)')
402
      }
403
      else {
404
         alert('error GET DATA (UPCOMING REQUESTS)')
405
      }
190 406
    }
407
} catch (e) {
408
  alert('error catch GET DATA (UPCOMING REQUESTS)')
409
  } 
410
}
411

  
412
// ************** SEND ACCEPTED DATA - UPCOMING REQUESTS - EMPLOYER **************
413
export async function sendAcceptedRequest(acceptedRequests) {
414
  try {
415
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/requests?type=VACATION', {
416
      headers: {
417
        Authorization: 1,
418
        'Content-Type': 'application/json',
419
      },
420
      method: 'PUT',
421
      body: JSON.stringify(acceptedRequests),
422
    });
423

  
424
    if (response.ok) {
425
    return;
426

  
427
    } else {
428
      if(response.status === 400) {
429
        alert('error 400 SEND ACCEPTED DATA (UPCOMING REQUESTS)')
430
     }
431
        else if (response.status === 500) {
432
           alert ('error 500 SEND ACCEPTED DATA (UPCOMING REQUESTS)')
433
        }
434
        else {
435
           alert('error SEND ACCEPTED DATA (UPCOMING REQUESTS)')
436
        }
437
    }
438
  } catch (e) {
439
    alert('error catch SEND ACCEPTED DATA (UPCOMING REQUESTS)')
440
    }
441
}
442

  
443
// ************** SEND REJECTED DATA - UPCOMING REQUESTS - EMPLOYER **************
444

  
445
export async function sendRejectedRequest(rejectedRequest) {
446
  try {
447
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/requests?type=VACATION', {
448
      headers: {
449
        Authorization: 1,
450
        'Content-Type': 'application/json',
451
      },
452
      method: 'PUT',
453
      body: JSON.stringify(rejectedRequest),
454
    });
455

  
456
  if (response.ok) {    
457
    return;
458

  
459
  } else {
460
    if(response.status === 400) {
461
      alert('error 400 SEND REJECTED DATA (UPCOMING REQUESTS)')
462
   }
463
      else if (response.status === 500) {
464
         alert ('error 500 SEND REJECTED DATA (UPCOMING REQUESTS)')
465
      }
466
      else {
467
         alert('error SEND REJECTED DATA (UPCOMING REQUESTS)')
468
      }
469
  }
470
} catch (e) {
471
  alert('error catch SEND REJECTED DATA (UPCOMING REQUESTS)')
472
  }
473
}
474

  
191 475

  
192
    setOpen(false)}
193
    
194
//

Také k dispozici: Unified diff