Skip to content

Commit

Permalink
feat: Validation of DeckThemes token
Browse files Browse the repository at this point in the history
  • Loading branch information
beebls committed Jul 1, 2023
1 parent 42ae175 commit a4922fc
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ import { open } from "@tauri-apps/api/dialog";
import { osContext } from "@contexts/osContext";
import { ImSpinner5 } from "react-icons/im";
import { CreateTemplateTheme } from "@components/Settings";
import { fetch, Body } from "@tauri-apps/api/http";

export default function SettingsPage() {
const [token, setToken] = useState<string>("");
function onSaveToken() {
storeWrite("shortToken", token).then(() => {
toast("Token Saved");
});
}

useEffect(() => {
storeRead("shortToken").then((res) => {
if (res.success && res.result) {
Expand All @@ -43,6 +40,32 @@ export default function SettingsPage() {
const [installText, setInstallText] = useState<string>("");
const [installModalDesc, setInstallModalDesc] = useState<string>("");

function onSaveToken() {
if (token.length !== 12) {
toast("Invalid Token Length");
return;
}
setOngoingAction(true);
fetch("https://api.deckthemes.com/auth/authenticate_token", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: Body.json({ token: token }),
})
.then((res) => {
if (res.ok) {
storeWrite("shortToken", token).then(() => {});
toast("Token Saved");
setOngoingAction(false);
} else {
throw new Error("Invalid Token");
}
})
.catch(() => {
toast("Error Validating Token");
setOngoingAction(false);
});
}

return (
<>
<main className="flex w-full flex-1 flex-grow flex-col items-center gap-4">
Expand Down Expand Up @@ -80,10 +103,11 @@ export default function SettingsPage() {
onValueChange={setToken}
/>
<button
disabled={ongoingAction}
onClick={onSaveToken}
className="h-12 whitespace-nowrap rounded-xl bg-brandBlue px-4"
>
Save Token
{ongoingAction ? <ImSpinner5 /> : "Save Token"}
</button>
</div>
</div>
Expand Down

0 comments on commit a4922fc

Please sign in to comment.