Skip to content

Commit

Permalink
add setting sheet and usename changing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sk337 committed Jan 23, 2024
1 parent 6354f67 commit 47b9dde
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"license": "ISC",
"dependencies": {
"@babel/plugin-syntax-import-attributes": "^7.23.3",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-label": "^2.0.2",
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-toast": "^1.1.5",
Expand Down
107 changes: 95 additions & 12 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ import {
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Toast } from "@/components/ui/toast";
import { Toaster } from "@/components/ui/toaster";
import { useToast } from "@/components/ui/use-toast";
import {
Sheet,
SheetClose,
SheetContent,
SheetDescription,
SheetFooter,
SheetHeader,
SheetTitle,
SheetTrigger,
} from "@/components/ui/sheet";

import loading from "@/loading.svg";

export default function App() {
const ApiUrl = "https://sb-api-fb48ef34a197.herokuapp.com/";

const [isLogin, setIsLogin] = useState("load");
const [userData, setUserData] = useState(false);
const [pubInfo, setPubInfo] = useState(false);
Expand All @@ -41,15 +52,18 @@ export default function App() {
fetchUserData();
}, []);

function flogin(){
login(document.getElementById("username").value, document.getElementById("password").value).then( res => {
if (res.error){
alert(`login failed: ${res.message}`)
} else {
window.localStorage.setItem("token",res.token)
window.location.reload()
}
});
function flogin() {
login(
document.getElementById("username").value,
document.getElementById("password").value
).then((res) => {
if (res.error) {
alert(`login failed: ${res.message}`);
} else {
window.localStorage.setItem("token", res.token);
window.location.reload();
}
});
}

if (isLogin == false) {
Expand All @@ -61,7 +75,12 @@ export default function App() {
Login With Swordbattle
</h1>
<Input id="username" placeholder="Username"></Input>
<Input className="mt-5" id="password" placeholder="Password" type="password"></Input>
<Input
className="mt-5"
id="password"
placeholder="Password"
type="password"
></Input>
<Button
className="text-center mt-5 bg-blue-500 hover:bg-blue-600"
onClick={flogin}
Expand Down Expand Up @@ -131,6 +150,39 @@ export default function App() {
window.location.reload();
}

function changeUsername() {
let oldUsername = document.getElementById("oldUsername").value;
let newUsername = document.getElementById("newUsername").value;
if (oldUsername == "" || newUsername == "") {
alert("Please fill out all fields");
return;
} else if (oldUsername !== newUsername) {
alert("Username must match");
return;
}

fetch(`${ApiUrl}/`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${window.localStorage.getItem("token")}`,
},
body: JSON.stringify({
newUsername: newUsername,
}),
}).then((res) => {
if (res.status == 200) {
res.json().then((data) => {
//Changing username resets the token
window.localStorage.setItem("token", data.token);
window.location.reload();
});
} else {
alert("Username change failed");
}
});
}

// console.log(pubInfo)
if (userData && pubInfo) {
return (
Expand Down Expand Up @@ -193,6 +245,37 @@ export default function App() {
{userData.account.secret}
</span>
<br />
<div className="flex flex-row w-full mt-5">
<Sheet className="w-full mr-2">
<SheetTrigger className="w-full ">
<Button className="w-full">Settings</Button>
</SheetTrigger>
<SheetContent>
<p className="text-3xl font-bold m-3">Settings</p>

<p className="font-bold mt-5">Change Username</p>
<hr />
<Label htmlFor="ou">Old Username</Label>
<Input
name="ou"
id="oldUsername"
placeholder="Original Username"
></Input>

<Label htmlFor="nu">New Username</Label>
<Input
name="nu"
id="newUsername"
placeholder="New Username"
></Input>
<Button className="mt-5" onClick={changeUsername}>
Submit
</Button>
</SheetContent>
</Sheet>

<Button className="w-full ml-2">test</Button>
</div>
<Button
className="bg-blue-500 hover:bg-blue-600 mt-5 text-center w-full"
onClick={logout}
Expand Down
138 changes: 138 additions & 0 deletions src/components/ui/sheet.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import * as React from "react"
import * as SheetPrimitive from "@radix-ui/react-dialog"
import { cva, type VariantProps } from "class-variance-authority"
import { X } from "lucide-react"

import { cn } from "@/utils/utils"

const Sheet = SheetPrimitive.Root

const SheetTrigger = SheetPrimitive.Trigger

const SheetClose = SheetPrimitive.Close

const SheetPortal = SheetPrimitive.Portal

const SheetOverlay = React.forwardRef<
React.ElementRef<typeof SheetPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof SheetPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<SheetPrimitive.Overlay
className={cn(
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
ref={ref}
/>
))
SheetOverlay.displayName = SheetPrimitive.Overlay.displayName

const sheetVariants = cva(
"fixed z-50 gap-4 bg-background p-6 shadow-lg transition ease-in-out data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:duration-300 data-[state=open]:duration-500",
{
variants: {
side: {
top: "inset-x-0 top-0 border-b data-[state=closed]:slide-out-to-top data-[state=open]:slide-in-from-top",
bottom:
"inset-x-0 bottom-0 border-t data-[state=closed]:slide-out-to-bottom data-[state=open]:slide-in-from-bottom",
left: "inset-y-0 left-0 h-full w-3/4 border-r data-[state=closed]:slide-out-to-left data-[state=open]:slide-in-from-left sm:max-w-sm",
right:
"inset-y-0 right-0 h-full w-3/4 border-l data-[state=closed]:slide-out-to-right data-[state=open]:slide-in-from-right sm:max-w-sm",
},
},
defaultVariants: {
side: "right",
},
}
)

interface SheetContentProps
extends React.ComponentPropsWithoutRef<typeof SheetPrimitive.Content>,
VariantProps<typeof sheetVariants> {}

const SheetContent = React.forwardRef<
React.ElementRef<typeof SheetPrimitive.Content>,
SheetContentProps
>(({ side = "right", className, children, ...props }, ref) => (
<SheetPortal>
<SheetOverlay />
<SheetPrimitive.Content
ref={ref}
className={cn(sheetVariants({ side }), className)}
{...props}
>
{children}
<SheetPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-secondary">
<X className="h-4 w-4" />
<span className="sr-only">Close</span>
</SheetPrimitive.Close>
</SheetPrimitive.Content>
</SheetPortal>
))
SheetContent.displayName = SheetPrimitive.Content.displayName

const SheetHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-2 text-center sm:text-left",
className
)}
{...props}
/>
)
SheetHeader.displayName = "SheetHeader"

const SheetFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className
)}
{...props}
/>
)
SheetFooter.displayName = "SheetFooter"

const SheetTitle = React.forwardRef<
React.ElementRef<typeof SheetPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof SheetPrimitive.Title>
>(({ className, ...props }, ref) => (
<SheetPrimitive.Title
ref={ref}
className={cn("text-lg font-semibold text-foreground", className)}
{...props}
/>
))
SheetTitle.displayName = SheetPrimitive.Title.displayName

const SheetDescription = React.forwardRef<
React.ElementRef<typeof SheetPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof SheetPrimitive.Description>
>(({ className, ...props }, ref) => (
<SheetPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
SheetDescription.displayName = SheetPrimitive.Description.displayName

export {
Sheet,
SheetPortal,
SheetOverlay,
SheetTrigger,
SheetClose,
SheetContent,
SheetHeader,
SheetFooter,
SheetTitle,
SheetDescription,
}

0 comments on commit 47b9dde

Please sign in to comment.