Skip to content

Commit

Permalink
Merge pull request #42 from ra4nd0m/feat-improvements
Browse files Browse the repository at this point in the history
Feat improvements
  • Loading branch information
ra4nd0m authored Sep 1, 2024
2 parents 13904e1 + 28de70b commit 2b53309
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 104 deletions.
3 changes: 2 additions & 1 deletion src/pages/components/AddRecord.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
}
</script>

<RecordsDisplay {mat_id} {secret} bShowLastRecords={true} />

<div class="d-flex justify-content-center">
<form
on:submit|preventDefault={async () => {
Expand Down Expand Up @@ -134,3 +134,4 @@
</div>
</form>
</div>
<RecordsDisplay {mat_id} {secret} />
26 changes: 18 additions & 8 deletions src/pages/components/GetMaterialsList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { materials_data } from "../lib/stores";
import { onMount } from "svelte";
export let secret: string;
let specialIds = [26, 27, 28];
onMount(async () => {
await grabData(secret);
});
Expand Down Expand Up @@ -36,14 +37,24 @@
const today = new Date().toISOString().split("T")[0];
let updateDatesArr = [];
for (let matObj of mat_list) {
let paylad = {
finish: today,
material_source_id: matObj.Id,
n_values: 1,
property_id: 1,
};
let payload = {};
if (specialIds.includes(matObj.Id)) {
payload = {
finish: today,
material_source_id: matObj.Id,
n_values: 1,
property_id: 6,
};
} else {
payload = {
finish: today,
material_source_id: matObj.Id,
n_values: 1,
property_id: 1,
};
}
const res = (await doFetch(
JSON.stringify(paylad),
JSON.stringify(payload),
"/getNLastValues",
secret,
).then((val) => {
Expand All @@ -67,4 +78,3 @@
}}
class="btn btn-primary">Обновить список материалов</button
>

7 changes: 0 additions & 7 deletions src/pages/components/MaterialList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,6 @@
mat_id={row.Id}
/>
{/if}
{#if selectedMenu == "showRecords"}
<RecordsDisplay
{secret}
mat_id={row.Id}
bShowLastRecords={false}
/>
{/if}
</SubmenuSwitch>
</td>
</tr>
Expand Down
116 changes: 33 additions & 83 deletions src/pages/components/RecordsDisplay.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import Flatpickr from "svelte-flatpickr";
export let mat_id: number;
export let secret: string;
export let bShowLastRecords: boolean;
let dates: string;
let start_date: string;
let finish_date = "";
let propList: matProp[] = [];
let isTableFolded = false;
let monthsAgo = 3;
let isTableFolded = true;
let monthsAgo = 1;
let fetchFired = false;
let isAvgChecked = true;
let isAvgChecked = false;
let maxIndexArr: any[] = [];
// Fetch the list of properties when the component mounts
onMount(async () => {
Expand All @@ -28,45 +27,23 @@
(val) => (val.isSelected = preheckdProps.includes(val.Name)),
);
fetchFired = true;
if (bShowLastRecords) {
recalcDates();
await getAllRecords();
}
});
function recalcDates() {
const today = new Date();
finish_date = today.toISOString().split("T")[0];
start_date = new Date(today.setMonth(today.getMonth() - monthsAgo))
.toISOString()
.split("T")[0];
}
$: {
isAvgChecked;
if (fetchFired && bShowLastRecords && monthsAgo && propList) {
recalcDates();
getAllRecords();
}
}
function extractDates(dates: string) {
const buf = dates.split(" ");
start_date = buf[0];
finish_date = buf[2];
}
let dataList: dataListToDisplay[] = [];
let preheckdProps = ["Средняя цена", "Прогноз месяц"];
let preheckdProps = ["Средняя цена", "Мин цена", "Макс цена", "Запас"];
//Not good
//Backend support should work better
async function getAllRecords() {
// Extract the start and finish dates from the dates string
let initialData: dateValuePair[][] = [];
let isFetchAttempted = false;
if (!bShowLastRecords) {
extractDates(dates);
}
extractDates(dates);
// Loop over each property in the propList
for (const prop of propList) {
//Check for bad props and skip if found
Expand Down Expand Up @@ -181,6 +158,7 @@
});
//Create an empty array with length of maxReachedIndex to interate over when table is being rendered
maxIndexArr = Array.from({ length: maxReachedIndex });
isTableFolded = false;
}
function toggleTableFold() {
Expand All @@ -190,11 +168,7 @@
$: {
propList;
isAvgChecked;
if (
start_date !== "" &&
typeof start_date !== "undefined" &&
!bShowLastRecords
) {
if (start_date !== "" && typeof start_date !== "undefined") {
getAllRecords();
}
}
Expand Down Expand Up @@ -243,51 +217,35 @@
{/if}
</div>
</div>
{#if !bShowLastRecords}
<div class="d-flex justify-content-center">
<div class="ms-3 mt-3">
<Flatpickr
options={{ enableTime: false, mode: "range" }}
bind:formattedValue={dates}
class="form-control"
placeholder="Дата"
/>
</div>
<div class="ms-3 mt-3">
<button
class="btn btn-primary"
disabled={!dateFilled}
on:click={async () => {
await getAllRecords();
}}>Загрузить</button
>
</div>
</div>
{/if}
{#if bShowLastRecords}
<div class="d-flex justify-content-center">
<label class="form-label" for="monthsAgoId"
>Месяцов от текущей даты
</label>
<input
class=" ms-3 from-control"
id="monthsAgoId"
type="number"
min="1"
bind:value={monthsAgo}
<div class="d-flex justify-content-center">
<div class="ms-3 mt-3">
<Flatpickr
options={{ enableTime: false, mode: "range" }}
bind:formattedValue={dates}
class="form-control"
placeholder="Дата"
/>
</div>
{/if}
<div>
{#if bShowLastRecords}
<div class="ms-3 mt-3">
<button
class="btn btn-primary mb-3"
on:click={() => {
toggleTableFold();
}}
>{#if isTableFolded}Развернуть таблицу{:else}Свернуть таблицу{/if}</button
class="btn btn-primary"
disabled={!dateFilled}
on:click={async () => {
await getAllRecords();
}}>Загрузить</button
>
{/if}
</div>
</div>

<div>
<button
class="btn btn-primary mb-3"
on:click={() => {
toggleTableFold();
}}
>{#if isTableFolded}Развернуть таблицу{:else}Свернуть таблицу{/if}</button
>

{#if !isTableFolded}
<table class="table">
<thead>
Expand Down Expand Up @@ -317,7 +275,7 @@
</tbody>
</table>
{/if}
{#if !isTableFolded && bShowLastRecords}
{#if !isTableFolded}
<button
class="btn btn-primary"
on:click={() => {
Expand All @@ -327,12 +285,4 @@
>
{/if}
</div>
{#if bShowLastRecords && !isTableFolded}
<div class="d-flex justify-content-center">
<label class="form-label" for="monthsAgoId"
>Месяцов от текущей даты
</label>
<input class="ms-3" type="number" min="1" bind:value={monthsAgo} />
</div>
{/if}
</div>
3 changes: 0 additions & 3 deletions src/pages/components/SubmenuSwitch.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@
<button class="btn btn-primary" on:click={() => (selectedMenu = "addRecord")}
>Добавить запись</button
>
<button class="btn btn-primary" on:click={() => (selectedMenu = "showRecords")}
>Показать записи</button
>
<slot />
</div>
4 changes: 2 additions & 2 deletions src/pages/lib/getData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function doFetch(payload: string, address: string, token: string, i
return ret_value;
} catch (err) {
Sentry.captureException(err);
alert(err);
alert(`Ошибка:${err}\nВозможно, данные за месяц еще не внесены и база выдает ошибку при запросе средних значений за месяц\nПопробуйте запросить данные за прошлый месяц`);
return '';
}
}
Expand All @@ -48,7 +48,7 @@ export interface priceFeed {
value: number;
}
export interface material {
Id?: number;
Id: number;
name: string;
source: string;
group: string;
Expand Down

0 comments on commit 2b53309

Please sign in to comment.