This repository has been archived by the owner on Oct 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portfolio.js
172 lines (159 loc) · 5.75 KB
/
portfolio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React from 'react'
import { graphql } from 'gatsby'
import PropTypes from 'prop-types'
import { Button } from 'rebass'
import { Layout, PortfolioProjectCard } from '../components/common'
import Masonry from "react-masonry-component"
class PortfolioPage extends React.Component {
constructor(props) {
super(props)
this.state = {
projectFilter: `ALL`,
}
this.onFilterChange = this.onFilterChange.bind(this)
}
onFilterChange = (newFilter) => {
this.setState({
projectFilter: newFilter,
})
//Update URL hash without keeping it in the history, just so reloading the page uses the right filter
window.history.replaceState(null, null, `#${newFilter}`)
}
// eslint-disable-next-line no-unused-vars
shouldComponentUpdate(nextProps, nextState, nextContext) {
return this.props.location.hash === nextProps.location.hash
}
componentDidMount() {
//Pick the right filter right from the URL, if the hash is available, e.g.: /portfolio#android
this.onFilterChange(this.props.location.hash ? this.props.location.hash.slice(`#`.length) : `ALL`)
}
render() {
const portfolioHomePage = this.props.data.ghostPage
const pages = this.props.data.allGhostPage.edges
const portfolioTagNames = new Set()
portfolioTagNames.add(`ALL`)
pages.forEach((node) => {
const page = node.node
if (page.tags) {
page.tags.forEach((pageTag) => {
if (pageTag.name && pageTag.name.startsWith(`portfolio-`)) {
portfolioTagNames.add(pageTag.name.slice(`portfolio-`.length))
}
})
}
})
const portfolioTags = []
Array.from(portfolioTagNames).sort().forEach((portfolioTag) => {
let filterButtonBgColor = null
if (this.state.projectFilter) {
if (this.state.projectFilter === portfolioTag) {
filterButtonBgColor = `tomato`
}
} else {
if (portfolioTag === `ALL`) {
filterButtonBgColor = `tomato`
}
}
portfolioTags.push(<Button color={`black`} bg={filterButtonBgColor} mr={10} mb={10} fontFamily={`monospace`} sx={{
fontSize: 1,
textTransform: `uppercase`,
':hover': {
backgroundColor: `lightgray`,
},
':focus': {
backgroundColor: `tomato`,
},
}} data-filter={portfolioTag} onClick={() => {
this.onFilterChange(portfolioTag)
}}>{portfolioTag}</Button>)
})
return (
<>
<Layout>
<div className="container">
<article className="content" style={{ textAlign: `center` }}>
<h1 className="content-title">{portfolioHomePage.title}</h1>
<section className="content-body">
<div dangerouslySetInnerHTML={{ __html: portfolioHomePage.html }}/>
<div className="button-group filter-button-group grid-filters">
<div className="tabs is-centered is-toggle">
{portfolioTags}
</div>
</div>
<Masonry className="showcase">
{pages
.filter(({ node }) => {
if (this.state.projectFilter) {
if (this.state.projectFilter === `ALL`) {
return true
}
const uniqueTags = new Set()
node.tags.forEach(tag => uniqueTags.add((tag.name.startsWith(`portfolio-`) ?
tag.name.slice(`portfolio-`.length) : tag.name)))
return uniqueTags.has(this.state.projectFilter)
}
return true
})
.map(({ node }) => (
<PortfolioProjectCard key={node.id} page={node}/>
))}
</Masonry>
</section>
</article>
</div>
</Layout>
</>
)
}
}
PortfolioPage.propTypes = {
data: PropTypes.shape({
allGhostPage: PropTypes.object.isRequired,
ghostPage: PropTypes.object.isRequired,
}).isRequired,
location: PropTypes.shape({
pathname: PropTypes.string.isRequired,
hash: PropTypes.string,
}).isRequired,
pageContext: PropTypes.object,
}
export default PortfolioPage
export const pageRootQuery = graphql`
query PortfolioPageQuery {
allGhostPage(
sort: {
order: DESC,
fields: [ published_at ]
},
filter: {
tags: {
elemMatch: {
name: {
in: [ "work" ]
}
}
}
}
) {
edges {
node {
...GhostPageFields
}
}
}
ghostPage(
tags: {
elemMatch: {
name: {
in: [ "page-portfolio-home" ]
}
}
}
) {
id
uuid
title
html
}
}
`