-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use paths node summery on category page header
- Loading branch information
Showing
3 changed files
with
113 additions
and
101 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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React from 'react'; | ||
|
||
import { InstanceType } from 'common/__generated__/paths/graphql'; | ||
|
||
import ContentLoader from 'react-content-loader'; | ||
import { useTheme } from 'styled-components'; | ||
|
||
import { activeGoalVar } from '@/context/paths/cache'; | ||
import { GET_NODE_CONTENT } from '@/queries/paths/get-paths-node'; | ||
import { getHttpHeaders } from '@/utils/paths/paths.utils'; | ||
|
||
import { NetworkStatus, useQuery, useReactiveVar } from '@apollo/client'; | ||
|
||
import InventoryNodeSummary from './InventoryNodeSummary'; | ||
import ActionNodeSummary from './ActionNodeSummary'; | ||
|
||
const PathsContentLoader = (props) => { | ||
const theme = useTheme(); | ||
return ( | ||
<ContentLoader | ||
speed={1} | ||
width={330} | ||
height={80} | ||
viewBox="0 0 330 80" | ||
backgroundColor={theme.graphColors.grey010} | ||
foregroundColor={theme.graphColors.grey030} | ||
{...props} | ||
> | ||
<rect x="5" y="1" rx="3" ry="3" width="166" height="16" /> | ||
<rect x="0" y="88" rx="3" ry="3" width="178" height="6" /> | ||
<rect x="6" y="24" rx="3" ry="3" width="130" height="27" /> | ||
<rect x="4" y="61" rx="3" ry="3" width="166" height="16" /> | ||
</ContentLoader> | ||
); | ||
}; | ||
|
||
type PathsNodeContentProps = { | ||
categoryId: string; | ||
node: string; | ||
pathsInstance: InstanceType; | ||
onLoaded?: (id: string, impact: number) => void; | ||
}; | ||
|
||
const PathsNodeSummary = React.memo((props: PathsNodeContentProps) => { | ||
const { categoryId, node, pathsInstance, onLoaded } = props; | ||
const pathsInstanceId = pathsInstance.id; | ||
const activeGoal = useReactiveVar(activeGoalVar); | ||
const displayAllGoals = true; | ||
const displayGoals = displayAllGoals | ||
? pathsInstance.goals | ||
: activeGoal | ||
? [activeGoal] | ||
: undefined; | ||
|
||
const { data, loading, error, networkStatus } = useQuery(GET_NODE_CONTENT, { | ||
fetchPolicy: 'no-cache', | ||
variables: { node: node, goal: activeGoal?.id }, | ||
notifyOnNetworkStatusChange: true, | ||
context: { | ||
uri: '/api/graphql-paths', | ||
headers: getHttpHeaders({ instanceIdentifier: pathsInstanceId }), | ||
}, | ||
}); | ||
|
||
const refetching = networkStatus === NetworkStatus.refetch; | ||
|
||
if (loading && !refetching) { | ||
return <PathsContentLoader />; | ||
} | ||
if (error) { | ||
return <div>Error: {error.message}</div>; // Handle error appropriately | ||
} | ||
if (data) { | ||
if (data.node.__typename === 'ActionNode') { | ||
return ( | ||
<ActionNodeSummary | ||
categoryId={categoryId} | ||
node={data.node} | ||
onLoaded={onLoaded ? onLoaded : () => void 0} | ||
refetching={refetching} | ||
/> | ||
); | ||
} else if (data.node.__typename && displayGoals) { | ||
return ( | ||
<InventoryNodeSummary | ||
categoryId={categoryId} | ||
node={data.node} | ||
onLoaded={onLoaded ? onLoaded : () => void 0} | ||
displayGoals={displayGoals} | ||
/> | ||
); | ||
} | ||
return null; | ||
} | ||
}); | ||
|
||
PathsNodeSummary.displayName = 'PathsNodeSummary'; | ||
|
||
export default PathsNodeSummary; |
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