From 44b75b7f7af743ad3a6ecc0b392bde749a6ad0eb Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Tue, 3 Oct 2023 11:39:42 +0200 Subject: [PATCH] :sparkles: Refacto get anchors on page swap --- package-lock.json | 16 ++++----- package.json | 2 +- .../OfficeFolderAnchors.ts | 36 ++++++++++++++----- .../DesignSystem/Header/Navigation/index.tsx | 25 ++++++++++++- .../Folder/FolderInformation/index.tsx | 2 +- 5 files changed, 62 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8fd2491a..c29f24b2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index bb886725..7d306a4d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/front/Api/LeCoffreApi/Notary/OfficeFolderAnchors/OfficeFolderAnchors.ts b/src/front/Api/LeCoffreApi/Notary/OfficeFolderAnchors/OfficeFolderAnchors.ts index 4aa3bb4b..52735f61 100644 --- a/src/front/Api/LeCoffreApi/Notary/OfficeFolderAnchors/OfficeFolderAnchors.ts +++ b/src/front/Api/LeCoffreApi/Notary/OfficeFolderAnchors/OfficeFolderAnchors.ts @@ -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 { - const url = new URL(this.baseURl.concat(`/${uid}`)); + public async get(q?: IGetAnchorsParams): Promise { + 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(url); + return await this.getRequest(url); } catch (err) { this.onError(err); return Promise.reject(err); } } - public async post(uid: string): Promise { - const url = new URL(this.baseURl.concat(`/${uid}`)); + public async getByUid(uid: string): Promise { + const url = new URL(this.baseUrl.concat(`/${uid}`)); try { - return await this.postRequest(url, {}); + return await this.getRequest(url); + } catch (err) { + this.onError(err); + return Promise.reject(err); + } + } + + public async post(uid: string): Promise { + const url = new URL(this.baseUrl.concat(`/${uid}`)); + try { + return await this.postRequest(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 { - const url = new URL(this.baseURl.concat(`/download/${uid}`)); + const url = new URL(this.baseUrl.concat(`/download/${uid}`)); try { return await this.getRequest(url, undefined, ContentType.PDF, `${uid}.pdf`); } catch (err) { diff --git a/src/front/Components/DesignSystem/Header/Navigation/index.tsx b/src/front/Components/DesignSystem/Header/Navigation/index.tsx index 37b9b84c..f51975a1 100644 --- a/src/front/Components/DesignSystem/Header/Navigation/index.tsx +++ b/src/front/Components/DesignSystem/Header/Navigation/index.tsx @@ -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(); diff --git a/src/front/Components/Layouts/Folder/FolderInformation/index.tsx b/src/front/Components/Layouts/Folder/FolderInformation/index.tsx index 804c8aa7..9c116b19 100644 --- a/src/front/Components/Layouts/Folder/FolderInformation/index.tsx +++ b/src/front/Components/Layouts/Folder/FolderInformation/index.tsx @@ -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);