Skip to content
This repository has been archived by the owner on Dec 10, 2023. It is now read-only.

Commit

Permalink
added functionality on profile page
Browse files Browse the repository at this point in the history
  • Loading branch information
Manas2403 committed Jun 17, 2023
1 parent 1148741 commit 53b800e
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 127 deletions.
3 changes: 3 additions & 0 deletions components/elements/ProjectCard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export default (props) => {
{props.allowedOrigins.map((origin, i) => (
<div className="text-[#970606]" key={i}>
{origin}
{i !== props.allowedOrigins.length - 1
? ","
: ""}
</div>
))}
</div>
Expand Down
144 changes: 72 additions & 72 deletions components/utils/API/index.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,101 @@
import axios from 'axios';
import { getLS, removeLS } from '../LocalStorage/index';
import axios from "axios";
import { getLS, removeLS } from "../LocalStorage/index";

const API_URL =
process.env.NEXT_PUBLIC_ENVIORNMENT === 'prod'
? 'https://api.savemyform.tk'
: 'https://dev-api.savemyform.tk';
process.env.NEXT_PUBLIC_ENVIORNMENT === "prod"
? "https://api.savemyform.tk"
: "https://dev-api.savemyform.tk";

const getAccessToken = () => {
return getLS('secret');
return getLS("secret");
};

const getHeaders = (token) => {
if (!token) token = getAccessToken();
if (token) {
if (!token) token = getAccessToken();
if (token) {
return {
headers: {
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
};
}
return {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
},
headers: {
Accept: "application/json",
},
};
}
return {
headers: {
Accept: 'application/json',
},
};
};

const post = async (endpoint, body, token = null, form = false) => {
let options = getHeaders(token);
if (form) {
options.headers['Content-Type'] = 'multipart/form-data';
}
try {
const response = await axios.post(API_URL + endpoint, body, options);
return response;
} catch (err) {
console.error(err?.response?.data || err);
if (err?.response?.status === 401) {
console.log('Wrong password');
removeLS('secret');
} else if (err?.response?.status === 404) {
console.log('404 Error');
removeLS('secret');
let options = getHeaders(token);
if (form) {
options.headers["Content-Type"] = "multipart/form-data";
}
try {
const response = await axios.post(API_URL + endpoint, body, options);
return response;
} catch (err) {
console.error(err?.response?.data || err);
if (err?.response?.status === 401) {
console.log("Wrong password");
removeLS("secret");
} else if (err?.response?.status === 404) {
console.log("404 Error");
removeLS("secret");
}
return err?.response?.data || err;
}
return err?.response?.data || err;
}
};

const get = async (endpoint, token = null) => {
try {
const response = await axios.get(API_URL + endpoint, getHeaders(token));
return response;
} catch (err) {
console.error(err?.response?.data || err);
if (err?.response?.status === 401) {
console.log('Wrong password');
removeLS('secret');
try {
const response = await axios.get(API_URL + endpoint, getHeaders(token));
return response;
} catch (err) {
console.error(err?.response?.data || err);
if (err?.response?.status === 401) {
console.log("Wrong password");
removeLS("secret");
}
return err?.response?.data || err;
}
return err?.response?.data || err;
}
};

const patch = async (endpoint, body, token = null) => {
try {
const response = await axios.patch(
API_URL + endpoint,
body,
getHeaders(token),
);
return response.data;
} catch (err) {
console.error(err?.response?.data || err);
if (err?.response?.status === 401) {
console.log('Wrong password');
removeLS('secret');
try {
const response = await axios.patch(
API_URL + endpoint,
body,
getHeaders(token)
);
return response.data;
} catch (err) {
console.error(err?.response?.data || err);
if (err?.response?.status === 401) {
console.log("Wrong password");
removeLS("secret");
}
return err?.response?.data || err;
}
return err?.response?.data || err;
}
};

const remove = async (endpoint, body, token = null) => {
try {
let conf = getHeaders(token);
conf['data'] = body;
const response = await axios.delete(API_URL + endpoint, conf);
return response.data;
} catch (err) {
console.log(err);
try {
let conf = getHeaders(token);
conf["data"] = body;
const response = await axios.delete(API_URL + endpoint, conf);
return response.data;
} catch (err) {
console.log(err);

console.error(err?.response?.data || err);
if (err?.response?.status === 401) {
console.log('Wrong password');
removeLS('secret');
console.error(err?.response?.data || err);
if (err?.response?.status === 401) {
console.log("Wrong password");
removeLS("secret");
}
return err?.response?.data || err;
}
return err?.response?.data || err;
}
};

export { getAccessToken, post, get, patch, remove, API_URL };
2 changes: 1 addition & 1 deletion pages/dashboard/project/[id]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function Project({ id }) {
return (
<>
<SEO
title={`SaveMyForm | Project | ${projectQuery?.data?.name}}`}
title={`SaveMyForm | Project | ${projectQuery?.data?.name}`}
desc={`Dashboard of project ${projectQuery?.data?.name}. SaveMyForm is a platform where yoy save your form data now
easily and securely.No need to create a Backend for collecting
form responses on your application`}
Expand Down
75 changes: 30 additions & 45 deletions pages/dashboard/project/newproject/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,35 @@ export default function NewProject() {
const { executeRecaptcha } = useGoogleReCaptcha();
const router = useRouter();
const [state, setState] = useState(false);
const [firstDom, setFirstDom] = useState("");
const [projectName, setProjectName] = useState(null);
const [recaptchaKey, setReCaptchaKey] = useState(null);
const [recaptchaSecret, setReCaptchaSecret] = useState(null);
const [domain, setDomain] = useState([]);
const [domain, setDomain] = useState([""]);
const onChange = (checked) => {
setState(checked);
};
function handleFirstDomainChange(e) {
setFirstDom(e.target.value);
}
function handleAddDomain() {
if (firstDom === "") {
message.error("Please fill the previous domain");
return;
} else if (domain[domain.length - 1] == "") {
message.error("Please fill the previous domain");
return;
}
const dom = [...domain, []];
setDomain(dom);
}

function handleDomainChange(e, index) {
const dom = [...domain];
dom[index] = e.target.value;
setDomain(dom);
}

function handleDeleteDomain(index) {
const delVal = [...domain];
delVal.splice(index, 1);
setDomain(delVal);
}

const success = () => {
message.success("Project Successfully saved");
router.push("/dashboard");
};
const addDomain = () => {
if (domain.length < 3) {
setDomain([...domain, ""]);
} else {
message.error("You cannot add more than 3 allowed origins");
}
};
const handleDeleteDomain = (i) => {
if (domain.length > 1) {
setDomain((prev) => {
let nDomains = [...prev];
nDomains.splice(i, 1);
return nDomains;
});
} else {
message.error("At least 1 field is required");
}
};
const handleSubmit = async () => {
if (!executeRecaptcha) {
message.error("Recaptcha Failed");
Expand All @@ -80,8 +70,7 @@ export default function NewProject() {
setReCaptchaKey(null);
setReCaptchaSecret(null);
setState(false);
setDomain([]);
setFirstDom("");
setDomain([""]);
};
return (
<>
Expand Down Expand Up @@ -162,16 +151,6 @@ export default function NewProject() {
<div className="text-lg text-[#01684a] font-bold">
Add Allowed Domains
</div>
<div className="flex flex-row items-center">
<Input
placeholder="Domain"
className="rounded-lg border-solid border-2 border-[#01684a]"
onChange={(e) => {
handleFirstDomainChange(e);
}}
value={firstDom}
/>
</div>
{domain.map((item, index) => {
return (
<div
Expand All @@ -181,9 +160,15 @@ export default function NewProject() {
<Input
placeholder="Domain"
className="rounded-lg border-solid border-2 border-[#01684a]"
onChange={(e) => {
handleDomainChange(e, index);
}}
onChange={(e) =>
setDomain((prev) => {
let nFields = [...prev];
nFields[index] =
e.target.value;
return nFields;
})
}
value={item}
/>
<button
className="ml-2"
Expand All @@ -205,7 +190,7 @@ export default function NewProject() {
<div>
<button
className="rounded-lg bg-[#DEF7E5] text-[#023430] font-semibold p-2 w-32"
onClick={() => handleAddDomain()}
onClick={addDomain}
type="button"
>
<Space>
Expand Down
39 changes: 31 additions & 8 deletions pages/settings/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useEffect } from "react";
import { Input, Button, message } from "antd";
import { Input, Button, message, notification } from "antd";
import { patch } from "../../components/utils/API";
import { UserContext } from "../../components/context";
import { useContext } from "react";
Expand All @@ -8,27 +8,51 @@ import { useQueryClient } from "@tanstack/react-query";
import Avatar from "../../components/elements/Avatar";
import SEO from "../../components/utils/SEO";
import Footer from "../../components/elements/Footer";
import { useRouter } from "next/router";
const Settings = () => {
const queryClient = useQueryClient();
const router = useRouter();
const [api, contextHolder] = notification.useNotification();
const openNotification = (placement) => {
const description = (
<div>
<p>
Verify your Email to get access to all the features of
SaveMyForm
</p>
<Button
className="mt-2 rounded-lg bg-[#fbeefc]"
onClick={() => {
router.push("/verify");
}}
>
Verify
</Button>
</div>
);
api.warning({
message: `Email not verified`,
description: description,
placement,
duration: 5,
});
};
const { executeRecaptcha } = useGoogleReCaptcha();
const { user, setUser } = useContext(UserContext);
const [oldPasswordVisible, setOldPasswordVisible] = useState(false);
const [newPasswordVisible, setNewPasswordVisible] = useState(false);
const [userPasswordVisible, setUserPasswordVisible] = useState(false);
const [confirmPasswordVisible, setConfirmPasswordVisible] = useState(false);
const [passwordTab, setPasswordTab] = useState(false);
const [name, setName] = useState(user?.name);
const [email, setEmail] = useState(user?.email);
const [oldPassword, setOldPassword] = useState("");
const [newPassword, setNewPassword] = useState("");
const [confPassword, setConfPassword] = useState("");
const [errorBool, setErrorBool] = useState(false);
const [errorMessage, setErrorMessage] = useState("");
const [messageApi, contextHolder] = message.useMessage();
const [userPassword, setUserPassword] = useState("");
useEffect(() => {
setName(user?.name);
setEmail(user?.email);
if (!user?.verified) {
openNotification("top");
}
}, [user]);
async function handleSave() {
if (!executeRecaptcha) {
Expand Down Expand Up @@ -91,7 +115,6 @@ const Settings = () => {
patch("/user/update", {
name: name,
email: email,
password: userPassword,
recaptcha_token: token,
})
.catch((err) => {
Expand Down
2 changes: 1 addition & 1 deletion pages/verify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useContext, useState, useEffect } from "react";
import { useRouter } from "next/router";
import { get } from "../../components/utils/API";
import React from "react";
import SEO from "../../components/SEO";
import SEO from "../../components/utils/SEO";
export default function Verify() {
const router = useRouter();
let { isLoggedIn, user } = useContext(UserContext);
Expand Down

0 comments on commit 53b800e

Please sign in to comment.