-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace useContext and useReducer with redux
- Loading branch information
Showing
13 changed files
with
139 additions
and
81 deletions.
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
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
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
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
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,20 +1,28 @@ | ||
import { RootState } from '@/options/store'; | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
|
||
const DEFAULT_GROUP = { name: '', id: '-1' }; | ||
const DEFAULT_TAG = { id: '-1', name: '' }; | ||
|
||
export const selectedSlice = createSlice({ | ||
name: 'selected', | ||
initialState: { | ||
name: '', | ||
id: '-1', | ||
group: DEFAULT_GROUP, | ||
tag: DEFAULT_TAG, | ||
}, | ||
reducers: { | ||
selectGroup: (state, action) => { | ||
// https://github.com/reduxjs/redux-toolkit/issues/521#issuecomment-624796711 | ||
return action.payload; | ||
state.group = action.payload.group; | ||
state.tag = DEFAULT_TAG; | ||
}, | ||
selectTag: (state, action) => { | ||
state.group = DEFAULT_GROUP; | ||
state.tag = action.payload.tag; | ||
}, | ||
}, | ||
}); | ||
|
||
export const { selectGroup } = selectedSlice.actions; | ||
export const selectorGroup = (state: RootState) => state.selectedGroup; | ||
export const { selectGroup, selectTag } = selectedSlice.actions; | ||
export const selectorItem = (state: RootState) => state.selectedGroup; | ||
export default selectedSlice.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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { getStarsListByGroup, getStarsListByTag } from '@/content_script/services/local/stars'; | ||
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'; | ||
|
||
export const fetchStarsByGroup = createAsyncThunk('stars/fetchStarsByGroup', async (groupId: string) => { | ||
const stars = await getStarsListByGroup(groupId); | ||
return stars; | ||
}); | ||
|
||
export const fetchStarsByTag = createAsyncThunk('stars/fetchStarsByTag', async (tagId: string) => { | ||
const stars = await getStarsListByTag(tagId); | ||
return stars; | ||
}); | ||
|
||
export const starsSlice = createSlice({ | ||
name: 'stars', | ||
initialState: { | ||
loading: false, | ||
data: [], | ||
}, | ||
reducers: {}, | ||
extraReducers: (builder) => { | ||
// TODO ugly | ||
builder | ||
.addCase(fetchStarsByGroup.pending, (state) => { | ||
state.loading = true; | ||
}) | ||
.addCase(fetchStarsByGroup.fulfilled, (state, action) => { | ||
state.data = action.payload; | ||
state.loading = false; | ||
}) | ||
.addCase(fetchStarsByGroup.rejected, (state) => { | ||
state.loading = false; | ||
}) | ||
.addCase(fetchStarsByTag.pending, (state) => { | ||
state.loading = true; | ||
}) | ||
.addCase(fetchStarsByTag.fulfilled, (state, action) => { | ||
state.data = action.payload; | ||
state.loading = false; | ||
}) | ||
.addCase(fetchStarsByTag.rejected, (state) => { | ||
state.loading = false; | ||
}); | ||
}, | ||
}); | ||
|
||
export default starsSlice.reducer; |
Oops, something went wrong.