added rates to currencies

This commit is contained in:
FIRST_NAME LAST_NAME 2025-01-22 21:50:35 +03:00
parent 27b37d8b58
commit e41509fc2a

View File

@ -1,210 +1,209 @@
<script> <script>
// @ts-nocheck // @ts-nocheck
import { browser } from "$app/environment"; import { browser } from "$app/environment";
import { page } from "$app/stores"; import { page } from "$app/stores";
import { getAuthInfo, makeAuthHeaderForAxios } from "$lib/auth/Auth"; import { getAuthInfo, makeAuthHeaderForAxios } from "$lib/auth/Auth";
import { makePost } from "$lib/tools/requests/requests"; import { makePost } from "$lib/tools/requests/requests";
import { sayError } from "$lib/tools/toaster/Toaster"; import { sayError } from "$lib/tools/toaster/Toaster";
import Pagination from "$lib/ui-components/pagination.svelte"; import Pagination from "$lib/ui-components/pagination.svelte";
import { el } from "date-fns/locale"; import { el } from "date-fns/locale";
let currenciesCurrentPage = 1; let currenciesCurrentPage = 1;
let currenciesMaxPage = 1; let currenciesMaxPage = 1;
let currenciesFilter = "-1"; let currenciesFilter = "-1";
let showCurrenciesLoading = false; let showCurrenciesLoading = false;
let currencies = []; let currencies = [];
async function getCurrencies() { let rates = {};
showCurrenciesLoading = true; async function getCurrencies() {
const res = await makePost( showCurrenciesLoading = true;
"admin/loadCurrencies", const res = await makePost(
{ "admin/loadCurrencies",
page: currenciesCurrentPage, {
status: page: currenciesCurrentPage,
Number(currenciesFilter) > -1 status:
? Number(currenciesFilter) Number(currenciesFilter) > -1 ? Number(currenciesFilter) : undefined,
: undefined, },
}, makeAuthHeaderForAxios(getAuthInfo()?.a)
makeAuthHeaderForAxios(getAuthInfo()?.a), );
); // console.log(res);
// console.log(res); if (res.error) {
if (res.error) { sayError("Не удалось загрузить валюты");
sayError("Не удалось загрузить валюты"); console.error(
console.error(
{
page: currenciesCurrentPage,
status:
Number(currenciesFilter) > -1
? Number(currenciesFilter)
: undefined,
},
"- ERROR",
);
showCurrenciesLoading = false;
return;
}
currencies = res.data.data ?? [];
currenciesMaxPage = Number(res.data.pages);
showCurrenciesLoading = false;
}
if (browser) {
getCurrencies();
}
$: if (currenciesFilter.length > 0) {
console.log(currenciesFilter);
currenciesCurrentPage = 1;
getCurrencies();
}
let selectedCurrency = {};
let showChangeCurrency = false;
let showChangeCurrencyLoading = false;
let newBankValue = "";
let newVolumeValue = "";
async function changeCurrency() {
if (newBankValue.length < 1 && newVolumeValue.length < 1) {
sayError("Укажите хотябы одно изменение");
return;
}
showChangeCurrencyLoading = true;
const res = await makePost("admin/updateCurrency", {
bank: newBankValue.length >= 1 ? newBankValue:undefined,
volume: newVolumeValue.length > 1 ? newVolumeValue:undefined,
code: selectedCurrency['code']
}, makeAuthHeaderForAxios(getAuthInfo()?.a));
if(res.error)
{ {
sayError("Не удалось обновить валюту"); page: currenciesCurrentPage,
showChangeCurrencyLoading = false; status:
return; Number(currenciesFilter) > -1
} ? Number(currenciesFilter)
: undefined,
showChangeCurrencyLoading = false; },
showChangeCurrency = false; "- ERROR"
newBankValue = ""; );
newVolumeValue = ""; showCurrenciesLoading = false;
getCurrencies(); return;
} }
currencies = res.data.data ?? [];
currenciesMaxPage = Number(res.data.pages);
showCurrenciesLoading = false;
rates = res.data["rates"];
}
if (browser) {
getCurrencies();
}
$: if (currenciesFilter.length > 0) {
console.log(currenciesFilter);
currenciesCurrentPage = 1;
getCurrencies();
}
let selectedCurrency = {};
let showChangeCurrency = false;
let showChangeCurrencyLoading = false;
let newBankValue = "";
let newVolumeValue = "";
async function changeCurrency() {
if (newBankValue.length < 1 && newVolumeValue.length < 1) {
sayError("Укажите хотябы одно изменение");
return;
}
showChangeCurrencyLoading = true;
const res = await makePost(
"admin/updateCurrency",
{
bank: newBankValue.length >= 1 ? newBankValue : undefined,
volume: newVolumeValue.length > 1 ? newVolumeValue : undefined,
code: selectedCurrency["code"],
},
makeAuthHeaderForAxios(getAuthInfo()?.a)
);
if (res.error) {
sayError("Не удалось обновить валюту");
showChangeCurrencyLoading = false;
return;
}
showChangeCurrencyLoading = false;
showChangeCurrency = false;
newBankValue = "";
newVolumeValue = "";
getCurrencies();
}
</script> </script>
<div class="w-full flex flex-col gap-8"> <div class="w-full flex flex-col gap-8">
<div class="flex gap-4"> <div class="flex gap-4">
<div class="bg-accent rounded-[4px] w-[10px] h-full"></div> <div class="bg-accent rounded-[4px] w-[10px] h-full"></div>
<h1 class="text-2xl font-semibold">Валюты</h1> <h1 class="text-2xl font-semibold">Валюты</h1>
</div>
<div class="flex w-full flex-col bg-base-300 p-4 rounded-box overflow-x-auto">
<div class="flex justify-center items-center p-4">
<Pagination
pageChangedCallback={(n) => {
currenciesCurrentPage = n;
getCurrencies();
}}
totalPages={currenciesMaxPage}
css={"btn-neutral"}
disableButtons={showCurrenciesLoading}
/>
</div> </div>
<div <div class="flex justify-center items-center p-2">
class="flex w-full flex-col bg-base-300 p-4 rounded-box overflow-x-auto" <select bind:value={currenciesFilter} class="select select-bordered">
> <option value="-1">Все</option>
<div class="flex justify-center items-center p-4"> <option value="0">Активные</option>
<Pagination <option value="1">Неактивные</option>
pageChangedCallback={(n) => { </select>
currenciesCurrentPage = n;
getCurrencies();
}}
totalPages={currenciesMaxPage}
css={"btn-neutral"}
disableButtons={showCurrenciesLoading}
/>
</div>
<div class="flex justify-center items-center p-2">
<select
bind:value={currenciesFilter}
class="select select-bordered"
>
<option value="-1">Все</option>
<option value="0">Активные</option>
<option value="1">Неактивные</option>
</select>
</div>
{#if showCurrenciesLoading}
<span class="loading self-center mt-4"></span>
{:else}
<table class="table">
<thead>
<tr>
<th>Код</th>
<th>Номер (ISO)</th>
<th>Страна</th>
<th>Статус</th>
<th>Minor Units</th>
<th>Приоритетный банк (Bybit)</th>
<th>Объем</th>
<th>ID</th>
<th></th>
</tr>
</thead>
<tbody>
{#each currencies as cur}
<tr class="hover">
<td>{cur["code"]}</td>
<td>{cur["num"]}</td>
<td>{cur["country"]}</td>
<td
class={cur["is_active"] === "t"
? "text-success"
: "text-error"}
>{cur["is_active"] === "t"
? "Активна"
: "Неактивна"}</td
>
<td>{cur["minor_unit"]}</td>
<td>{cur["priority_bank_bybit"]}</td>
<td>{cur["volume"]}</td>
<td>{cur["id"]}</td>
<td>
<button
on:click={() => {
selectedCurrency = cur;
showChangeCurrency = true;
}}
class="btn btn-outline">Изменить</button
>
</td>
</tr>
{/each}
</tbody>
</table>
{/if}
</div> </div>
{#if showCurrenciesLoading}
<span class="loading self-center mt-4"></span>
{:else}
<table class="table">
<thead>
<tr>
<th>Код</th>
<th>Номер (ISO)</th>
<th>Страна</th>
<th>Статус</th>
<th>Minor Units</th>
<th>Приоритетный банк (Bybit)</th>
<th>Курс</th>
<th>Объем</th>
<th>ID</th>
<th></th>
</tr>
</thead>
<tbody>
{#each currencies as cur}
<tr class="hover">
<td>{cur["code"]}</td>
<td>{cur["num"]}</td>
<td>{cur["country"]}</td>
<td
class={cur["is_active"] === "t" ? "text-success" : "text-error"}
>{cur["is_active"] === "t" ? "Активна" : "Неактивна"}</td
>
<td>{cur["minor_unit"]}</td>
<td>{cur["priority_bank_bybit"]}</td>
<td>{rates[cur["code"]] ?? ""}</td>
<td>{cur["volume"]}</td>
<td>{cur["id"]}</td>
<td>
<button
on:click={() => {
selectedCurrency = cur;
showChangeCurrency = true;
}}
class="btn btn-outline">Изменить</button
>
</td>
</tr>
{/each}
</tbody>
</table>
{/if}
</div>
</div> </div>
{#if showChangeCurrency} {#if showChangeCurrency}
<div <div
class="fixed inset-0 bg-black bg-opacity-50 flex justify-center items-center" class="fixed inset-0 bg-black bg-opacity-50 flex justify-center items-center"
> >
<div class="flex flex-col p-4 bg-base-300 rounded-md"> <div class="flex flex-col p-4 bg-base-300 rounded-md">
<p class="font-bold">Изменение валюты {selectedCurrency["code"]}</p> <p class="font-bold">Изменение валюты {selectedCurrency["code"]}</p>
<p class="text-sm mt-4">Банк</p> <p class="text-sm mt-4">Банк</p>
<input <input
bind:value={newBankValue} bind:value={newBankValue}
type="text" type="text"
class="input input-bordered" class="input input-bordered"
/> />
<p class="mt-2 text-sm">Объем</p> <p class="mt-2 text-sm">Объем</p>
<input <input
bind:value={newVolumeValue} bind:value={newVolumeValue}
type="text" type="text"
class="input input-bordered" class="input input-bordered"
/> />
<button on:click={()=>{ <button
changeCurrency(); on:click={() => {
}} class="btn btn-outline btn-success mt-4"> changeCurrency();
Сохранить }}
</button> class="btn btn-outline btn-success mt-4"
<button >
on:click={() => { Сохранить
showChangeCurrency = false; </button>
}} <button
class="btn btn-outline mt-1">Закрыть</button on:click={() => {
> showChangeCurrency = false;
</div> }}
class="btn btn-outline mt-1">Закрыть</button
>
</div> </div>
</div>
{/if} {/if}