From eb4f655a18c799e3b80c8aee45fc6f1540be7c98 Mon Sep 17 00:00:00 2001 From: Stefan Date: Wed, 14 Jun 2023 16:40:50 +0200 Subject: [PATCH] Add session storage to token snippet --- shared/_tokenSnippet.jsx | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/shared/_tokenSnippet.jsx b/shared/_tokenSnippet.jsx index cedd799..e1787eb 100644 --- a/shared/_tokenSnippet.jsx +++ b/shared/_tokenSnippet.jsx @@ -3,6 +3,8 @@ import './tokenSnippet.css'; const BASE_URL = 'https://stream-calls-dogfood.vercel.app/api/call/sample?'; +const STORAGE_KEY = 'tokenSnippetData'; + async function callAPI(sampleApp) { const constructedUrl = constructUrl(sampleApp); const response = await fetch(constructedUrl); @@ -37,13 +39,23 @@ export class TokenSnippet extends React.Component { } componentDidMount() { - callAPI(this.state.sampleApp).then((result) => { + const storedData = sessionStorage.getItem(STORAGE_KEY); + if (storedData) { this.setState({ ...this.state, loadingFinished: true, - ...result, + ...JSON.parse(storedData), + }); + } else { + callAPI(this.state.sampleApp).then((result) => { + sessionStorage.setItem(STORAGE_KEY, JSON.stringify(result)); + this.setState({ + ...this.state, + loadingFinished: true, + ...result, + }); }); - }); + } } render() {