-
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.
- Loading branch information
1 parent
3f38415
commit bce8e6c
Showing
18 changed files
with
263 additions
and
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,72 @@ | ||
import React, { useCallback, useEffect, useState } from 'react' | ||
import { useLocation } from 'react-router-dom' | ||
import { apiFetchMenuList } from '../data/api' | ||
import { useApi } from '../hooks/useApi' | ||
import { Menu } from '../types/classes/chain' | ||
import { Menu, NetworkType } from '../types/classes/chain' | ||
|
||
const DashboardContext: React.Context<{ | ||
chains: Menu | ||
updateMenu: () => void | ||
collapse: string[] | ||
setCollapse: React.Dispatch<React.SetStateAction<string[]>> | ||
collapse: NetworkType[] | ||
toggleCollapse: (type: NetworkType) => void | ||
}> = React.createContext({} as any) | ||
interface Props { | ||
children: React.ReactNode | ||
} | ||
|
||
const DashboardProvider = React.memo( | ||
({ children }): React.ReactElement<Props> => { | ||
const [chains, setChains] = useState<Menu>({} as Menu) | ||
const [collapse, setCollapse] = useState<string[]>([]) | ||
const { user } = useApi() | ||
|
||
const updateMenu = useCallback(() => { | ||
apiFetchMenuList(user.id).then((_chains) => { | ||
setChains(_chains) | ||
}) | ||
}, [user.id]) | ||
|
||
useEffect(() => { | ||
updateMenu() | ||
}, [updateMenu]) | ||
|
||
|
||
|
||
return ( | ||
<DashboardContext.Provider | ||
value={{ | ||
chains, | ||
updateMenu, | ||
collapse, | ||
setCollapse, | ||
}} | ||
> | ||
{children} | ||
</DashboardContext.Provider> | ||
) | ||
const DashboardProvider = (props: Props): React.ReactElement<Props> => { | ||
const [chains, setChains] = useState<Menu>({} as Menu) | ||
const [collapse, setCollapse] = useState<NetworkType[]>([]) | ||
const location = useLocation() | ||
const { user } = useApi() | ||
|
||
const updateMenu = useCallback(() => { | ||
apiFetchMenuList(user.id).then((_chains) => { | ||
setChains(_chains) | ||
}) | ||
}, [user.id]) | ||
|
||
const toggleCollapse = (type: NetworkType) => { | ||
const idx = collapse.indexOf(type) | ||
if (idx > -1) { | ||
collapse.splice(idx, 1) | ||
} else { | ||
collapse.push(type) | ||
} | ||
setCollapse(collapse.slice()) | ||
} | ||
) | ||
|
||
useEffect(() => { | ||
const chain = location.pathname.split('/')[3] as NetworkType | ||
|
||
for (let type in chains) { | ||
const selectedChainInfo = chains[type].find((info) => info.name === chain) | ||
if (selectedChainInfo && collapse.indexOf(chain) === -1) { | ||
collapse.push(selectedChainInfo.network) | ||
setCollapse(collapse.slice()) | ||
} | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [location.pathname, chains]) | ||
|
||
useEffect(() => { | ||
console.log('xxx') | ||
|
||
updateMenu() | ||
}, [updateMenu]) | ||
|
||
return ( | ||
<DashboardContext.Provider | ||
value={{ | ||
chains, | ||
updateMenu, | ||
collapse, | ||
toggleCollapse, | ||
}} | ||
> | ||
{props.children} | ||
</DashboardContext.Provider> | ||
) | ||
} | ||
|
||
export { DashboardContext, DashboardProvider } |
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 |
---|---|---|
|
@@ -5,4 +5,5 @@ export class User { | |
maxProjectNum: number | ||
projectNum: number | ||
bwDayLimit: number | ||
reqDayLimit: number | ||
} |
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
Oops, something went wrong.