mirror of
https://github.com/lemeow125/Reactnative-notesapp.git
synced 2025-06-28 16:35:44 +08:00
Merge commit '958e62d733
' into feature/initial_frontend
This commit is contained in:
commit
7388b851d5
11 changed files with 5071 additions and 641 deletions
142
src/Components/Api/Api.tsx
Normal file
142
src/Components/Api/Api.tsx
Normal file
|
@ -0,0 +1,142 @@
|
|||
import axios from "axios";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import {
|
||||
ActivationParams,
|
||||
UpdateNoteParams,
|
||||
AddNoteParams,
|
||||
LoginParams,
|
||||
RegistrationParams,
|
||||
} from "../../Interfaces/Interfaces";
|
||||
|
||||
// Note APIs
|
||||
|
||||
const instance = axios.create({
|
||||
baseURL: "https://keannu126.pythonanywhere.com",
|
||||
});
|
||||
|
||||
export async function GetNotes() {
|
||||
const token = JSON.parse(await AsyncStorage.getItem("token") || "{}");
|
||||
return instance
|
||||
.get("/api/v1/notes/", {
|
||||
headers: {
|
||||
Authorization: "Token " + token,
|
||||
},
|
||||
})
|
||||
.then((response) => {
|
||||
return response.data;
|
||||
});
|
||||
}
|
||||
|
||||
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 + "/", {
|
||||
headers: {
|
||||
Authorization: "Token " + token,
|
||||
},
|
||||
})
|
||||
.then((response) => {
|
||||
return response.data;
|
||||
});
|
||||
}
|
||||
|
||||
export async function UpdateNote(note: UpdateNoteParams) {
|
||||
const token = JSON.parse(await AsyncStorage.getItem("token") || "{}");
|
||||
return instance
|
||||
.patch("/api/v1/notes/" + note.id + "/", note, {
|
||||
headers: {
|
||||
Authorization: "Token " + token,
|
||||
},
|
||||
})
|
||||
.then((response) => {
|
||||
return response.data;
|
||||
})
|
||||
.catch((error) => {
|
||||
return error;
|
||||
});
|
||||
}
|
||||
|
||||
export async function AddNote(note: AddNoteParams) {
|
||||
const token = JSON.parse(await AsyncStorage.getItem("token") || "{}");
|
||||
return instance
|
||||
.post("/api/v1/notes/", note, {
|
||||
headers: {
|
||||
Authorization: "Token " + token,
|
||||
},
|
||||
})
|
||||
.then((response) => {
|
||||
return response.data;
|
||||
})
|
||||
.catch((error) => {
|
||||
return error;
|
||||
});
|
||||
}
|
||||
|
||||
export async function DeleteNote(id: number) {
|
||||
const token = JSON.parse(await AsyncStorage.getItem("token") || "{}");
|
||||
return instance
|
||||
.delete("/api/v1/notes/" + id + "/", {
|
||||
headers: {
|
||||
Authorization: "Token " + token,
|
||||
},
|
||||
})
|
||||
.catch((error) => {
|
||||
return error;
|
||||
});
|
||||
}
|
||||
|
||||
// User APIs
|
||||
|
||||
export function UserRegister(register: RegistrationParams) {
|
||||
return instance
|
||||
.post("/api/v1/accounts/users/", register)
|
||||
.then(async (response) => {
|
||||
return true;
|
||||
})
|
||||
.catch((error) => {
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
export function UserLogin(user: LoginParams) {
|
||||
return instance
|
||||
.post("/api/v1/accounts/token/login/", user)
|
||||
.then(async (response) => {
|
||||
AsyncStorage.setItem("token", JSON.stringify(response.data.auth_token));
|
||||
return true;
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("Login Failed: " + error);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
export async function UserInfo() {
|
||||
const token = JSON.parse(await AsyncStorage.getItem("token") || "{}");
|
||||
return instance
|
||||
.get("/api/v1/accounts/users/me/", {
|
||||
headers: {
|
||||
Authorization: "Token " + token,
|
||||
},
|
||||
})
|
||||
.then((response) => {
|
||||
return response.data;
|
||||
});
|
||||
}
|
||||
|
||||
export function UserActivate(activation: ActivationParams) {
|
||||
return instance
|
||||
.post("/api/v1/accounts/users/activation/", activation)
|
||||
.then(async (response) => {
|
||||
return true;
|
||||
})
|
||||
.catch((error) => {
|
||||
return false;
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue