joyent-portal/consoles/my-joy-navigation/src/header.js

165 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-01-17 21:10:39 +02:00
import React from 'react';
import { graphql, compose } from 'react-apollo';
import gql from 'graphql-tag';
import pascalCase from 'pascal-case';
import keys from 'lodash.keys';
2018-01-17 21:10:39 +02:00
import { DataCenterIcon, TritonIcon, ServicesIcon } from './components';
import * as Overlays from './containers';
import {
Avatar,
Header,
HeaderRow,
HeaderItem,
HeaderItemContent,
HeaderItemSubContent,
HeaderItemIcon,
HeaderFlexibleSpaceItem,
HeaderDividerItem,
HeaderSpace
2018-01-17 21:10:39 +02:00
} from './components';
const UpdateHeaderMutation = gql`
2018-01-17 21:10:39 +02:00
mutation updateHeader($isOpen: Boolean!, $activePanel: String!) {
updateHeader(isOpen: $isOpen, activePanel: $activePanel) @client
}
`;
const GetHeader = gql`
2018-01-17 21:10:39 +02:00
{
isOpen @client
activePanel @client
}
`;
const GetAccount = gql`
{
datacenter {
name
}
account {
login
2018-03-28 16:26:25 +03:00
emailHash
}
}
`;
const Navigation = ({
2018-03-28 16:26:25 +03:00
user = {},
datacenter,
toggleSectionOpen,
isOpen,
activePanel
}) => (
2018-01-17 21:10:39 +02:00
<Header onOutsideClick={() => toggleSectionOpen()}>
<HeaderRow>
<HeaderItem>
<TritonIcon light />
</HeaderItem>
<HeaderDividerItem />
<HeaderItem
onClick={() => toggleSectionOpen('services')}
active={isOpen && activePanel === 'services'}
>
<HeaderItemContent>Products & Services</HeaderItemContent>
<HeaderItemIcon>
<ServicesIcon light />
</HeaderItemIcon>
</HeaderItem>
<HeaderDividerItem />
2018-01-17 21:10:39 +02:00
<HeaderFlexibleSpaceItem />
<HeaderDividerItem />
{datacenter ? (
<HeaderItem
onClick={() => toggleSectionOpen('datacenter')}
active={isOpen && activePanel === 'datacenter'}
>
<HeaderItemContent>
<HeaderItemSubContent>Data Center:</HeaderItemSubContent>
<HeaderSpace />
<span>{datacenter}</span>
</HeaderItemContent>
<HeaderItemIcon>
<DataCenterIcon light />
</HeaderItemIcon>
</HeaderItem>
) : null}
2018-03-06 03:14:33 +02:00
{datacenter ? <HeaderDividerItem /> : null}
2018-03-28 16:26:25 +03:00
{user.login ? (
<HeaderItem
onClick={() => toggleSectionOpen('account')}
active={isOpen && activePanel === 'account'}
>
<HeaderItemContent>
<HeaderItemSubContent>Account:</HeaderItemSubContent>
<HeaderSpace />
2018-03-28 16:26:25 +03:00
{`${user.login}`}
</HeaderItemContent>
<HeaderItemIcon>
2018-03-28 16:26:25 +03:00
<Avatar src={user.image} />
</HeaderItemIcon>
</HeaderItem>
) : null}
2018-01-17 21:10:39 +02:00
</HeaderRow>
{keys(Overlays).map(panelName =>
React.createElement(Overlays[panelName], {
expanded: isOpen && panelName === pascalCase(activePanel)
})
)}
2018-01-17 21:10:39 +02:00
</Header>
);
export default compose(
graphql(GetAccount, {
options: () => ({
ssr: false
}),
props: ({ data }) => {
const {
account = {},
datacenter = {},
loading = false,
error = null
} = data;
2018-01-08 17:13:05 +02:00
const { name } = datacenter;
2018-03-28 16:26:25 +03:00
const user = {
...account,
image: `https://www.gravatar.com/avatar/${account.emailHash}`
};
return { user, datacenter: name, loading, error };
}
}),
graphql(GetHeader, {
options: () => ({
ssr: false
}),
2018-01-17 21:10:39 +02:00
props: ({ data }) => {
const {
isOpen = false,
activePanel = '',
loading = false,
error = null
} = data;
return { isOpen, activePanel, loading, error };
}
}),
graphql(UpdateHeaderMutation, {
2018-01-17 21:10:39 +02:00
props: ({ mutate, ownProps }) => ({
toggleSectionOpen: (name = '') => {
const { isOpen, activePanel } = ownProps;
return mutate({
variables: {
isOpen: !(isOpen && name === activePanel),
activePanel: name
}
});
}
})
})
)(Navigation);