mirror of
https://github.com/dancojocaru2000/foxbank.git
synced 2025-02-22 23:39:36 +02:00
Partial API frontend implementation
This commit is contained in:
parent
b44a48f241
commit
0e27defbdc
4 changed files with 156 additions and 10 deletions
|
@ -1,6 +1,6 @@
|
||||||
<script>
|
<script>
|
||||||
import { onMount } from "svelte";
|
import { onMount } from "svelte";
|
||||||
import { whoami } from "./api";
|
import { whoami, createnotification } from "./api";
|
||||||
|
|
||||||
import BottomBorder from "./BottomBorder.svelte";
|
import BottomBorder from "./BottomBorder.svelte";
|
||||||
import CheckNotifications from "./CheckNotifications.svelte";
|
import CheckNotifications from "./CheckNotifications.svelte";
|
||||||
|
@ -34,12 +34,19 @@ import { whoami } from "./api";
|
||||||
var today = new Date();
|
var today = new Date();
|
||||||
var date = today.getDate()+'/'+(today.getMonth()+1)+'/'+today.getFullYear();
|
var date = today.getDate()+'/'+(today.getMonth()+1)+'/'+today.getFullYear();
|
||||||
var time = today.getHours() + ":" + today.getMinutes();
|
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;
|
isCreatingAccount = false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -48,7 +55,7 @@ import { whoami } from "./api";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "create_acc_failed":
|
case "create_acc_failed":
|
||||||
isCreatingAccount = false;
|
// isCreatingAccount = false;
|
||||||
alert(`Account creation failed! Reason: ${event.detail.reason}`);
|
alert(`Account creation failed! Reason: ${event.detail.reason}`);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
import Overlay from "./Overlay.svelte";
|
import Overlay from "./Overlay.svelte";
|
||||||
import { fade, fly, slide } from 'svelte/transition';
|
import { fade, fly, slide } from 'svelte/transition';
|
||||||
import { flip } from 'svelte/animate';
|
import { flip } from 'svelte/animate';
|
||||||
|
import { createaccount } from "./api";
|
||||||
|
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
const dispatch = createEventDispatcher();
|
||||||
|
@ -26,8 +27,15 @@
|
||||||
}else if (!termsAccepted){
|
}else if (!termsAccepted){
|
||||||
alert("Terms of Service not accepted!");
|
alert("Terms of Service not accepted!");
|
||||||
}else{
|
}else{
|
||||||
//TODO Create account with provided details on the server
|
createaccount(async function() {
|
||||||
dispatch("createPopup",{type:"create_acc_success", account:{type:type, currency:currency, transactions:[]}});
|
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});
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
import GreenButton from './GreenButton.svelte';
|
import GreenButton from './GreenButton.svelte';
|
||||||
import { fade, fly, slide } from 'svelte/transition';
|
import { fade, fly, slide } from 'svelte/transition';
|
||||||
import { flip } from 'svelte/animate';
|
import { flip } from 'svelte/animate';
|
||||||
import { logout, whoami } from './api';
|
import { logout, whoami, getaccountlist } from './api';
|
||||||
|
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
const dispatch = createEventDispatcher();
|
||||||
|
@ -99,6 +99,12 @@
|
||||||
fullname = result.user.fullname;
|
fullname = result.user.fullname;
|
||||||
email = result.user.email;
|
email = result.user.email;
|
||||||
username = result.user.username;
|
username = result.user.username;
|
||||||
|
|
||||||
|
//get the list of accounts
|
||||||
|
const accresult = await getaccountlist(username, token);
|
||||||
|
if(accresult == "success"){
|
||||||
|
accounts = accresult.user.accounts;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -54,4 +54,129 @@ export async function logout(token) {
|
||||||
code: "request/failure"
|
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…
Add table
Reference in a new issue