-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor search && sorting reducers && actions using redux toolkit #3124
Open
PiyushChandra17
wants to merge
5
commits into
processing:develop
Choose a base branch
from
PiyushChandra17:piyush/refactor-search-sorting-reducers-actions-rtk
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+47
−60
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
331889e
refactor search && sorting reducers && actions using redux toolkit
PiyushChandra17 64a4bbb
fix few stuff
PiyushChandra17 8f4e8fd
refactor few things
PiyushChandra17 680c242
fix
PiyushChandra17 45968e3
added prepare feature to accept two args
PiyushChandra17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,39 +1,18 @@ | ||
import * as ActionTypes from '../../../constants'; | ||
import { setSorting } from '../reducers/sorting'; | ||
import { setSearchTerm } from '../reducers/search'; | ||
|
||
export { setSearchTerm } from '../reducers/search'; | ||
export { toggleDirectionForField } from '../reducers/sorting'; | ||
|
||
export const DIRECTION = { | ||
ASC: 'ASCENDING', | ||
DESC: 'DESCENDING' | ||
}; | ||
|
||
export function setSorting(field, direction) { | ||
return { | ||
type: ActionTypes.SET_SORTING, | ||
payload: { | ||
field, | ||
direction | ||
} | ||
}; | ||
} | ||
|
||
export function resetSorting() { | ||
return setSorting('createdAt', DIRECTION.DESC); | ||
} | ||
|
||
export function toggleDirectionForField(field) { | ||
return { | ||
type: ActionTypes.TOGGLE_DIRECTION, | ||
field | ||
}; | ||
} | ||
|
||
export function setSearchTerm(scope, searchTerm) { | ||
return { | ||
type: ActionTypes.SET_SEARCH_TERM, | ||
query: searchTerm, | ||
scope | ||
}; | ||
} | ||
|
||
export function resetSearchTerm(scope) { | ||
return setSearchTerm(scope, ''); | ||
} |
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,15 +1,24 @@ | ||
import * as ActionTypes from '../../../constants'; | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
|
||
const initialState = { | ||
collectionSearchTerm: '', | ||
sketchSearchTerm: '' | ||
}; | ||
|
||
export default (state = initialState, action) => { | ||
switch (action.type) { | ||
case ActionTypes.SET_SEARCH_TERM: | ||
return { ...state, [`${action.scope}SearchTerm`]: action.query }; | ||
default: | ||
return state; | ||
const searchSlice = createSlice({ | ||
name: 'search', | ||
initialState, | ||
reducers: { | ||
setSearchTerm: (state, action) => { | ||
const { scope, query } = action.payload; | ||
return { | ||
...state, | ||
[`${scope}SearchTerm`]: query | ||
}; | ||
} | ||
} | ||
}; | ||
}); | ||
|
||
export const { setSearchTerm } = searchSlice.actions; | ||
|
||
export default searchSlice.reducer; |
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,33 @@ | ||
import * as ActionTypes from '../../../constants'; | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
import { DIRECTION } from '../actions/sorting'; | ||
|
||
const initialState = { | ||
field: 'createdAt', | ||
direction: DIRECTION.DESC | ||
}; | ||
|
||
const sorting = (state = initialState, action) => { | ||
switch (action.type) { | ||
case ActionTypes.TOGGLE_DIRECTION: | ||
if (action.field && action.field !== state.field) { | ||
if (action.field === 'name') { | ||
return { ...state, field: action.field, direction: DIRECTION.ASC }; | ||
} | ||
return { ...state, field: action.field, direction: DIRECTION.DESC }; | ||
const sortingSlice = createSlice({ | ||
name: 'sorting', | ||
initialState, | ||
reducers: { | ||
toggleDirectionForField: (state, action) => { | ||
const { field } = action.payload; | ||
if (field && field !== state.field) { | ||
const direction = field === 'name' ? DIRECTION.ASC : DIRECTION.DESC; | ||
return { ...state, field, direction }; | ||
} | ||
if (state.direction === DIRECTION.ASC) { | ||
return { ...state, direction: DIRECTION.DESC }; | ||
} | ||
return { ...state, direction: DIRECTION.ASC }; | ||
case ActionTypes.SET_SORTING: | ||
return { | ||
...state, | ||
field: action.payload.field, | ||
direction: action.payload.direction | ||
}; | ||
default: | ||
return state; | ||
const direction = | ||
state.direction === DIRECTION.ASC ? DIRECTION.DESC : DIRECTION.ASC; | ||
return { ...state, direction }; | ||
}, | ||
setSorting: (state, action) => { | ||
const { field, direction } = action.payload; | ||
console.log(field); | ||
return { ...state, field, direction }; | ||
} | ||
} | ||
}; | ||
}); | ||
|
||
export const { toggleDirectionForField, setSorting } = sortingSlice.actions; | ||
|
||
export default sorting; | ||
export default sortingSlice.reducer; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to do
...state
stuff when using Redux Toolkit. The reducer uses a proxy of the actual state so it is okay to mutate it. You canreturn
a new state to replace the entire state, but typically you just modify the state and don'treturn
anything. It can be written as: