Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

demo-166-storage-api-webapis - Done #172

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ So far, it includes the following examples:
1. 🌐 URL API
1. 🗒️ Selection API
1. 📃 Page Visibility API
1. 💽 Storage API

# 🤝 Open Source

Expand Down
18 changes: 18 additions & 0 deletions src/modules/apis/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,24 @@ export const data: Array<Demo> = [
apiDocURL: '',
canIUseURL: 'https://caniuse.com/pagevisibility',
},
},
{
id: 'storage',
emoji: '💽',
title: 'Storage API',
description: 'The Storage interface of the Web Storage API provides access to a particular domain\'s session or local storage. It allows, for example, the addition, modification, or deletion of stored data items.',
meta: {
author: {
name: 'Ade Adedoja',
social: {
email: '[email protected]',
github: 'ade1705',
twitter: 'TrussDamola',
},
},
apiDocURL: '',
canIUseURL: 'https://caniuse.com/storage',
},
},
{
id: 'selection-api',
Expand Down
66 changes: 66 additions & 0 deletions src/modules/apis/storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
export const hasSupport = (): boolean => {
return 'localStorage' in window && 'sessionStorage' in window;
}

const localStorageHandler = {
set(callback: () => void): void {
const localStorageKeyElement = document.getElementById('js-input-local-storage--key') as HTMLInputElement;
const localStorageValueElement = document.getElementById('js-input-local-storage--value') as HTMLInputElement;
try {
localStorage.setItem(localStorageKeyElement.value, localStorageValueElement.value);
alert('Key/Value pair saved successfully to Local Storage');
localStorageKeyElement.value = "";
localStorageValueElement.value = "";
callback();
} catch (e) {
alert('Error saving Key/Value pair');
}
},
get(): Record<string, string> {
return ({...localStorage});
},
clear(callback: () => void): void {
try {
localStorage.clear();
alert('Local Storage successfully cleared');
callback();
} catch (e) {
alert('Error clearing Local Storage');
}
},
}

const sessionStorageHandler = {
set(callback: () => void): void {
const sessionStorageKeyElement = document.getElementById('js-input-session-storage--key') as HTMLInputElement;
const sessionStorageValueElement = document.getElementById('js-input-session-storage--value') as HTMLInputElement;
try {
sessionStorage.setItem(sessionStorageKeyElement.value, sessionStorageValueElement.value);
alert('Key/Value pair saved successfully to Session Storage');
sessionStorageKeyElement.value = "";
sessionStorageValueElement.value = "";
callback();
} catch (e) {
alert('Error saving Key/Value pair');
}
},
get(): Record<string, string> {
return ({...sessionStorage});
},
clear(callback: () => void): void {
try {
sessionStorage.clear();
alert('Session Storage successfully cleared');
callback();
} catch (e) {
alert('Error clearing Session Storage');
}
},
}

const run = {
sessionStorageHandler,
localStorageHandler,
};

export default run;
293 changes: 293 additions & 0 deletions src/modules/demos/storage/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
import run, { hasSupport } from '../../apis/storage';
import React, {useEffect, useState} from "react";
import {Input} from "@/components/Input";
import {Button} from "@/components/Button";

function Storage() {
const [localStorageData, setLocalStorageData] = useState<Record<string, string>>({});
const [sessionStorageData, setSessionStorageData] = useState<Record<string, string>>({});
const initData = () => {
setLocalStorageData(run.localStorageHandler.get());
setSessionStorageData(run.sessionStorageHandler.get());
}

useEffect(() => {
initData();
}, []);

if (!hasSupport) {
return <h1>Unsupported</h1>;
}

return (
<>
<>
<h2 className="tw-text-xl tw-font-bold tw-mb-3">
Local Storage
<small
className="tw-underline tw-text-sm tw-ml-2 tw-cursor-pointer"
onClick={() => run.localStorageHandler.clear(initData)}
>
Clear Local Storage
</small>
</h2>
<div className="tw-mb-6">
<div className="tw-grid tw-grid-cols-3 tw-gap-4">
<div>
<Input
id="js-input-local-storage--key"
type="text"
name="key"
placeholder="Key"
/>
</div>
<div>
<Input
id="js-input-local-storage--value"
type="text"
name="value"
placeholder="Value"
/>
</div>
<Button onClick={() => run.localStorageHandler.set(initData)}>Save</Button>
</div>
<div
className="
tw-mt-4
tw-shadow
tw-overflow-hidden
tw-border-b
tw-border-gray-200
sm:tw-rounded-lg
"
>
<table
className="
tw-w-full
tw-min-w-full
tw-divide-y
tw-divide-gray-200
"
>
<thead className="tw-bg-gray-100">
<tr>
<th
scope="col"
className="
tw-px-6
tw-py-3
tw-text-left
tw-text-xs
tw-font-medium
tw-text-gray-500
tw-uppercase
tw-tracking-wider
"
>
Key
</th>
<th
scope="col"
className="
tw-px-6
tw-py-3
tw-text-left
tw-text-xs
tw-font-medium
tw-text-gray-500
tw-uppercase
tw-tracking-wider
"
>
Value
</th>
</tr>
</thead>
<tbody
className="
tw-bg-white
tw-divide-y
tw-divide-gray-200
"
>
{Object.keys(localStorageData).map((key: string, index: number) => {
return (
<tr key={key + index}>
<td
className="
tw-px-6
tw-py-4
tw-whitespace-nowrap
"
>
<div
className="
tw-text-sm
tw-text-gray-900
"
>
{key}
</div>
</td>
<td
className="
tw-px-6
tw-py-4
tw-whitespace-nowrap
"
>
<span
className="
tw-text-sm
tw-text-gray-900
"
>
{localStorageData[key]}
</span>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
</div>
</>

<>
<h2 className="tw-text-xl tw-font-bold tw-mb-3">
Session Storage
<small
className="tw-underline tw-text-sm tw-ml-2 tw-cursor-pointer"
onClick={() => run.sessionStorageHandler.clear(initData)}
>
Clear Session Storage
</small>
</h2>
<div className="tw-mb-6">
<div className="tw-grid tw-grid-cols-3 tw-gap-4">
<div>
<Input
id="js-input-session-storage--key"
type="text"
name="key"
placeholder="Key"
/>
</div>
<div>
<Input
id="js-input-session-storage--value"
type="text"
name="value"
placeholder="Value"
/>
</div>
<Button onClick={() => run.sessionStorageHandler.set(initData)}>Save</Button>
</div>
<div
className="
tw-mt-4
tw-shadow
tw-overflow-hidden
tw-border-b
tw-border-gray-200
sm:tw-rounded-lg
"
>
<table
className="
tw-w-full
tw-min-w-full
tw-divide-y
tw-divide-gray-200
"
>
<thead className="tw-bg-gray-100">
<tr>
<th
scope="col"
className="
tw-px-6
tw-py-3
tw-text-left
tw-text-xs
tw-font-medium
tw-text-gray-500
tw-uppercase
tw-tracking-wider
"
>
Key
</th>
<th
scope="col"
className="
tw-px-6
tw-py-3
tw-text-left
tw-text-xs
tw-font-medium
tw-text-gray-500
tw-uppercase
tw-tracking-wider
"
>
Value
</th>
</tr>
</thead>
<tbody
className="
tw-bg-white
tw-divide-y
tw-divide-gray-200
"
>
{Object.keys(sessionStorageData).map((key: string, index: number) => {
return (
<tr key={key + index}>
<td
className="
tw-px-6
tw-py-4
tw-whitespace-nowrap
"
>
<div
className="
tw-text-sm
tw-text-gray-900
"
>
{key}
</div>
</td>
<td
className="
tw-px-6
tw-py-4
tw-whitespace-nowrap
"
>
<span
className="
tw-text-sm
tw-text-gray-900
"
>
{sessionStorageData[key]}
</span>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
</div>
</>
</>
);
}

export default Storage;