diff --git a/pages/account/onboarding/index.js b/pages/account/onboarding/index.js index 3c5542cd941..c6727d19a91 100644 --- a/pages/account/onboarding/index.js +++ b/pages/account/onboarding/index.js @@ -2,6 +2,7 @@ import { authOptions } from "../../api/auth/[...nextauth]"; import { getServerSession } from "next-auth/next"; import { useRouter } from "next/router"; import { useSession } from "next-auth/react"; +import { useState } from "react"; import { FaGithub, FaLink, @@ -10,8 +11,10 @@ import { FaTent, FaCertificate, FaArrowUpRightFromSquare, + FaTriangleExclamation, } from "react-icons/fa6"; +import { clientEnv } from "@config/schemas/clientSchema"; import logger from "@config/logger"; import PageHead from "@components/PageHead"; import Page from "@components/Page"; @@ -19,6 +22,7 @@ import { getUserApi } from "pages/api/profiles/[username]"; import { PROJECT_NAME } from "@constants/index"; import Card from "@components/Card"; import Button from "@components/Button"; +import Modal from "@components/Modal"; import Navigation from "@components/account/manage/Navigation"; import ProgressBar from "@components/statistics/ProgressBar"; import Alert from "@components/Alert"; @@ -46,6 +50,7 @@ export async function getServerSideProps(context) { "testimonials", "events", "repos", + "delete" ]; let progress = { percentage: 0, @@ -62,11 +67,11 @@ export async function getServerSideProps(context) { ).toFixed(0); return { - props: { profile, progress }, + props: { profile, progress, BASE_URL: clientEnv.NEXT_PUBLIC_BASE_URL }, }; } -export default function Onboarding({ profile, progress }) { +export default function Onboarding({ profile, progress, BASE_URL }) { const router = useRouter(); const { data: session } = useSession(); if (typeof window !== "undefined" && window.localStorage) { @@ -82,6 +87,7 @@ export default function Onboarding({ profile, progress }) { router.push("/api/stripe"); } } + const [showModal, setShowModal] = useState(false); const cards = [ { @@ -144,12 +150,32 @@ export default function Onboarding({ profile, progress }) { }, isEdit: profile.milestones && profile.milestones.length > 0, }, + { + icon: FaTriangleExclamation, + title: "Delete Account", + description: "Delete your account", + button: { + name: "Delete Account", + }, + click: true, + }, ]; const alerts = { premium: "You are now a premium user!", cancel: "You cancelled your subscription.", }; + + //TODO move this? + const deleteLinks = async () => { + await fetch(`${BASE_URL}/api/account/manage/delete/${profile.username}`, { + method: "DELETE", + headers: { + "Content-Type": "application/json", + }, + }); + } + return ( <> {cards.map((card) => (
  • + {card.click ? +
    +
    + +

    {card.title}

    +
    +

    {card.description}

    + + + + {/* TODO: on click, show a indicator deltion is happening - success message after */} +
    + + +
    +

    Warning! This cannot be reversed once you click Delete.

    +
    + +
    + :

    {card.title}

    {card.description}

    - {card.isEdit && } - {!card.isEdit && ( - - )} + {card.isEdit ? + : + }
    +}
  • ))} diff --git a/pages/api/account/manage/delete/[[...data]].js b/pages/api/account/manage/delete/[[...data]].js new file mode 100644 index 00000000000..75779fb6175 --- /dev/null +++ b/pages/api/account/manage/delete/[[...data]].js @@ -0,0 +1,71 @@ +import { authOptions } from "../../../auth/[...nextauth]"; +import { getServerSession } from "next-auth/next"; + +import connectMongo from "@config/mongo"; +//import logger from "@config/logger"; +import { Link } from "@models/index"; +import fs from 'fs'; + +export default async function handler(req, res) { + const session = await getServerSession(req, res, authOptions); + const username = session.username; + + if (!["DELETE"].includes(req.method)) { + return res.status(400).json({ + error: "Invalid request: DELETE required", + }); + } + + const { data } = req.query; + const context = { req, res }; + + let link = {}; + + if (req.method === "DELETE") { + link = await deleteLinkApi(context, username, data[0]); + } + + if (link.error) { + return res.status(400).json({ message: link.error }); + } + + return res.status(200).json(link); +} + + + export async function deleteLinkApi(context, username) { + await connectMongo(); + // const log = logger.child({ username }); + + // delete link + try { + await Link.deleteMany({ username: username }); + } catch (e) { + const error = `failed to delete links from profile for username: ${username}`; + //log.error(e, error); + return { error }; + } + checkJsonProfile(username) + return JSON.parse(JSON.stringify({})); + } + + //check for json version, delete if exists + async function checkJsonProfile(username){ + if(fs.existsSync(`./data/${username}.json`)){ + try { + + console.log("found file" + username) + fs.unlinkSync(`./data/${username}.json`) + + } catch (e) { + const error = `failed to delete json profile for username: ${username}`; + //log.error(e, error); + return { error }; + } + + return JSON.parse(JSON.stringify({})); + }else{ + console.log(`no json profile`) + return JSON.parse(JSON.stringify({})); + } + } \ No newline at end of file