Skip to content

Commit 1909d11

Browse files
committed
🚧 stared working on goal sync
1 parent 946f8f3 commit 1909d11

3 files changed

Lines changed: 161 additions & 22 deletions

File tree

media/stylesheet.css

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ button.button-disabled,
760760
flex-wrap: wrap;
761761
justify-content: center;
762762
align-items: center;
763+
763764
width: auto;
764765
margin: 0 0 10px;
765766
text-align: center;
@@ -776,12 +777,40 @@ button.button-disabled,
776777
min-height: 100px;
777778
}
778779

780+
.explore-input-box {
781+
position: relative;
782+
}
783+
779784
.explore-input {
780785
display: block;
781786
width: 100%;
782787
max-width: 100%;
783788
margin: 20px 0 10px;
784789
box-sizing: border-box;
790+
padding-right: 42px;
791+
}
792+
793+
.explore-clear-query {
794+
position: absolute;
795+
right: 12px;
796+
top: 50%;
797+
transform: translateY(-50%);
798+
margin: 0;
799+
padding: 0;
800+
border: 0;
801+
border-radius: 6px;
802+
width: 24px;
803+
height: 24px;
804+
background: transparent;
805+
color: var(--text);
806+
display: inline-flex;
807+
align-items: center;
808+
justify-content: center;
809+
}
810+
811+
.explore-clear-query:hover {
812+
background: var(--input-background-hover);
813+
color: var(--text);
785814
}
786815

787816
.cookie-display {
@@ -1109,6 +1138,10 @@ button.button-disabled,
11091138
color: var(--warning);
11101139
}
11111140

