Skip to content

Commit

Permalink
Update frozen button functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
Heathcorp committed Mar 14, 2024
1 parent 65b5c2c commit da296aa
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
2 changes: 2 additions & 0 deletions experiments/thebutton/src/components/components.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

border: 0.25rem solid var(--border-0);

white-space: nowrap;

/* ///// */
/* border: 0.25rem inset var(--background-1);
background-color: var(--background-2);
Expand Down
31 changes: 21 additions & 10 deletions experiments/thebutton/src/pages/MainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createResource,
onCleanup,
on,
Show,
} from 'solid-js';

import LogoType from '../components/LogoType';
Expand All @@ -29,11 +30,11 @@ const MainPage: Component = (props) => {
const functions = getFunctions(firebaseApp);
const getButtonCountFunction = httpsCallable<
unknown,
{ success: boolean; count?: number; reason?: string }
{ success: boolean; count?: number; reason?: string; frozen?: boolean }
>(functions, 'buttonCount');
const pressButtonFunction = httpsCallable<
{ count?: number; turnstileToken: string },
{ success: boolean; reason?: string }
{ success: boolean; reason?: string; frozen?: boolean }
>(functions, 'buttonPressed');

// get the button count/store the button count
Expand All @@ -60,6 +61,9 @@ const MainPage: Component = (props) => {
}
setLoadingStatus('LOADED');
resolve(resp.data.count ?? value ?? 0);
if (resp.data.frozen) {
setCountFrozenGlobally(true);
}
})
.catch((err) => {
fail = err;
Expand Down Expand Up @@ -90,6 +94,7 @@ const MainPage: Component = (props) => {
setSpooledPresses((prevSpooled) => prevSpooled + cachedSpooledPresses);
};
let fail: string | undefined;
let frozenReturned = false;
setLoadingStatus('UPLOADING');
pressButtonFunction({
count: cachedSpooledPresses,
Expand All @@ -98,29 +103,30 @@ const MainPage: Component = (props) => {
.then((value) => {
console.log(value);
if (!value.data.success) {
revert();
fail = value.data.reason;
}
if (value.data.frozen) {
frozenReturned = true;
}
})
.catch((reason) => {
console.error(reason);
console.log('fail', reason);
revert();
fail = reason;
})
.finally(() => {
if (fail) {
setLoadingStatus('ERROR');
if (fail === 'count frozen') {
console.log('COUNT FROZEN RETURNED');
setCountFrozenGlobally(true);
setSpooledPresses(0);
return;
}
revert();
} else {
setLoadingStatus('LOADED');
refetchIntervalId = setInterval(refetch, REFETCH_INTERVAL);
}
if (frozenReturned) {
console.log('COUNT FROZEN RETURNED');
setCountFrozenGlobally(true);
setSpooledPresses(0);
}
});
};

Expand Down Expand Up @@ -202,6 +208,11 @@ const MainPage: Component = (props) => {
}
/>
</div>
<Show when={countFrozenGlobally()}>
<div class="infoText">
The Button has been frozen for maintenance. Please check back later.
</div>
</Show>
</div>
{/* TODO: PARAMETERIZE PROPERLY AND REFACTOR */}
<Turnstile
Expand Down
6 changes: 6 additions & 0 deletions experiments/thebutton/src/pages/pages.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@
padding: 2rem;
border: 0.25rem outset black;
gap: 1rem;
width: min-content;
}

.infoText {
padding: 0.5rem;
border: 0.25rem dashed var(--border-0);
}
22 changes: 17 additions & 5 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ export const helloWorld = https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});


// extreme cases where we need to shut off the button but still show a count
const COUNT_FROZEN: boolean = false;
const getIsFrozen = async () => {
const db = getDatabase();
const ref = db.ref("thebutton/frozen");
const frozen = (await ref.once("value")).val();
return !!frozen;
};

export const buttonCount = https.onCall(async (data, context) => {
const db = getDatabase();


try {
const frozen = await getIsFrozen();

const ref = db.ref("thebutton/main_count");
const count = (await ref.once("value")).val();

Expand All @@ -34,6 +43,7 @@ export const buttonCount = https.onCall(async (data, context) => {

return {
success: true,
frozen,
count,
};
} catch (err: any) {
Expand All @@ -48,10 +58,12 @@ export const buttonCount = https.onCall(async (data, context) => {

export const buttonPressed = https.onCall(
async (data: { count?: number; turnstileToken: string }, context) => {
if (COUNT_FROZEN) {
return {success: false, reason: "count frozen"};
}

const frozen = await getIsFrozen();
if (frozen) {
return { success: false, frozen };
}

const db = getDatabase();

if (
Expand All @@ -65,7 +77,7 @@ export const buttonPressed = https.onCall(
}

// Captcha time!
try {
try {
if (!data.turnstileToken) {
return {
success: false,
Expand Down

0 comments on commit da296aa

Please sign in to comment.