Refacto get anchors on page swap

This commit is contained in:
Maxime Lalo 2023-10-03 11:39:42 +02:00
parent 360386dd88
commit 44b75b7f7a
5 changed files with 62 additions and 19 deletions

16
package-lock.json generated
View File

@ -22,7 +22,7 @@
"eslint-config-next": "13.2.4",
"form-data": "^4.0.0",
"jwt-decode": "^3.1.2",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.85",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.89",
"next": "13.2.4",
"prettier": "^2.8.7",
"react": "18.2.0",
@ -394,9 +394,9 @@
}
},
"node_modules/@eslint-community/regexpp": {
"version": "4.9.0",
"resolved": "https://registry.npmjs.org/@eslint-community/regexpp/-/regexpp-4.9.0.tgz",
"integrity": "sha512-zJmuCWj2VLBt4c25CfBIbMZLGLyhkvs7LznyVX5HfpzeocThgIj5XQK4L+g3U36mMcx8bPMhGyPpwCATamC4jQ==",
"version": "4.9.1",
"resolved": "https://registry.npmjs.org/@eslint-community/regexpp/-/regexpp-4.9.1.tgz",
"integrity": "sha512-Y27x+MBLjXa+0JWDhykM3+JE+il3kHKAEqabfEWq3SDhZjLYb6/BHL/JKFnH3fe207JaXkyDo685Oc2Glt6ifA==",
"engines": {
"node": "^12.0.0 || ^14.0.0 || >=16.0.0"
}
@ -1507,9 +1507,9 @@
}
},
"node_modules/caniuse-lite": {
"version": "1.0.30001542",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001542.tgz",
"integrity": "sha512-UrtAXVcj1mvPBFQ4sKd38daP8dEcXXr5sQe6QNNinaPd0iA/cxg9/l3VrSdL73jgw5sKyuQ6jNgiKO12W3SsVA==",
"version": "1.0.30001543",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001543.tgz",
"integrity": "sha512-qxdO8KPWPQ+Zk6bvNpPeQIOH47qZSYdFZd6dXQzb2KzhnSXju4Kd7H1PkSJx6NICSMgo/IhRZRhhfPTHYpJUCA==",
"funding": [
{
"type": "opencollective",
@ -3361,7 +3361,7 @@
}
},
"node_modules/le-coffre-resources": {
"resolved": "git+ssh://git@github.com/smart-chain-fr/leCoffre-resources.git#0e1663716a698cc584a89e5e2a03b72113702d55",
"resolved": "git+ssh://git@github.com/smart-chain-fr/leCoffre-resources.git#a100398ef5c1984ba74cb1c8c182648315accc3e",
"license": "MIT",
"dependencies": {
"class-transformer": "^0.5.1",

View File

@ -24,7 +24,7 @@
"eslint-config-next": "13.2.4",
"form-data": "^4.0.0",
"jwt-decode": "^3.1.2",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.88",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.89",
"next": "13.2.4",
"prettier": "^2.8.7",
"react": "18.2.0",

View File

@ -1,9 +1,15 @@
import { ContentType } from "@Front/Api/BaseApiService";
import BaseNotary from "../BaseNotary";
import { OfficeFolderAnchor } from "le-coffre-resources/dist/Notary";
export interface IGetAnchorsParams {
where?: {};
include?: {};
select?: {};
}
export default class OfficeFolderAnchors extends BaseNotary {
private static instance: OfficeFolderAnchors;
private readonly baseURl = this.namespaceUrl.concat("/anchors");
private readonly baseUrl = this.namespaceUrl.concat("/anchors");
private constructor() {
super();
@ -17,20 +23,34 @@ export default class OfficeFolderAnchors extends BaseNotary {
}
}
public async get(uid: string): Promise<any> {
const url = new URL(this.baseURl.concat(`/${uid}`));
public async get(q?: IGetAnchorsParams): Promise<OfficeFolderAnchor[]> {
const url = new URL(this.baseUrl);
if (q) {
const query = { q };
Object.entries(query).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value)));
}
try {
return await this.getRequest<any>(url);
return await this.getRequest<OfficeFolderAnchor[]>(url);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
public async post(uid: string): Promise<any> {
const url = new URL(this.baseURl.concat(`/${uid}`));
public async getByUid(uid: string): Promise<OfficeFolderAnchor> {
const url = new URL(this.baseUrl.concat(`/${uid}`));
try {
return await this.postRequest<any>(url, {});
return await this.getRequest<OfficeFolderAnchor>(url);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
public async post(uid: string): Promise<OfficeFolderAnchor> {
const url = new URL(this.baseUrl.concat(`/${uid}`));
try {
return await this.postRequest<OfficeFolderAnchor>(url, {});
} catch (err) {
this.onError(err);
return Promise.reject(err);
@ -38,7 +58,7 @@ export default class OfficeFolderAnchors extends BaseNotary {
}
public async download(uid: string): Promise<any> {
const url = new URL(this.baseURl.concat(`/download/${uid}`));
const url = new URL(this.baseUrl.concat(`/download/${uid}`));
try {
return await this.getRequest<any>(url, undefined, ContentType.PDF, `${uid}.pdf`);
} catch (err) {

View File

@ -8,10 +8,33 @@ import { AppRuleActions, AppRuleNames } from "@Front/Api/Entities/rule";
import { usePathname } from "next/navigation";
import Notifications from "@Front/Api/LeCoffreApi/Notary/Notifications/Notifications";
import Toasts from "@Front/Stores/Toasts";
import OfficeFolderAnchors from "@Front/Api/LeCoffreApi/Notary/OfficeFolderAnchors/OfficeFolderAnchors";
export default function Navigation() {
const pathname = usePathname();
const getAnchoringStatus = useCallback(async () => {
const anchors = await OfficeFolderAnchors.getInstance().get({
where: {
status: {
not: "VERIFIED_ON_CHAIN",
},
},
include: {
folder: true,
},
});
try {
for (const anchor of anchors) {
await OfficeFolderAnchors.getInstance().getByUid(anchor.folder?.uid as string);
}
} catch (e) {
console.log(e);
}
}, []);
const getNotifications = useCallback(async () => {
await getAnchoringStatus();
const notifications = await Notifications.getInstance().get({
where: {
read: false,
@ -24,7 +47,7 @@ export default function Navigation() {
redirectUrl: notification.notification.redirection_url,
});
});
}, []);
}, [getAnchoringStatus]);
useEffect(() => {
getNotifications();

View File

@ -409,7 +409,7 @@ export default function FolderInformation(props: IProps) {
const getAnchoringStatus = useCallback(async () => {
if (!folderUid) return;
try {
const anchorStatus = await OfficeFolderAnchors.getInstance().get(folderUid as string);
const anchorStatus = await OfficeFolderAnchors.getInstance().getByUid(folderUid as string);
setIsAnchored(anchorStatus.status === "VERIFIED_ON_CHAIN" ? AnchorStatus.VERIFIED_ON_CHAIN : AnchorStatus.ANCHORING);
} catch (e) {
setIsAnchored(AnchorStatus.NOT_ANCHORED);