2024-05-04 18:52:47 +07:00
|
|
|
import { apiUri } from '$lib/utils/common';
|
2024-05-04 15:32:42 +07:00
|
|
|
|
2024-05-31 16:28:21 +07:00
|
|
|
/**
|
|
|
|
* @typedef {import('@vincjo/datatables/remote').State} State
|
|
|
|
* @param {State} state - The state object from the data table.
|
|
|
|
*/
|
2024-05-04 18:52:47 +07:00
|
|
|
export const loadData = async (state) => {
|
|
|
|
const response = await fetch(apiUri(`/api/v1/nodes?${getParams(state)}`));
|
2024-05-04 15:32:42 +07:00
|
|
|
const json = await response.json();
|
2024-05-04 18:52:47 +07:00
|
|
|
state.setTotalRows(json.data.total_rows ?? 0);
|
|
|
|
return json.data.items ?? [];
|
|
|
|
};
|
2024-05-04 15:32:42 +07:00
|
|
|
|
2024-05-06 13:59:33 +07:00
|
|
|
export const loadCountries = async () => {
|
2024-05-28 10:03:29 +07:00
|
|
|
const response = await fetch(apiUri('/api/v1/countries'));
|
|
|
|
const json = await response.json();
|
|
|
|
return json.data ?? [];
|
2024-05-06 13:59:33 +07:00
|
|
|
};
|
|
|
|
|
2024-05-31 16:28:21 +07:00
|
|
|
export const loadFees = async () => {
|
|
|
|
const response = await fetch(apiUri('/api/v1/fees'));
|
|
|
|
const json = await response.json();
|
|
|
|
return json.data ?? [];
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @param {State} state - The state object from the data table. */
|
2024-05-04 18:52:47 +07:00
|
|
|
const getParams = ({ pageNumber, rowsPerPage, sort, filters }) => {
|
2024-05-04 15:32:42 +07:00
|
|
|
let params = `page=${pageNumber}&limit=${rowsPerPage}`;
|
2024-05-04 18:52:47 +07:00
|
|
|
|
2024-05-04 15:32:42 +07:00
|
|
|
if (sort) {
|
2024-05-04 18:52:47 +07:00
|
|
|
params += `&sort_by=${sort.orderBy}&sort_direction=${sort.direction}`;
|
2024-05-04 15:32:42 +07:00
|
|
|
}
|
|
|
|
if (filters) {
|
|
|
|
params += filters.map(({ filterBy, value }) => `&${filterBy}=${value}`).join('');
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
};
|