1141+
.goal-toggle {
1142+
margin-bottom: 30px;
1143+
}
1144+
11121145
@media (max-width: 600px) {
11131146
.goal-item {
11141147
width: 100%;

src/apiCalls.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ export async function getGoals(givenApiKey: string) {
830830
return (await res.json()) as goalsResponse;
831831
}
832832

833-
export async function putGoals(givenApiKey: string, givenGoals: []) {
833+
export async function putGoals(givenApiKey: string, givenGoal: string) {
834834
// get api key
835835
const apiKey = resolveApiKey(givenApiKey);
836836

@@ -842,7 +842,7 @@ export async function putGoals(givenApiKey: string, givenGoals: []) {
842842
}
843843

844844
const body = new URLSearchParams({
845-
goals: givenGoals
845+
goals: givenGoal
846846
});
847847

848848
const res = await fetch(`https://logpheus.gizzy.gay/api/v1/goals`, {
@@ -856,7 +856,40 @@ export async function putGoals(givenApiKey: string, givenGoals: []) {
856856

857857
if (!res.ok) {
858858
throw new Error(
859-
`Failed to put goals: ${res.status} ${await res.text()}`,
859+
`Failed to put goal: ${res.status} ${await res.text()}`,
860+
);
861+
}
862+
863+
return (await res.json()) as goalsResponse;
864+
}
865+
866+
export async function deleteGoals(givenApiKey: string, givenGoal: string) {
867+
// get api key
868+
const apiKey = resolveApiKey(givenApiKey);
869+
870+
// get project id
871+
const config = vscode.workspace.getConfiguration("flavorcode");
872+
873+
interface goalsResponse {
874+
goals: []
875+
}
876+
877+
const body = new URLSearchParams({
878+
goals: givenGoal
879+
});
880+
881+
const res = await fetch(`https://logpheus.gizzy.gay/api/v1/goals`, {
882+
method: "PUT",
883+
headers: {
884+
Authorization: `Bearer ${apiKey}`,
885+
"Content-Type": "application/x-www-form-urlencoded",
886+
},
887+
body,
888+
});
889+
890+
if (!res.ok) {
891+
throw new Error(
892+
`Failed to delete goal: ${res.status} ${await res.text()}`,
860893
);
861894
}
862895

src/webviews/explore.html

Lines changed: 92 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,29 @@ <h1>Explore</h1>
3737

3838
<div id="shop" class="explore-box">
3939
<div class="cookie-display">
40-
<p>Current Balance: 100🍪</p>
40+
<p id="cookie-display">Current Balance: 100🍪</p>
41+
</div>
42+
<div class="goal-toggle">
43+
<button onclick="toggleGoals()">Toggle goals</button>
4144
</div>
4245
<div id="goals" class="shop-item-box goals-grid"></div>
4346
<div>
44-
<div>
45-
<input
46-
type="text"
47-
placeholder="search shop item"
48-
class="explore-input"
49-
oninput="updateQuery(this)"
50-
/>
47+
<div class="explore-input-box">
48+
<input
49+
id="shop-query"
50+
type="text"
51+
placeholder="search shop item"
52+
class="explore-input"
53+
oninput="updateQuery(this)"
54+
/>
55+
<button
56+
class="explore-clear-query"
57+
type="button"
58+
aria-label="Clear search"
59+
onclick="clearQuery()"
60+
>
61+
<i class="codicon codicon-close"></i>
62+
</button>
5163
</div>
5264

5365
<div class="explore-filters">
@@ -188,13 +200,18 @@ <h3>price history</h3>
188200
var chart = null;
189201
var goals = [];
190202
var goalAmounts = {};
203+
var goalsHidden = false;
191204

192205
// user
193206
var users = {};
194207

195208
// project
196209
var projects = {};
197210

211+
// retrive cache
212+
retriveCacheState();
213+
handleShopLoad();
214+
198215
// general functions
199216

200217
function setTheme(theme) {
@@ -241,6 +258,19 @@ <h3>price history</h3>
241258
}
242259
}
243260

261+
// cache functions
262+
function cacheState(state) {
263+
vscode.setState({ cache: state });
264+
}
265+
266+
function retriveCacheState() {
267+
const state = vscode.getState();
268+
shopItems = state?.cache?.shopItems;
269+
userData = state?.cache?.userData;
270+
goals = state?.cache?.goals;
271+
goalsHidden = state?.cache?.goalsHidden;
272+
}
273+
244274
function escapeHtml(value) {
245275
if (!value) return "";
246276

@@ -350,6 +380,14 @@ <h3>price history</h3>
350380

351381
function handleShopLoad() {
352382
if (shopItems && goals && userData) {
383+
// cache state
384+
cacheState({
385+
shopItems: shopItems,
386+
userData: userData,
387+
goals: goals,
388+
goalsHidden: goalsHidden,
389+
});
390+
353391
populateShop();
354392
populateGoals();
355393
}
@@ -362,6 +400,9 @@ <h3>price history</h3>
362400
return item.name;
363401
});
364402

403+
document.getElementById("cookie-display").textContent =
404+
`Current Balance: ${userData.cookies ?? 0}🍪`;
405+
365406
const fuse = new Fuse(shopItemsNameArray, { includeScore: true });
366407

367408
// Get matching names once
@@ -426,19 +467,27 @@ <h3>price history</h3>
426467

427468
// filters display
428469
.filter((item) => {
470+
const price =
471+
item.ticket_cost?.[region] ?? item.ticket_cost?.base_cost ?? 0;
472+
429473
if (display === "all") {
430474
return true;
431475
}
432476

433-
if (
434-
display === "affordable" &&
435-
item.ticket_cost?.[region] <= (userData.cookies ?? 0)
436-
) {
437-
return true;
477+
if (display === "affordable") {
478+
if (price <= (userData.cookies ?? 0)) {
479+
return true;
480+
} else {
481+
return false;
482+
}
438483
}
439484

440-
if (display === "in-stock" && (item.stock ?? 0) >= 1) {
441-
return true;
485+
if (display === "in-stock") {
486+
if ((item.stock ?? 0) >= 1 || !item.limited) {
487+
return true;
488+
} else {
489+
return false;
490+
}
442491
}
443492

444493
if (display === "sale") {
@@ -701,6 +750,18 @@ <h2>${item.name}</h2>
701750
populateShop();
702751
}
703752

753+
function clearQuery() {
754+
const input = document.getElementById("shop-query");
755+
if (!input) {
756+
return;
757+
}
758+
759+
input.value = "";
760+
shopQuery = "";
761+
populateShop();
762+
input.focus();
763+
}
764+
704765
function updateExtension(circleId, containerId, price) {
705766
const container = document.getElementById(
706767
`extension-container${containerId}`,
@@ -752,7 +813,9 @@ <h2>${item.name}</h2>
752813
goalContainer.innerHTML = uniqueGoals
753814
.map((goal) => {
754815
if (shopItems[goal]) {
755-
const amount = goals.filter((item) => Number(item) === goal).length;
816+
const amount = goals.filter(
817+
(item) => Number(item) === goal,
818+
).length;
756819
goalAmounts[goal] = amount;
757820
const item = shopItems[goal];
758821
const price =
@@ -779,11 +842,22 @@ <h2>${item.name}</h2>
779842
</div>
780843
<button class="goal-delete-button" onclick='deleteGoal(${goal})'><i class="codicon codicon-trash"></i></button>
781844
</div>`;
845+
782846
}
783847
})
784848
.join("");
785849
}
786850

851+
function toggleGoals() {
852+
if (goalsHidden) {
853+
goalContainer.style.display = "";
854+
goalsHidden = !goalsHidden;
855+
} else {
856+
goalContainer.style.display = "none";
857+
goalsHidden = !goalsHidden;
858+
}
859+
}
860+
787861
function handleAddGoal(id) {
788862
const goalId = Number(id);
789863

@@ -792,19 +866,17 @@ <h2>${item.name}</h2>
792866
}
793867

794868
if (goals.includes(goalId)) {
795-
goals = goals.filter((goal) => Number(goal) !== goalId);
796869
document.getElementById(`favourite-${id}`).className = "favourite";
797870
document.getElementById(`favourite-icon-${id}`).className =
798871
"codicon codicon-star-empty";
799872
} else {
800-
goals.push(goalId);
801873
document.getElementById(`favourite-${id}`).className =
802874
"favourite-active favourite";
803875
document.getElementById(`favourite-icon-${id}`).className =
804876
"codicon codicon-star-full";
805877
}
806878

807-
vscode.postMessage({ command: "put-goal", value: goals });
879+
vscode.postMessage({ command: "put-goal", value: });
808880
populateGoals();
809881
}
810882

@@ -919,6 +991,7 @@ <h2>${item.name}</h2>
919991
}
920992
case "user-data": {
921993
userData = message.value;
994+
handleShopLoad();
922995
break;
923996
}
924997
case "goals": {

0 commit comments

Comments
 (0)