From 958e62d733e19813e76a2f9d5d49d46ac1eebbc2 Mon Sep 17 00:00:00 2001 From: toledo Date: Sat, 8 Apr 2023 16:24:15 +0800 Subject: [PATCH] Polished apis --- package-lock.json | 175 +++++++++-------------------- package.json | 1 + src/Components/Api/Api.tsx | 63 +++++------ src/Features/Redux/Store/Store.tsx | 12 +- 4 files changed, 88 insertions(+), 163 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9f07568..d08aef5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "dependencies": { "@expo/webpack-config": "^18.0.1", + "@react-native-async-storage/async-storage": "^1.18.1", "@react-navigation/drawer": "^6.6.2", "@react-navigation/native": "^6.1.6", "@reduxjs/toolkit": "^1.9.3", @@ -2815,13 +2816,13 @@ "integrity": "sha512-TI+l71+5aSKnShYclFa14Kum+hQMZ86b95SH6tQUG3qZEmLTarvWpKwqtTwQKqvlJSJrpFiSFu3eCuZokY6zWA==" }, "node_modules/@expo/webpack-config": { - "version": "18.0.1", - "resolved": "https://registry.npmjs.org/@expo/webpack-config/-/webpack-config-18.0.1.tgz", - "integrity": "sha512-0C+wjmmQ0usySdhtzeRp0yYuf9zkUZ/kNgA6AHQ9N7eG4JIr0DM1c87g119smxcJTbd8N+//mv5znPxSJqBqmg==", + "version": "18.0.3", + "resolved": "https://registry.npmjs.org/@expo/webpack-config/-/webpack-config-18.0.3.tgz", + "integrity": "sha512-/1sg+hDtvSQCEWDP6J+3yM3+SGcUiUinX/JarBTsH8MudvzeUmZgsI8IxRFYGfUPYUkwJ5e57eHQzadj0taeHQ==", "dependencies": { - "@babel/core": "^7.16.0", + "@babel/core": "^7.20.2", "@expo/config": "6.0.20", - "babel-loader": "^8.2.3", + "babel-loader": "^8.3.0", "chalk": "^4.0.0", "clean-webpack-plugin": "^4.0.0", "copy-webpack-plugin": "^10.2.0", @@ -3737,6 +3738,17 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/@react-native-async-storage/async-storage": { + "version": "1.18.1", + "resolved": "https://registry.npmjs.org/@react-native-async-storage/async-storage/-/async-storage-1.18.1.tgz", + "integrity": "sha512-70aFW8fVCKl+oA1AKPFDpE6s4t9pulj2QeLX+MabEmzfT3urd/3cckv45WJvtocdoIH/oXA3Y+YcCRJCcNa8mA==", + "dependencies": { + "merge-options": "^3.0.4" + }, + "peerDependencies": { + "react-native": "^0.0.0-0 || 0.60 - 0.72 || 1000.0.0" + } + }, "node_modules/@react-native-community/cli": { "version": "10.2.2", "resolved": "https://registry.npmjs.org/@react-native-community/cli/-/cli-10.2.2.tgz", @@ -6052,9 +6064,9 @@ "integrity": "sha512-Xj9XuRuz3nTSbaTXWv3itLOcxyF4oPD8douBBmj7U9BBC6nEBYfyOJYQMf/8PJAFotC62UY5dFfIGEPr7WswzQ==" }, "node_modules/babel-preset-expo": { - "version": "9.3.0", - "resolved": "https://registry.npmjs.org/babel-preset-expo/-/babel-preset-expo-9.3.0.tgz", - "integrity": "sha512-cIz+5TVBkcZgtfpTyFPo1peswr2dvQj2VIwdj5vY37/zESsYBHfaZ+u/A11yb1WnuZHcYD/ZoSLNwmWr20jp4Q==", + "version": "9.3.2", + "resolved": "https://registry.npmjs.org/babel-preset-expo/-/babel-preset-expo-9.3.2.tgz", + "integrity": "sha512-BjyvjwjJG0MaaDBLP/esbXRrAItM76po9L9zfnLxeqgFsHCIPmD+6ir45coDLGAXwR8m9It3G1yqYM9JPyemsQ==", "dependencies": { "@babel/plugin-proposal-decorators": "^7.12.9", "@babel/plugin-proposal-object-rest-spread": "^7.12.13", @@ -6062,7 +6074,7 @@ "@babel/preset-env": "^7.20.0", "babel-plugin-module-resolver": "^4.1.0", "babel-plugin-react-native-web": "~0.18.10", - "metro-react-native-babel-preset": "0.73.7" + "metro-react-native-babel-preset": "0.73.9" } }, "node_modules/babel-preset-fbjs": { @@ -8460,25 +8472,25 @@ "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" }, "node_modules/expo": { - "version": "48.0.5", - "resolved": "https://registry.npmjs.org/expo/-/expo-48.0.5.tgz", - "integrity": "sha512-4sd0y8DX2GXbVWBS+UTKqcm60KeZxh7+EvGUi9jbJJAJkbCyt3smu4L0WGCaq/8+ki44H9G/pjM0B7VcZZjAiQ==", + "version": "48.0.10", + "resolved": "https://registry.npmjs.org/expo/-/expo-48.0.10.tgz", + "integrity": "sha512-8YXG6um3ld36nu/ONEC0NNkMatdj4k/HwR7OUd3dKUt3PJSkZHsCeLXIu8za7WSWpgPAU/xAj35noPFEFnjO1w==", "dependencies": { "@babel/runtime": "^7.20.0", "@expo/cli": "0.6.2", "@expo/config": "8.0.2", "@expo/config-plugins": "6.0.1", "@expo/vector-icons": "^13.0.0", - "babel-preset-expo": "~9.3.0", + "babel-preset-expo": "~9.3.2", "cross-spawn": "^6.0.5", "expo-application": "~5.1.1", - "expo-asset": "~8.9.0", + "expo-asset": "~8.9.1", "expo-constants": "~14.2.1", "expo-file-system": "~15.2.2", "expo-font": "~11.1.1", "expo-keep-awake": "~12.0.1", "expo-modules-autolinking": "1.1.2", - "expo-modules-core": "1.2.4", + "expo-modules-core": "1.2.6", "fbemitter": "^3.0.0", "getenv": "^1.0.0", "invariant": "^2.2.4", @@ -8500,9 +8512,9 @@ } }, "node_modules/expo-asset": { - "version": "8.9.0", - "resolved": "https://registry.npmjs.org/expo-asset/-/expo-asset-8.9.0.tgz", - "integrity": "sha512-zenkZrYcsJAcTVl478mCarVmaOE9r2GbTlVvKZaXL8UJIL4rnspk63xUSJLAGMArZIdHzsGFQMXlQG3pgEfzrg==", + "version": "8.9.1", + "resolved": "https://registry.npmjs.org/expo-asset/-/expo-asset-8.9.1.tgz", + "integrity": "sha512-ugavxA7Scn96TBdeTYQA6xtHktnk0o/0xk7nFkxJKoH/t2cZDFSB05X0BI2/LDZY4iE6xTPOYw4C4mmourWfuA==", "dependencies": { "blueimp-md5": "^2.10.0", "expo-constants": "~14.2.0", @@ -8668,9 +8680,9 @@ } }, "node_modules/expo-modules-core": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/expo-modules-core/-/expo-modules-core-1.2.4.tgz", - "integrity": "sha512-AV0NCTy9O8xQqpKgX6gvsDzV1ogpCzYpGxqM85Vw1xHsOF51s7Avu7NdNjBPUZOVuDderUXAvd97dWrtefSKcA==", + "version": "1.2.6", + "resolved": "https://registry.npmjs.org/expo-modules-core/-/expo-modules-core-1.2.6.tgz", + "integrity": "sha512-vyleKepkP8F6L+D55B/E4FbZ8x9pdy3yw/mdbGBkDkrmo2gmeMjOM1mKLSszOkLIqet05O7Wy8m0FZHZTo0VBg==", "dependencies": { "compare-versions": "^3.4.0", "invariant": "^2.2.4" @@ -11710,6 +11722,25 @@ "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", "integrity": "sha512-cCi6g3/Zr1iqQi6ySbseM1Xvooa98N0w31jzUYrXPX2xqObmFGHJ0tQ5u74H3mVh7wLouTseZyYIq39g8cNp1w==" }, + "node_modules/merge-options": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/merge-options/-/merge-options-3.0.4.tgz", + "integrity": "sha512-2Sug1+knBjkaMsMgf1ctR1Ujx+Ayku4EdJN4Z+C2+JzoeF7A3OZ9KM2GY0CpQS51NR61LTurMJrRKPhSs3ZRTQ==", + "dependencies": { + "is-plain-obj": "^2.1.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/merge-options/node_modules/is-plain-obj": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-2.1.0.tgz", + "integrity": "sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==", + "engines": { + "node": ">=8" + } + }, "node_modules/merge-stream": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", @@ -12117,9 +12148,9 @@ } }, "node_modules/metro-react-native-babel-preset": { - "version": "0.73.7", - "resolved": "https://registry.npmjs.org/metro-react-native-babel-preset/-/metro-react-native-babel-preset-0.73.7.tgz", - "integrity": "sha512-RKcmRZREjJCzHKP+JhC9QTCohkeb3xa/DtqHU14U5KWzJHdC0mMrkTZYNXhV0cryxsaVKVEw5873KhbZyZHMVw==", + "version": "0.73.9", + "resolved": "https://registry.npmjs.org/metro-react-native-babel-preset/-/metro-react-native-babel-preset-0.73.9.tgz", + "integrity": "sha512-AoD7v132iYDV4K78yN2OLgTPwtAKn0XlD2pOhzyBxiI8PeXzozhbKyPV7zUOJUPETj+pcEVfuYj5ZN/8+bhbCw==", "dependencies": { "@babel/core": "^7.20.0", "@babel/plugin-proposal-async-generator-functions": "^7.0.0", @@ -12181,54 +12212,6 @@ "@babel/core": "*" } }, - "node_modules/metro-react-native-babel-transformer/node_modules/metro-react-native-babel-preset": { - "version": "0.73.9", - "resolved": "https://registry.npmjs.org/metro-react-native-babel-preset/-/metro-react-native-babel-preset-0.73.9.tgz", - "integrity": "sha512-AoD7v132iYDV4K78yN2OLgTPwtAKn0XlD2pOhzyBxiI8PeXzozhbKyPV7zUOJUPETj+pcEVfuYj5ZN/8+bhbCw==", - "dependencies": { - "@babel/core": "^7.20.0", - "@babel/plugin-proposal-async-generator-functions": "^7.0.0", - "@babel/plugin-proposal-class-properties": "^7.0.0", - "@babel/plugin-proposal-export-default-from": "^7.0.0", - "@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0", - "@babel/plugin-proposal-object-rest-spread": "^7.0.0", - "@babel/plugin-proposal-optional-catch-binding": "^7.0.0", - "@babel/plugin-proposal-optional-chaining": "^7.0.0", - "@babel/plugin-syntax-dynamic-import": "^7.0.0", - "@babel/plugin-syntax-export-default-from": "^7.0.0", - "@babel/plugin-syntax-flow": "^7.18.0", - "@babel/plugin-syntax-nullish-coalescing-operator": "^7.0.0", - "@babel/plugin-syntax-optional-chaining": "^7.0.0", - "@babel/plugin-transform-arrow-functions": "^7.0.0", - "@babel/plugin-transform-async-to-generator": "^7.0.0", - "@babel/plugin-transform-block-scoping": "^7.0.0", - "@babel/plugin-transform-classes": "^7.0.0", - "@babel/plugin-transform-computed-properties": "^7.0.0", - "@babel/plugin-transform-destructuring": "^7.0.0", - "@babel/plugin-transform-flow-strip-types": "^7.0.0", - "@babel/plugin-transform-function-name": "^7.0.0", - "@babel/plugin-transform-literals": "^7.0.0", - "@babel/plugin-transform-modules-commonjs": "^7.0.0", - "@babel/plugin-transform-named-capturing-groups-regex": "^7.0.0", - "@babel/plugin-transform-parameters": "^7.0.0", - "@babel/plugin-transform-react-display-name": "^7.0.0", - "@babel/plugin-transform-react-jsx": "^7.0.0", - "@babel/plugin-transform-react-jsx-self": "^7.0.0", - "@babel/plugin-transform-react-jsx-source": "^7.0.0", - "@babel/plugin-transform-runtime": "^7.0.0", - "@babel/plugin-transform-shorthand-properties": "^7.0.0", - "@babel/plugin-transform-spread": "^7.0.0", - "@babel/plugin-transform-sticky-regex": "^7.0.0", - "@babel/plugin-transform-template-literals": "^7.0.0", - "@babel/plugin-transform-typescript": "^7.5.0", - "@babel/plugin-transform-unicode-regex": "^7.0.0", - "@babel/template": "^7.0.0", - "react-refresh": "^0.4.0" - }, - "peerDependencies": { - "@babel/core": "*" - } - }, "node_modules/metro-resolver": { "version": "0.73.9", "resolved": "https://registry.npmjs.org/metro-resolver/-/metro-resolver-0.73.9.tgz", @@ -12407,54 +12390,6 @@ "node": ">=8" } }, - "node_modules/metro/node_modules/metro-react-native-babel-preset": { - "version": "0.73.9", - "resolved": "https://registry.npmjs.org/metro-react-native-babel-preset/-/metro-react-native-babel-preset-0.73.9.tgz", - "integrity": "sha512-AoD7v132iYDV4K78yN2OLgTPwtAKn0XlD2pOhzyBxiI8PeXzozhbKyPV7zUOJUPETj+pcEVfuYj5ZN/8+bhbCw==", - "dependencies": { - "@babel/core": "^7.20.0", - "@babel/plugin-proposal-async-generator-functions": "^7.0.0", - "@babel/plugin-proposal-class-properties": "^7.0.0", - "@babel/plugin-proposal-export-default-from": "^7.0.0", - "@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0", - "@babel/plugin-proposal-object-rest-spread": "^7.0.0", - "@babel/plugin-proposal-optional-catch-binding": "^7.0.0", - "@babel/plugin-proposal-optional-chaining": "^7.0.0", - "@babel/plugin-syntax-dynamic-import": "^7.0.0", - "@babel/plugin-syntax-export-default-from": "^7.0.0", - "@babel/plugin-syntax-flow": "^7.18.0", - "@babel/plugin-syntax-nullish-coalescing-operator": "^7.0.0", - "@babel/plugin-syntax-optional-chaining": "^7.0.0", - "@babel/plugin-transform-arrow-functions": "^7.0.0", - "@babel/plugin-transform-async-to-generator": "^7.0.0", - "@babel/plugin-transform-block-scoping": "^7.0.0", - "@babel/plugin-transform-classes": "^7.0.0", - "@babel/plugin-transform-computed-properties": "^7.0.0", - "@babel/plugin-transform-destructuring": "^7.0.0", - "@babel/plugin-transform-flow-strip-types": "^7.0.0", - "@babel/plugin-transform-function-name": "^7.0.0", - "@babel/plugin-transform-literals": "^7.0.0", - "@babel/plugin-transform-modules-commonjs": "^7.0.0", - "@babel/plugin-transform-named-capturing-groups-regex": "^7.0.0", - "@babel/plugin-transform-parameters": "^7.0.0", - "@babel/plugin-transform-react-display-name": "^7.0.0", - "@babel/plugin-transform-react-jsx": "^7.0.0", - "@babel/plugin-transform-react-jsx-self": "^7.0.0", - "@babel/plugin-transform-react-jsx-source": "^7.0.0", - "@babel/plugin-transform-runtime": "^7.0.0", - "@babel/plugin-transform-shorthand-properties": "^7.0.0", - "@babel/plugin-transform-spread": "^7.0.0", - "@babel/plugin-transform-sticky-regex": "^7.0.0", - "@babel/plugin-transform-template-literals": "^7.0.0", - "@babel/plugin-transform-typescript": "^7.5.0", - "@babel/plugin-transform-unicode-regex": "^7.0.0", - "@babel/template": "^7.0.0", - "react-refresh": "^0.4.0" - }, - "peerDependencies": { - "@babel/core": "*" - } - }, "node_modules/metro/node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", diff --git a/package.json b/package.json index d24dd43..dae57cc 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ }, "dependencies": { "@expo/webpack-config": "^18.0.1", + "@react-native-async-storage/async-storage": "^1.18.1", "@react-navigation/drawer": "^6.6.2", "@react-navigation/native": "^6.1.6", "@reduxjs/toolkit": "^1.9.3", diff --git a/src/Components/Api/Api.tsx b/src/Components/Api/Api.tsx index 64c9490..e3120a1 100644 --- a/src/Components/Api/Api.tsx +++ b/src/Components/Api/Api.tsx @@ -1,4 +1,5 @@ import axios from "axios"; +import AsyncStorage from "@react-native-async-storage/async-storage"; import { ActivationParams, UpdateNoteParams, @@ -13,10 +14,10 @@ const instance = axios.create({ baseURL: "https://keannu126.pythonanywhere.com", }); -export function GetNotes() { - const token = JSON.parse(localStorage.getItem("token") || "{}"); +export async function GetNotes() { + const token = JSON.parse(await AsyncStorage.getItem("token") || "{}"); return instance - .get("api/v1/notes/", { + .get("/api/v1/notes/", { headers: { Authorization: "Token " + token, }, @@ -26,10 +27,16 @@ export function GetNotes() { }); } -export function GetNote(id: number) { - const token = JSON.parse(localStorage.getItem("token") || "{}"); +export function GetPublicNotes() { + return instance.get("/api/v1/public_notes/").then((response) => { + return response.data; + }); +} + +export async function GetNote(id: number) { + const token = JSON.parse(await AsyncStorage.getItem("token") || "{}"); return instance - .get("api/v1/notes/" + id + "/", { + .get("/api/v1/notes/" + id + "/", { headers: { Authorization: "Token " + token, }, @@ -39,10 +46,10 @@ export function GetNote(id: number) { }); } -export function UpdateNote(note: UpdateNoteParams) { - const token = JSON.parse(localStorage.getItem("token") || "{}"); +export async function UpdateNote(note: UpdateNoteParams) { + const token = JSON.parse(await AsyncStorage.getItem("token") || "{}"); return instance - .patch("api/v1/notes/" + note.id + "/", note, { + .patch("/api/v1/notes/" + note.id + "/", note, { headers: { Authorization: "Token " + token, }, @@ -51,15 +58,14 @@ export function UpdateNote(note: UpdateNoteParams) { return response.data; }) .catch((error) => { - console.log("Error updating note", error); return error; }); } -export function AddNote(note: AddNoteParams) { - const token = JSON.parse(localStorage.getItem("token") || "{}"); +export async function AddNote(note: AddNoteParams) { + const token = JSON.parse(await AsyncStorage.getItem("token") || "{}"); return instance - .post("api/v1/notes/", note, { + .post("/api/v1/notes/", note, { headers: { Authorization: "Token " + token, }, @@ -68,21 +74,19 @@ export function AddNote(note: AddNoteParams) { return response.data; }) .catch((error) => { - console.log("Error adding note", error); return error; }); } -export function DeleteNote(id: number) { - const token = JSON.parse(localStorage.getItem("token") || "{}"); +export async function DeleteNote(id: number) { + const token = JSON.parse(await AsyncStorage.getItem("token") || "{}"); return instance - .delete("api/v1/notes/" + id + "/", { + .delete("/api/v1/notes/" + id + "/", { headers: { Authorization: "Token " + token, }, }) .catch((error) => { - console.log("Error deleting note", error); return error; }); } @@ -91,26 +95,20 @@ export function DeleteNote(id: number) { export function UserRegister(register: RegistrationParams) { return instance - .post("api/v1/accounts/users/", register) + .post("/api/v1/accounts/users/", register) .then(async (response) => { - console.log(response.data); return true; }) .catch((error) => { - console.log("Registration failed: " + error); return false; }); } export function UserLogin(user: LoginParams) { return instance - .post("api/v1/accounts/token/login/", user) + .post("/api/v1/accounts/token/login/", user) .then(async (response) => { - localStorage.setItem("token", JSON.stringify(response.data.auth_token)); - console.log( - "Login Success! Stored Token: ", - JSON.parse(localStorage.getItem("token") || "{}") - ); + AsyncStorage.setItem("token", JSON.stringify(response.data.auth_token)); return true; }) .catch((error) => { @@ -119,29 +117,26 @@ export function UserLogin(user: LoginParams) { }); } -export function UserInfo() { - const token = JSON.parse(localStorage.getItem("token") || "{}"); +export async function UserInfo() { + const token = JSON.parse(await AsyncStorage.getItem("token") || "{}"); return instance - .get("api/v1/accounts/users/me/", { + .get("/api/v1/accounts/users/me/", { headers: { Authorization: "Token " + token, }, }) .then((response) => { - console.log(response.data); return response.data; }); } export function UserActivate(activation: ActivationParams) { return instance - .post("api/v1/accounts/users/activation/", activation) + .post("/api/v1/accounts/users/activation/", activation) .then(async (response) => { - console.log("Activation Success"); return true; }) .catch((error) => { - console.log("Activation failed: " + error); return false; }); } diff --git a/src/Features/Redux/Store/Store.tsx b/src/Features/Redux/Store/Store.tsx index 4f4da1a..063fd36 100644 --- a/src/Features/Redux/Store/Store.tsx +++ b/src/Features/Redux/Store/Store.tsx @@ -2,15 +2,9 @@ import { configureStore } from "@reduxjs/toolkit"; import LoginReducer from "../Slices/LoginSlice/LoginSlice"; import LoggedInUserReucer from "../Slices/LoggedInUserSlice/LoggedInUserSlice"; -const store = configureStore({ +export default configureStore({ reducer: { - logged_in: LoginReducer, - logged_in_user: LoggedInUserReucer, + Login: LoginReducer, + LoggedInUser: LoggedInUserReucer, }, }); - -export default store; - -// Infer the `RootState` and `AppDispatch` types from the store itself -export type RootState = ReturnType; -export type AppDispatch = typeof store.dispatch; \ No newline at end of file