Browse Source

Partial API frontend implementation

pull/6/head
DariusTFox 3 years ago
parent
commit
0e27defbdc
  1. 21
      client/src/App.svelte
  2. 12
      client/src/CreateAccount.svelte
  3. 8
      client/src/MainPage.svelte
  4. 125
      client/src/api.js

21
client/src/App.svelte

@ -1,6 +1,6 @@
<script>
import { onMount } from "svelte";
import { whoami } from "./api";
import { whoami, createnotification } from "./api";
import BottomBorder from "./BottomBorder.svelte";
import CheckNotifications from "./CheckNotifications.svelte";
@ -34,12 +34,19 @@ import { whoami } from "./api";
var today = new Date();
var date = today.getDate()+'/'+(today.getMonth()+1)+'/'+today.getFullYear();
var time = today.getHours() + ":" + today.getMinutes();
var body = "The "+ eventType.currency + " account with the name " + eventType.type + " was created succesfully!";
//add notification about created account
createnotification(async function() {
const token = sessionStorage.getItem("token");
const result = await createnotification(token, body, date+time);
if(result.status == "success") {
console.log("Succesfully created notification.");
}else{
console.log("Failed to create notification.");
}
})
notifications.push(
{
text: "The new account '" + event.detail.type+ "' was created successfully!",
time: time + " " + date,
});
isCreatingAccount = false;
break;
@ -48,7 +55,7 @@ import { whoami } from "./api";
break;
case "create_acc_failed":
isCreatingAccount = false;
// isCreatingAccount = false;
alert(`Account creation failed! Reason: ${event.detail.reason}`);
break;

12
client/src/CreateAccount.svelte

@ -8,6 +8,7 @@
import Overlay from "./Overlay.svelte";
import { fade, fly, slide } from 'svelte/transition';
import { flip } from 'svelte/animate';
import { createaccount } from "./api";
const dispatch = createEventDispatcher();
@ -26,8 +27,15 @@
}else if (!termsAccepted){
alert("Terms of Service not accepted!");
}else{
//TODO Create account with provided details on the server
dispatch("createPopup",{type:"create_acc_success", account:{type:type, currency:currency, transactions:[]}});
createaccount(async function() {
const token = sessionStorage.getItem("token");
const result = await createaccount(token, type, currency);
if(result.status == "success") {
dispatch("createPopup",{type:"create_acc_success", account:{type:type, currency:currency, transactions:[]}});
}else{
dispatch("createPopup",{type:"create_acc_failed", reason:"Failed to create account. Error:"+result.status});
}
})
}
}

8
client/src/MainPage.svelte

@ -6,7 +6,7 @@
import GreenButton from './GreenButton.svelte';
import { fade, fly, slide } from 'svelte/transition';
import { flip } from 'svelte/animate';
import { logout, whoami } from './api';
import { logout, whoami, getaccountlist } from './api';
const dispatch = createEventDispatcher();
@ -99,6 +99,12 @@
fullname = result.user.fullname;
email = result.user.email;
username = result.user.username;
//get the list of accounts
const accresult = await getaccountlist(username, token);
if(accresult == "success"){
accounts = accresult.user.accounts;
}
}
})

125
client/src/api.js

@ -54,4 +54,129 @@ export async function logout(token) {
code: "request/failure"
}
}
}
export async function getaccountlist(token) {
try {
const result = await fetch(new URL("/accounts", baseURL), {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token,
},
});
return (await result.json());
} catch (error) {
return {
status: "error",
code: "request/failure"
}
}
}
export async function getnotificationlist(token) {
try {
const result = await fetch(new URL("/notifications", baseURL), {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token,
},
});
return (await result.json());
} catch (error) {
return {
status: "error",
code: "request/failure"
}
}
}
export async function gettransactions(token, id) {
try {
const result = await fetch(new URL("/transactions?accountId="+id, baseURL), {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token,
},
});
return (await result.json());
} catch (error) {
return {
status: "error",
code: "request/failure"
}
}
}
export async function createaccount(token, name, currency) {
try {
const result = await fetch(new URL("/account/create", baseURL), {
method: "POST",
body: JSON.stringify({
name, currency,
}),
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token,
},
});
return (await result.json());
} catch (error) {
return {
status: "error",
code: "request/failure"
}
}
}
export async function createnotification(token, body, datetime) {
try {
const result = await fetch(new URL("/notification/create", baseURL), {
method: "POST",
body: JSON.stringify({
body, datetime,
}),
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token,
},
});
return (await result.json());
} catch (error) {
return {
status: "error",
code: "request/failure"
}
}
}
export async function createtransaction(token, otherparty, amount, type, ) {
try {
const result = await fetch(new URL("/transaction/create", baseURL), {
method: "POST",
body: JSON.stringify({
otherparty, amount, type,
}),
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token,
},
});
return (await result.json());
} catch (error) {
return {
status: "error",
code: "request/failure"
}
}
}
Loading…
Cancel
Save