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

Skrf 146 local storage #21

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions src/constants/storageKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const STORAGE_KEYS = {
token: 'token',
} as const;

export { STORAGE_KEYS };
26 changes: 26 additions & 0 deletions src/utils/localStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { STORAGE_KEYS } from '@constants/storageKeys';

type StorageKeys = keyof typeof STORAGE_KEYS;

const setStorage = <T>(key: StorageKeys, value: T) => {
try {
const jsonValue = JSON.stringify(value);
localStorage.setItem(key, jsonValue);
} catch {
throw new Error('๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€์— ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋„์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.');
}
};

const getStorage = <T>(key: StorageKeys, defaultValue?: unknown): T => {
const jsonValue = localStorage.getItem(key);
if (!jsonValue && !defaultValue) {
throw new Error('๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค.');
}
return jsonValue ? JSON.parse(jsonValue) : defaultValue;
};

const deleteStorage = (key: StorageKeys) => {
localStorage.removeItem(key);
};

export { getStorage, setStorage, deleteStorage };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์—๋Ÿฌ๋ฉ”์‹œ์ง€์˜ ๋‚ด์šฉ์ด๋‚˜ ์—๋Ÿฌ์ฒ˜๋ฆฌ๋Š” ์ž„์‹œ์ธ๊ฐ€์š”? ์‚ฌ์šฉ์ž์ž…์žฅ์—์„œ๋Š” ๋‚ด์šฉ์„ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์šธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์—๋Ÿฌ๋ฉ”์‹œ์ง€๋Š” ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค! ์ง€๊ธˆ์€ ๋‹จ์ˆœํžˆ ์—๋Ÿฌ๋ฅผ throwํ–ˆ์Šต๋‹ˆ๋‹ค getStorage๋ฅผ UI๋ ˆ์ด์–ด์—์„œ ํ˜ธ์ถœ์‹œ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ• ์ˆ˜ ์žˆ๊ฒŒ์š”..!
๊ฑฐ๊ธฐ์„œ๋Š” ์‚ฌ์šฉ์ž๋ฅผ ์œ„ํ•œ ์—๋Ÿฌ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ• ๊ฒƒ ๊ฐ™๋„ค์š”.. toast๋ฅผ ๋„์šด๋‹ค๋˜์ง€, ์—๋Ÿฌ ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ค€๋‹ค๋˜์ง€?
์•„๋‹ˆ๋ฉด ์ปค์Šคํ…€ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œ์ผœ์„œ ๊ณตํ†ต์ ์ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค„์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค!