-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
118 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const StageTypes = { | ||
user: 'user', | ||
repository: 'repository', | ||
show: 'show', | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { StageTypes } from "@/models/StageTypes"; | ||
import { createSlice } from "@/redux/utils"; | ||
|
||
const initialState = { | ||
step: StageTypes.user, | ||
view: StageTypes.repository, | ||
bodyOpen: false, | ||
}; | ||
|
||
export default createSlice({ | ||
name: 'ui', | ||
initialState, | ||
reducers: { | ||
change: (state, { payload }) => { | ||
return { | ||
...state, | ||
...payload, | ||
}; | ||
}, | ||
}, | ||
}); |
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 @@ | ||
export * from './useUIProperty'; |
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,31 @@ | ||
import { useCallback, useRef } from "react"; | ||
import slice from '@/redux/modules/ui'; | ||
import { functionalUpdate } from "@/shared/utils/functionalUpdate"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
|
||
/** | ||
* @param {String} property | ||
* @param {*} [defaultValue] | ||
* @return {[*, (function(*): void)]} | ||
*/ | ||
export const useUIProperty = (property, defaultValue) => { | ||
const dispatch = useDispatch(); | ||
const prop = useRef(null); | ||
|
||
prop.current = property || null; | ||
const { [property]: value = defaultValue } = useSelector(slice.selectors.getState); | ||
|
||
const prev = useRef(null); | ||
prev.current = value; | ||
|
||
const change = useCallback( | ||
(newValue) => { | ||
dispatch(slice.actions.change({ | ||
[prop.current]: functionalUpdate(newValue, prev.current), | ||
})); | ||
}, | ||
[dispatch], | ||
); | ||
|
||
return [value, change]; | ||
}; |
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,8 @@ | ||
/** | ||
* @param {*|function(prevValue): newValue} updater | ||
* @param {*} prev | ||
* @return {*} | ||
*/ | ||
export const functionalUpdate = (updater, prev) => { | ||
return typeof updater === 'function' ? updater(prev) : updater; | ||
}; |
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 +1,2 @@ | ||
export * from './makeId'; | ||
export * from './functionalUpdate'; |