Skip to content

Commit

Permalink
Merge pull request #8 from LarveyOfficial/dev
Browse files Browse the repository at this point in the history
v.1.3
  • Loading branch information
LarveyOfficial authored May 3, 2023
2 parents 85ce848 + 6dde388 commit 9f5a0a5
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 30 deletions.
75 changes: 51 additions & 24 deletions src/pages/api/manageUserObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,71 @@ export default async (req, res) => {
} else if (req.method === "POST") {
if (req.body.action == "delete") {
const userEmail = req.body.email;
const requestor = session.user.email;
const collection = db.collection("authorizedUsers");

const query = { email: userEmail };
const authQuery = { email: requestor };

const result = await collection.deleteOne(query);
if (result.deletedCount === 1) {
console.log("Successfully deleted one document.");
res.status(200).json({ status: "API called sucessfully", code: 200 });
const authResult = await collection.findOne(authQuery);

if (!authResult.admin) {
res.stauts(401).json({
status: "You are not allowed to preform this action",
code: 401,
});
} else {
res
.status(500)
.json({ status: "No documents with given query found", code: 500 });
const query = { email: userEmail };

const result = await collection.deleteOne(query);
if (result.deletedCount === 1) {
console.log("Successfully deleted one document.");
res
.status(200)
.json({ status: "API called sucessfully", code: 200 });
} else {
res.status(500).json({
status: "No documents with given query found",
code: 500,
});
}
}
} else if (req.body.action == "create") {
const userEmail = req.body.email;
const userName = req.body.name;
const isAdmin = req.body.admin == "true" ? true : false;
const requestor = session.user.email;
const collection = db.collection("authorizedUsers");

const query = { email: userEmail };
const authQuery = { email: requestor };

const findUser = await collection.findOne(query);
if (findUser) {
res
.status(422)
.json({ status: "This user already exists", code: 422 });
const authResult = await collection.findOne(authQuery);

if (!authResult.admin) {
res.stauts(401).json({
status: "You are not allowed to preform this action",
code: 401,
});
} else {
const newUser = {
email: userEmail,
admin: isAdmin,
name: userName,
};
const query = { email: userEmail };

const findUser = await collection.findOne(query);
if (findUser) {
res
.status(422)
.json({ status: "This user already exists", code: 422 });
} else {
const newUser = {
email: userEmail,
admin: isAdmin,
name: userName,
};

const addUser = await collection.insertOne(newUser);
console.log("Successfully added new User");
res
.status(200)
.json({ addUser, status: "API called sucessfully", code: 200 });
const addUser = await collection.insertOne(newUser);
console.log("Successfully added new User");
res
.status(200)
.json({ addUser, status: "API called sucessfully", code: 200 });
}
}
} else {
res.status(404).json({ status: "404 Route not found" });
Expand Down
73 changes: 67 additions & 6 deletions src/pages/panel/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export function Home() {
const [formData, setFormData] = useState({
url: "",
});
const [formUpload, setFormUpload] = useState();
const [formUploading, setFormUploading] = useState(false);
// Variable that displays if an error has occured or not
const [formSuccessCode, setFormSuccessCode] = useState(Number);

Expand Down Expand Up @@ -84,13 +86,57 @@ export function Home() {
}));
};

const handleFormUpload = (e: any) => {
setFormUpload(e.target.files[0]);
setFormUploading(true);
};

const uploadToSpace = async () => {
const formData = new FormData();
const auth = "" + process.env.NEXT_PUBLIC_TIXTEAPI;

if (formUpload) {
formData.append("file", formUpload!);
const res = await fetch("https://api.tixte.com/v1/upload?random=true", {
method: "POST",
headers: {
Authorization: auth,
},
body: formData,
});

const data = await res.json();
if (data.success == false) {
return "failed";
}
return data.data.direct_url;
} else {
return "failed";
}
};

// Handles the updating of GIF/Video source URL
const handleSubmit = async (event: any) => {
let data;
event.preventDefault();

const data = {
url: event.target.url.value,
};
if (formUploading) {
const newUrl = await uploadToSpace();
if (newUrl == "failed") {
setFormSuccessCode(403);
return;
}
setFormData((prevState) => ({
...prevState,
url: newUrl!,
}));
data = {
url: newUrl,
};
} else {
data = {
url: event.target.url.value,
};
}

const JSONdata = JSON.stringify(data);

Expand Down Expand Up @@ -134,6 +180,15 @@ export function Home() {
url: "",
});
setFormSuccessCode(200);
setFormUploading(false);
setFormUpload("" as any);
var oldInput = document.getElementById("formUploadFile");
var newInput = document.createElement("input");
newInput.type = "file";
newInput.id = oldInput!.id;
newInput.className = oldInput!.className;
newInput.onchange = handleFormUpload;
oldInput!.parentNode!.replaceChild(newInput, oldInput!);
await delay(2000);
setFormSuccessCode(418);
} else {
Expand Down Expand Up @@ -166,17 +221,23 @@ export function Home() {
id="url"
onChange={handleInput}
value={formData.url}
required
required={formUploading}
disabled={formUploading}
placeholder="https://media.giphy.com/media/fRB9j0KCRe0KY/giphy.gif"
className="flex flex-1 rounded-lg border focus:ring-inset focus:ring-yellow-400 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-100 sm:text-sm"
/>
<input
type="file"
id="formUploadFile"
onChange={handleFormUpload}
></input>
{formSuccessCode == 200 ? (
<p className="text-sm text-green-500">Success!</p>
) : (
<></>
)}
{formSuccessCode == 403 ? (
<p className="text-sm text-red-500">Wrong file type</p>
<p className="text-sm text-red-500">Something went wrong</p>
) : (
<></>
)}
Expand Down

1 comment on commit 9f5a0a5

@vercel
Copy link

@vercel vercel bot commented on 9f5a0a5 May 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

shopgifs – ./

shopgifs.vercel.app
shopgifs-luisrvervaet.vercel.app
shopgifs-git-main-luisrvervaet.vercel.app

Please sign in to comment.