Skip to content

Commit 063c224

Browse files
authored
Merge pull request #5 from TryShape/issue-3-setup-using-fetch-call
Issue 3 setup using fetch call
2 parents 3e0c9a7 + 8e483c2 commit 063c224

12 files changed

Lines changed: 557 additions & 14 deletions

File tree

components/core/App.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React, { useEffect, useState } from "react";
2+
import dynamic from "next/dynamic";
3+
4+
// harperDb fetch call
5+
import { harperFetch } from "../../utils/HarperFetch";
6+
7+
// Dummy Shape Data
8+
// import { shapes } from "../../data/shapes";
9+
// loader
10+
import Loader from "react-loader-spinner";
11+
12+
// ShapeListing
13+
import { ShapeList } from '..';
14+
15+
const App = (props) => {
16+
const [data, setData] = useState([]);
17+
const [loading, setLoading] = useState(true);
18+
19+
useEffect(async () => {
20+
setData([]);
21+
setLoading(true);
22+
23+
// fetching the shape data
24+
const shapes = await harperFetch({
25+
operation: "sql",
26+
sql: "SELECT * FROM tryshape.shapes",
27+
});
28+
console.log(shapes);
29+
let modifiedShapes = shapes.map((shape, index) => {
30+
shape.showAdvanced = false;
31+
return shape;
32+
});
33+
34+
console.log(modifiedShapes);
35+
36+
await setData(modifiedShapes);
37+
setLoading(false);
38+
}, []);
39+
40+
41+
42+
return (
43+
<>
44+
{loading ? (
45+
<Loader
46+
style={{margin: '20% auto auto 42%'}}
47+
type="Circles"
48+
color="#eb3d86"
49+
height={300}
50+
width={300}
51+
/>
52+
) : (
53+
<ShapeList data={ data } />
54+
)}
55+
</>
56+
);
57+
};
58+
59+
export default App;

components/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { default as App } from "./core/App";
2+
3+
export { default as ShapeList } from './utils/ShapeList';

components/utils/ShapeList.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import React, { useState } from "react";
2+
import dynamic from "next/dynamic";
3+
4+
// Toast
5+
import toast from "react-hot-toast";
6+
7+
// Clip-Path
8+
const Shape = dynamic(import("react-clip-path"), { ssr: false });
9+
10+
// Switch
11+
import Switch from "react-switch";
12+
13+
// Shape Listing Styled-Componentns
14+
import {
15+
ShapeCards,
16+
ShapeCard,
17+
ShapeName,
18+
ShapePallete,
19+
ShapeDetailsItems,
20+
ShapeCardSwitch,
21+
ShapeActions,
22+
ShapeCardHeader,
23+
CopyIcon,
24+
DownloadIcon,
25+
LikeIcon,
26+
} from './StyledComponents';
27+
28+
const ShapeList = ({ data }) => {
29+
30+
const [shapes, setShapes] = useState(data);
31+
32+
const handleSwicth = (shapeName) => {
33+
console.log(shapeName);
34+
35+
let modifiedShapes = shapes.map((shape, index) => {
36+
if (shape.name === shapeName) {
37+
return {
38+
...shape,
39+
showAdvanced: !shape.showAdvanced,
40+
};
41+
}
42+
return shape;
43+
});
44+
console.log(modifiedShapes);
45+
setShapes(...[modifiedShapes]);
46+
};
47+
48+
const getShapeFileName = (name) => {
49+
return name.split(" ").join("-");
50+
};
51+
52+
async function performCopy(event, formula) {
53+
event.preventDefault();
54+
try {
55+
await navigator.clipboard.writeText(formula);
56+
toast.success("Successfully Copied!");
57+
console.log("The clip-path formula copied to clipboard");
58+
} catch (err) {
59+
console.error("Failed to copy: ", err);
60+
}
61+
}
62+
63+
return (
64+
<ShapePallete>
65+
<ShapeCards>
66+
{shapes.map((shape, index) => (
67+
<React.Fragment key={index}>
68+
<ShapeCard>
69+
<ShapeCardHeader>
70+
<ShapeName>{shape.name}</ShapeName>
71+
<ShapeActions>
72+
<span title="Like">
73+
<LikeIcon size={24} />
74+
</span>{" "}
75+
<span title="Download">
76+
<DownloadIcon
77+
size={24}
78+
onClick={(event) =>
79+
saveAsPng(
80+
event,
81+
`${getShapeFileName(shape.name)}-id`,
82+
getShapeFileName(shape.name)
83+
)
84+
}
85+
/>
86+
</span>
87+
</ShapeActions>
88+
</ShapeCardHeader>
89+
<Shape
90+
width="300px"
91+
height="300px"
92+
name={shape.name}
93+
id={`${getShapeFileName(shape.name)}-id`}
94+
backgroundColor="#eb3d86"
95+
showShadow={shape.showAdvanced}
96+
/>
97+
98+
<ShapeCardSwitch>
99+
<label htmlFor={`${getShapeFileName(shape.name)}-form`}>
100+
<span>Show Clip-Path Info</span>{" "}
101+
<Switch
102+
onChange={() => handleSwicth(shape.name)}
103+
checked={shape.showAdvanced}
104+
id={`${getShapeFileName(shape.name)}-form`}
105+
/>
106+
</label>
107+
</ShapeCardSwitch>
108+
109+
{shape.showAdvanced && (
110+
<ShapeDetailsItems>
111+
<span>
112+
<b>Clip-Path:</b>{" "}
113+
<code>
114+
<b>{shape.formula}</b>
115+
</code>
116+
</span>{" "}
117+
<span title="Copy">
118+
<CopyIcon
119+
size={24}
120+
onClick={(event) => performCopy(event, shape.formula)}
121+
/>
122+
</span>
123+
</ShapeDetailsItems>
124+
)}
125+
</ShapeCard>
126+
</React.Fragment>
127+
))}
128+
</ShapeCards>
129+
</ShapePallete>
130+
);
131+
};
132+
133+
export default ShapeList;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import styled from "styled-components";
2+
import { FiCopy, FiDownload, FiHeart } from 'react-icons/fi';
3+
4+
const ShapeCards = styled.div`
5+
display: flex;
6+
flex-wrap: wrap;
7+
justify-content: center;
8+
align-items: center;
9+
`;
10+
11+
const ShapeCard = styled.div`
12+
width: 400px;
13+
min-height: 456px;
14+
border: 1px solid #ececec;
15+
border-radius: 4px;
16+
padding: 5px;
17+
margin: 5px;
18+
background-color: #ebebeb;
19+
`;
20+
21+
const ShapeActions = styled.div`
22+
float: right;
23+
`;
24+
25+
const ShapeName = styled.span`
26+
font-weight: bold;
27+
font-size: 20px;
28+
`;
29+
30+
const Playground = styled.div`
31+
width: 100%;
32+
`;
33+
34+
const ShapeDetails = styled.ul`
35+
background-color: #ebebeb;
36+
border-radius: 4px;
37+
padding: 10px;
38+
width: 100%;
39+
`;
40+
41+
const ShapeDetailsItems = styled.li`
42+
word-wrap: break-word;
43+
`;
44+
45+
const ShapePallete = styled.div`
46+
margin-top: 5px;
47+
`;
48+
49+
const ShapeCardHeader = styled.div`
50+
padding: 5px;
51+
margin: 5px;
52+
`;
53+
54+
const ShapeCardSwitch = styled.div`
55+
margin: 5px auto auto 9px;
56+
`;
57+
58+
const CopyIcon = styled(FiCopy)`
59+
cursor: pointer;
60+
&:hover {
61+
color: #f71b76;
62+
}
63+
`;
64+
65+
const DownloadIcon = styled(FiDownload)`
66+
cursor: pointer;
67+
&:hover {
68+
color: #f71b76;
69+
}
70+
`;
71+
72+
const LikeIcon = styled(FiHeart)`
73+
cursor: pointer;
74+
&:hover {
75+
color: #f71b6f;
76+
}
77+
`;
78+
79+
export {
80+
ShapeCards,
81+
ShapeCard,
82+
ShapeName,
83+
Playground,
84+
ShapeDetails,
85+
ShapeDetailsItems,
86+
ShapeCardSwitch,
87+
ShapePallete,
88+
CopyIcon,
89+
DownloadIcon,
90+
LikeIcon,
91+
ShapeActions,
92+
ShapeCardHeader };

0 commit comments

Comments
 (0)