Skip to content

Commit cf72348

Browse files
Merge pull request #45 from lumi-tip/development-lumi
♻️ refactor of some of the code regarding to the fetch (made it async) and 🐛 fix bug with linkedin cache saving url
2 parents 772b4e7 + 0fdc855 commit cf72348

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

pages/[token].js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ const Share = ({ cert }) => {
1616
const { query } = router;
1717
const [strings,setStrings] = useState(translations[query.lang || "en"]);
1818
const [path, setPath] = useState("")
19+
1920
useEffect(() => {
2021
setPath(window.location.href);
2122
},[])
23+
24+
const generateShareUrl = () => {
25+
const timestamp = new Date().getTime();
26+
return `${path}?cacheBuster=${timestamp}`;
27+
};
28+
2229
return (
2330
<>
2431

@@ -51,7 +58,7 @@ const Share = ({ cert }) => {
5158
<Button className="w-100"
5259
icon="arrow"
5360
variant="primary"
54-
href={`https://www.linkedin.com/sharing/share-offsite/?url=${path}`}
61+
href={`https://www.linkedin.com/sharing/share-offsite/?url=${generateShareUrl()}`}
5562
target={"_blank"}>
5663
<Icon name="linked-in" size="md" />
5764
<Button.Label>{strings["Share on LinkedIn"]}</Button.Label>

pages/find/index.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,44 @@ import React, { useEffect } from "react"
22
import { Input } from "../../components/ui/form";
33
import Button from "../../components/ui/Button";
44
import Router from "next/router";
5-
import {Alert} from "react-bootstrap";
5+
import { Alert } from "react-bootstrap";
66

77
const Find = () => {
88
const [token, setToken] = React.useState("");
9-
const [to, setTo] = React.useState("");
109
const [loading, setLoading] = React.useState(false);
11-
const [notify, setNotify] = React.useState({msg: "", type: ""});
10+
const [notify, setNotify] = React.useState({ msg: "", type: "" });
1211
const [show, setShow] = React.useState(true);
1312

14-
const onSubmit = (e) =>{
15-
e.preventDefault();
16-
fetch(`${process.env.NEXT_PUBLIC_BC_HOST}/${token}`)
17-
.then(res => res.json())
18-
.then(data => {
19-
if(token.length < 1 || data.status_code === 404 ) {
20-
setNotify({msg:"Invalid Token or Certificate not found", type:"error"})
21-
} else{
22-
setLoading(true);
23-
Router.push(`/${to}/${token}`)}
24-
}
25-
)
26-
.catch(err => err)
13+
const onSubmit = async (previewFormat) => {
14+
try {
15+
if(!token) return;
16+
setLoading(true)
17+
18+
const res = await fetch(`${process.env.NEXT_PUBLIC_BC_HOST}/${token}`)
19+
const data = res.json()
20+
21+
if(!res.ok || !data) throw new Error(data.detail)
22+
Router.push(`/${previewFormat}/${token}`)
23+
} catch (err) {
24+
console.error("Error fetching data:", err);
25+
setNotify({ msg: "An error occurred while fetching data", type: "error" });
26+
} finally {
27+
setLoading(false)
28+
}
2729
}
2830

2931
return <div className="container">
3032
<div className="row text-center">
3133
<div className="col-12">
3234
<h1>Looking for a certificate?</h1>
3335
</div>
34-
<div className="col-12">
35-
<form className="d-flex" onSubmit={(e) => onSubmit(e)}>
36-
<Input type="text" required onChange={(e) => setToken(e.target.value)} placeholder="Certificate token" className="mr-1 ml-auto"/>
37-
<Button disabled={loading} className="mr-1" type="submit" onClick={() => setTo("pdf")}>{ loading ? "Loading" : "Get certificate"}</Button>
38-
<Button disabled={loading} type="submit" onClick={() => setTo("preview")} className="mr-auto">{ loading ? "Loading" : "Get HTML"}</Button>
39-
</form>
36+
<div className="row mx-auto">
37+
<Input type="text" required onChange={(e) => setToken(e.target.value)} placeholder="Certificate token" className="mr-1 ml-auto" />
38+
<Button disabled={loading} className="mr-1" type="submit" onClick={() => onSubmit("pdf")}>{loading ? "Loading" : "Get certificate"}</Button>
39+
<Button disabled={loading} type="submit" onClick={() => onSubmit("preview")} className="mr-auto">{loading ? "Loading" : "Get HTML"}</Button>
4040
</div>
4141
</div>
42-
{notify.type == "error" ? <Alert onClose={setTimeout(()=> setShow(false),3000)} show={show} variant={"danger"} className="shadow-one mt-4 alert-position">{notify.msg}</Alert> : ""}
42+
{notify.type == "error" ? <Alert onClose={setTimeout(() => setShow(false), 3000)} show={show} variant={"danger"} className="shadow-one mt-4 alert-position">{notify.msg}</Alert> : ""}
4343
</div>
4444
}
4545
export default Find;

0 commit comments

Comments
 (0)