generated from TkachenkoKaterina/goit-react-hw-06-phonebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made backend, add operations.js, made createSlice, add store.js, add …
…selectors.js
- Loading branch information
1 parent
b63a2ff
commit 0b2062d
Showing
6 changed files
with
164 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,63 @@ | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
import { addContact, deleteContact, fetchContacts } from './operations'; | ||
|
||
const initialState = { | ||
contacts: [ | ||
{ id: 'id-1', name: 'Rosie Simpson', number: '459-12-56' }, | ||
{ id: 'id-2', name: 'Hermione Kline', number: '443-89-12' }, | ||
{ id: 'id-3', name: 'Eden Clements', number: '645-17-79' }, | ||
{ id: 'id-4', name: 'Annie Copeland', number: '227-91-26' }, | ||
], | ||
contacts: { | ||
items: [], | ||
isLoading: false, | ||
error: null, | ||
}, | ||
filter: '', | ||
}; | ||
|
||
const handlePending = state => { | ||
state.contacts.isLoading = true; | ||
}; | ||
|
||
const handleRejected = (state, action) => { | ||
state.contacts.isLoading = false; | ||
state.contacts.error = action.payload; | ||
}; | ||
|
||
export const contactsSlice = createSlice({ | ||
name: 'contacts', | ||
initialState, | ||
reducers: { | ||
addContact: (state, { payload }) => { | ||
state.contacts.contacts.push(payload); | ||
}, | ||
deleteContact: (state, { payload }) => { | ||
state.contacts.contacts = state.contacts.contacts.filter( | ||
contact => contact.id !== payload | ||
); | ||
}, | ||
updateFilter: (state, { payload }) => { | ||
state.filter = payload; | ||
}, | ||
extraReducers: builder => { | ||
builder | ||
// fetchContacts | ||
.addCase(fetchContacts.pending, handlePending) | ||
.addCase(fetchContacts.fulfilled, (state, action) => { | ||
state.contacts.isLoading = false; | ||
state.contacts.error = null; | ||
state.contacts.items = action.payload; | ||
}) | ||
.addCase(fetchContacts.rejected, handleRejected) | ||
// addContact | ||
.addCase(addContact.pending, handlePending) | ||
.addCase(addContact.fulfilled, (state, action) => { | ||
state.contacts.isLoading = false; | ||
state.contacts.error = null; | ||
state.contacts.items.push(action.payload); | ||
}) | ||
.addCase(addContact.rejected, handleRejected) | ||
// deleteContact | ||
.addCase(deleteContact.pending, handlePending) | ||
.addCase(deleteContact.fulfilled, (state, action) => { | ||
state.contacts.isLoading = false; | ||
state.contacts.error = null; | ||
state.contacts.items = state.contacts.items.filter( | ||
contact => contact.id !== action.payload | ||
); | ||
}) | ||
.addCase(deleteContact.rejected, handleRejected); | ||
}, | ||
}); | ||
|
||
export const { addContact, deleteContact, updateFilter } = | ||
contactsSlice.actions; | ||
export const contactsReducer = contactsSlice.reducer; | ||
|
||
// [ | ||
// { id: 'id-1', name: 'Rosie Simpson', number: '459-12-56' }, | ||
// { id: 'id-2', name: 'Hermione Kline', number: '443-89-12' }, | ||
// { id: 'id-3', name: 'Eden Clements', number: '645-17-79' }, | ||
// { id: 'id-4', name: 'Annie Copeland', number: '227-91-26' }, | ||
// ]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit'; | ||
import axios from 'axios'; | ||
|
||
axios.defaults.baseURL = 'https://65f1db4f034bdbecc763dfe4.mockapi.io'; | ||
|
||
export const fetchContacts = createAsyncThunk( | ||
'contacts/fetchAll', | ||
async (_, thunkAPI) => { | ||
try { | ||
const response = await axios.get('/contacts'); | ||
console.log('response :>> ', response); | ||
return response.data; | ||
} catch (error) { | ||
return thunkAPI.rejectWithValue(error.message); | ||
} | ||
} | ||
); | ||
|
||
export const addContact = createAsyncThunk( | ||
'contacts/addContact', | ||
async (contact, thunkAPI) => { | ||
try { | ||
const response = await axios.post('/contacts', { contact }); | ||
console.log('response :>> ', response); | ||
return response.data; | ||
} catch (error) { | ||
return thunkAPI.rejectWithValue(error.message); | ||
} | ||
} | ||
); | ||
|
||
export const deleteContact = createAsyncThunk( | ||
'contacts/deleteContact', | ||
async (contactId, thunkAPI) => { | ||
try { | ||
const response = await axios.delete(`/contacts/${contactId}`); | ||
console.log('response :>> ', response); | ||
return response.data; | ||
} catch (error) { | ||
return thunkAPI.rejectWithValue(error.message); | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
export const getContacts = state => state.contacts.contacts.contacts; | ||
export const getFilter = state => state.contacts.filter; | ||
export const selectContacts = state => state.contacts.contacts.items; | ||
|
||
export const selectIsLoading = state => state.contacts.contacts.isLoading; | ||
|
||
export const selectError = state => state.contacts.contacts.error; | ||
|
||
export const selectFilter = state => state.contacts.filter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,6 @@ | ||
import { configureStore } from '@reduxjs/toolkit'; | ||
import { persistStore, persistReducer } from 'redux-persist'; | ||
import storage from 'redux-persist/lib/storage'; | ||
import { contactsReducer } from './contactsSlice'; | ||
|
||
const persistConfig = { | ||
key: 'root', | ||
storage, | ||
blacklist: ['filter'], | ||
}; | ||
|
||
const persistedReducer = persistReducer(persistConfig, contactsReducer); | ||
|
||
export const store = configureStore({ | ||
reducer: { contacts: persistedReducer }, | ||
reducer: { contacts: contactsReducer }, | ||
}); | ||
|
||
export const persistor = persistStore(store); |