-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.ts
102 lines (86 loc) · 2.06 KB
/
actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {
FETCH_CONTACTS_STARTED,
FETCH_CONTACTS_SUCCESS,
FETCH_CONTACTS_FAILURE,
UPDATE_CONTACT_SUCCESS,
UPDATE_CONTACT_STARTED,
UPDATE_CONTACT_FAILURE,
DELETE_CONTACT_SUCCESS,
DELETE_CONTACT_STARTED,
DELETE_CONTACT_FAILURE,
} from "./types";
import axios from "axios";
export const getContacts = () => {
return (dispatch) => {
dispatch(getContactsStarted());
axios
.get("/api/contacts")
.then((res) => {
dispatch(getContactsSuccess(res.data));
})
.catch((err) => {
dispatch(getContactsFailure(err.message));
});
};
};
const getContactsSuccess = (contacts) => ({
type: FETCH_CONTACTS_SUCCESS,
payload: contacts,
});
const getContactsStarted = () => ({
type: FETCH_CONTACTS_STARTED,
});
const getContactsFailure = (error) => ({
type: FETCH_CONTACTS_FAILURE,
payload: error,
});
export const updateContact = (contact) => {
return (dispatch) => {
dispatch(updateContactStarted());
axios
.put(`/api/contacts?id=${contact.id}`, contact)
.then((res) => {
dispatch(updateContactSuccess(res.data));
})
.catch((err) => {
dispatch(updateContactFailure(err.message));
});
};
};
const updateContactSuccess = (contact) => ({
type: UPDATE_CONTACT_SUCCESS,
payload: contact,
});
const updateContactStarted = () => ({
type: UPDATE_CONTACT_STARTED,
});
const updateContactFailure = (error) => ({
type: UPDATE_CONTACT_FAILURE,
payload: error,
});
export const deleteContact = (id) => {
return (dispatch) => {
dispatch(deleteContactStarted());
axios
.delete(`/api/contacts?id=${id}`)
.then(() => {
dispatch(deleteContactSuccess(id));
})
.catch((err) => {
dispatch(deleteContactFailure(err.message));
});
};
};
const deleteContactSuccess = (contactId) => ({
type: DELETE_CONTACT_SUCCESS,
payload: {
id: contactId,
},
});
const deleteContactStarted = () => ({
type: DELETE_CONTACT_STARTED,
});
const deleteContactFailure = (error) => ({
type: DELETE_CONTACT_FAILURE,
payload: error,
});