source: sources/client/src/hooks/useCreateEmployee.js

Last change on this file was bc20307, checked in by Tasevski2 <39170279+Tasevski2@…>, 2 years ago

Push before video

  • Property mode set to 100644
File size: 1.3 KB
Line 
1import { useState, useContext } from 'react';
2import axios from 'axios';
3import { AccessoriesContext } from '../context/AccessoriesContext';
4import { useHistory } from 'react-router-dom';
5
6const useCreateEmployee = () => {
7 const { setAlert, setIsBackdropLoaderOpen } =
8 useContext(AccessoriesContext);
9 const history = useHistory();
10 const createEmployee = async ({ employee }) => {
11 setIsBackdropLoaderOpen(true);
12 console.log(employee);
13 await axios
14 .post(`/vraboten`, employee) // TODO CHANGE OBJECT
15 .then((res) => {
16 setAlert({
17 type: 'success',
18 msg: `Вработениот ${employee.firstName} ${employee.lastName} е успешно креирана!`,
19 });
20 history.push('/employees');
21 })
22 .catch((err) => {
23 // ALERT FOR ERROR WITH ERROR MSG
24 setAlert({
25 type: 'error',
26 msg: 'Проблеми со серверот!', // TODO change msg to err.message
27 });
28 })
29 .finally(() => {
30 setIsBackdropLoaderOpen(false);
31 });
32 };
33
34 return {
35 createEmployee,
36 };
37};
38
39export default useCreateEmployee;
Note: See TracBrowser for help on using the repository browser.