Skip to content

Commit

Permalink
Websockets support
Browse files Browse the repository at this point in the history
  • Loading branch information
isKONSTANTIN committed Jul 23, 2024
1 parent 9642e10 commit cd0cd58
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 17 deletions.
8 changes: 7 additions & 1 deletion components/analytics/pie.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const props = defineProps({
}
})
const { $transactionsTagsApi, $analyticsApi, $currenciesApi } = useNuxtApp();
const { $transactionsTagsApi, $analyticsApi, $transactionsApi, $currenciesApi } = useNuxtApp();
const { t, locale } = useI18n();
const mode = computed(() => props.mode === "month")
Expand Down Expand Up @@ -136,6 +136,12 @@ const reloadAnalytics = async () => {
));
}
$transactionsApi.registerUpdateListener(() => {
reloadAnalytics().then(() => {
buildChart();
})
})
const formatAmount = (delta) => {
const currencyObject = currenciesMap.value.get(currency.value);
Expand Down
4 changes: 3 additions & 1 deletion components/analytics/tagsByMonths.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import ApexChart from "vue3-apexcharts";
import TransactionsFilterDiv from "~/components/transactions/filter.vue";
import {useColor} from "~/composables/useColor";
const { $analyticsApi, $transactionsTagsApi, $currenciesApi, $serverConfigs } = useNuxtApp();
const { $analyticsApi, $transactionsApi, $transactionsTagsApi, $currenciesApi, $serverConfigs } = useNuxtApp();
const { t, locale } = useI18n();
const tagsMap = $transactionsTagsApi.getTagsMap();
Expand All @@ -46,6 +46,8 @@ const fetch = async () => {
analytics.value = new Map(Array.from(analyticsRaw).sort(([a], [b]) => a.localeCompare(b)));
}
$transactionsApi.registerUpdateListener(fetch)
const buildChart = async () => {
const xaxis = [];
const series = [];
Expand Down
4 changes: 3 additions & 1 deletion components/analytics/totalByPeriod.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import ApexChart from "vue3-apexcharts";
import {TransactionsFilter} from "~/libs/api/transactions/TransactionsFilter";
const { $analyticsApi, $transactionsTagsApi, $currenciesApi, $serverConfigs } = useNuxtApp();
const { $analyticsApi, $transactionsApi, $transactionsTagsApi, $currenciesApi, $serverConfigs } = useNuxtApp();
const { t, locale } = useI18n();
const props = defineProps({
Expand Down Expand Up @@ -122,6 +122,8 @@ watch([() => props.currencyId,() => props.period], () => {
buildChart();
});
$transactionsApi.registerUpdateListener(buildChart)
buildChart();
</script>
Expand Down
67 changes: 67 additions & 0 deletions composables/useApiLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {ConfigManager} from "~/libs/config/ConfigManager";
import {ServerConfigs} from "~/libs/config/ServerConfigs";
import {AccumulationsApi} from "~/libs/api/accumulations/AccumulationsApi";
import {ReportsApi} from "~/libs/api/reports/ReportsApi";
import {ca} from "date-fns/locale";

export const useApiLoader = new class ApiLoader {
private readonly serverConfigs: ConfigManager;
Expand All @@ -38,6 +39,8 @@ export const useApiLoader = new class ApiLoader {
private readonly accumulationsApi: AccumulationsApi;
private readonly reportsApi: ReportsApi;

private websocketClient : WebSocket | null = null;

constructor() {
this.serverConfigs = new ConfigManager();
this.userApi = new UserApi();
Expand Down Expand Up @@ -93,6 +96,70 @@ export const useApiLoader = new class ApiLoader {
});
}

public connectWebsocket() : void {
const config = useRuntimeConfig()
const auth = useNuxtApp().$auth.state();

this.websocketClient = new WebSocket(config.public.apiURL
.replace("https://", "wss://")
.replace("http://", "ws://") +
"websockets/events"
);

this.websocketClient.onmessage = (event) => {
this.parseMessage(JSON.parse(event.data))

console.log(event.data);
};

this.websocketClient.addEventListener("open", e =>{
this.websocketClient.send(JSON.stringify({
type: "auth",
body: {
token: auth.token
}
}))
})
}

protected parseMessage(message : any) {
if (message.type !== "update")
return;

const body = message.body;

switch (body.updated) {
case "accounts":
this.accountsApi.reloadAccounts();
break;
case "accountTags":
this.accountsTagsApi.fetch();
break;
case "accumulation":
this.accumulationsApi.fetchMap();
break;
case "currencies":
this.currenciesApi.fetch();
break;
case "notes":
this.notesApi.updateNotify();
break;
case "transactions":
this.accountsApi.reloadAccounts();
this.transactionsApi.updateNotify();
break;
case "reports":
this.reportsApi.updateNotify();
break;
case "recurringTransactions":
this.recurringTransactionsApi.fetch();
break;
case "transactionTags":
this.accountsTagsApi.fetch();
break;
}
}

public getProvider() : any {
return {
provide: {
Expand Down
12 changes: 8 additions & 4 deletions libs/api/accounts/tags/AccountsTagsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ export class AccountsTagsApi extends AbstractApi {
private tags: Ref<Array<any>> = ref([]);

async init(): Promise<void | boolean> {
await this.fetch();
}

public getTags(): Ref<Array<any> | null> {
return this.tags;
}

public async fetch(): Promise<void> {
const {data} = await useApi<any>("/user/accounts/tags/getList");

if (data.value === null)
Expand All @@ -13,10 +21,6 @@ export class AccountsTagsApi extends AbstractApi {
this.tags.value = data.value.tags || [];
}

public getTags(): Ref<Array<any> | null> {
return this.tags;
}

public async newTag(name: string, description: string | null) : Promise<number> {
const opts = {
method: "POST",
Expand Down
2 changes: 1 addition & 1 deletion libs/api/accumulations/AccumulationsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class AccumulationsApi extends AbstractApi {
await this.fetchMap();
}

protected async fetchMap() : Promise<void> {
public async fetchMap() : Promise<void> {
const {data} = await useApi<any>("/user/accumulations/getList");

if (data.value === null)
Expand Down
4 changes: 4 additions & 0 deletions libs/api/currencies/CurrenciesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export class CurrenciesApi extends AbstractApi {
private currenciesMap: Ref<Map<number, any>> = ref(new Map<number, any>);

async init(): Promise<void | boolean> {
await this.fetch();
}

public async fetch() : Promise<void> {
const {data} = await useApi<any>("/user/currencies/getList");

if (data.value === null)
Expand Down
10 changes: 10 additions & 0 deletions libs/api/notes/NotesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ import {Ref} from "vue";
import {AbstractApi} from "~/libs/api/AbstractApi";

export class NotesApi extends AbstractApi {
private notifyListeners : Array<Function> = new Array<Function>();

async init(): Promise<void | boolean> {

}

public updateNotify() {
this.notifyListeners.forEach(func => func());
}

public registerUpdateListener(func : Function) : void {
this.notifyListeners.push(func);
}

public async newNote(text: string, notificationTime: Date | null): Promise<boolean> {
const opts = {
method: "POST",
Expand Down
9 changes: 9 additions & 0 deletions libs/api/reports/ReportsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import {Ref} from "vue";
import {TransactionsFilter} from "~/libs/api/transactions/TransactionsFilter";

export class ReportsApi extends AbstractApi {
private notifyListeners : Array<Function> = new Array<Function>();

public updateNotify() {
this.notifyListeners.forEach(func => func());
}

public registerUpdateListener(func : Function) : void {
this.notifyListeners.push(func);
}

async init(): Promise<void | boolean> {
}
Expand Down
10 changes: 10 additions & 0 deletions libs/api/transactions/TransactionsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ import {TransactionsFilter} from "~/libs/api/transactions/TransactionsFilter";
import {AbstractApi} from "~/libs/api/AbstractApi";

export class TransactionsApi extends AbstractApi {
private notifyListeners : Array<Function> = new Array<Function>();

async init(): Promise<void | boolean> {

}

public updateNotify() {
this.notifyListeners.forEach(func => func());
}

public registerUpdateListener(func : Function) : void {
this.notifyListeners.push(func);
}

public async newBulkTransaction(transactions : any) : Promise<boolean> {
const opts = {
method: "POST",
Expand Down
4 changes: 4 additions & 0 deletions libs/api/transactions/recurring/RecurringTransactionsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export class RecurringTransactionsApi extends AbstractApi {
}

async init(): Promise<void | boolean> {
await this.fetch();
}

public async fetch(): Promise<void> {
const {data} = await useApi<any>("/user/transactions/recurring/getList");

if (data.value === null)
Expand Down
14 changes: 8 additions & 6 deletions libs/api/transactions/tag/TransactionsTagsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ export class TransactionsTagsApi extends AbstractApi {
private tagsTree: Ref<Array<any>> = ref([]);

async init(): Promise<void | boolean> {
watch(this.tags, (old, newV) => {
this.buildTree()
}, { deep: true })

await this.fetch();
}

public async fetch() : Promise<void> {
const {data} = await useApi<any>("/user/transactions/tags/getList");

if (data.value === null)
return;

this.tags.value = data.value.tags || [];

watch(this.tags, (old, newV) => {
this.buildTree()
}, { deep: true })

this.buildTree();
}

private rebuildMap() {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "finwave",
"version": "0.12.0",
"version": "0.13.0",
"private": true,
"scripts": {
"build": "nuxt build",
Expand Down
2 changes: 2 additions & 0 deletions pages/transactions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ const fetchData = async () => {
.catch(console.log);
}
$transactionsApi.registerUpdateListener(fetchData)
const changePage = async (pageNeed) => {
if (pageNeed < 1 || pageNeed > maxPages.value || pageNeed === page.value)
return;
Expand Down
2 changes: 2 additions & 0 deletions plugins/2.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ import {useApiLoader} from "#imports";
export default defineNuxtPlugin(async nuxtApp => {
await useApiLoader.fetch();

useApiLoader.connectWebsocket();

return useApiLoader.getProvider();
})

0 comments on commit cd0cd58

Please sign in to comment.