From 79b3e540e5a1e8b88281b0624c6499308da9394a Mon Sep 17 00:00:00 2001 From: Jan-Willem van Bremen Date: Thu, 25 Apr 2024 01:08:34 +0200 Subject: [PATCH 01/76] Add return --- pages/api/flatgroundtricks/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/api/flatgroundtricks/index.js b/pages/api/flatgroundtricks/index.js index 92ad5ee..40bd2c2 100644 --- a/pages/api/flatgroundtricks/index.js +++ b/pages/api/flatgroundtricks/index.js @@ -17,7 +17,7 @@ export default async function handler(req, res) { ...flatgroundTrick, trick: getFullTrickName(flatgroundTrick), })); - res.status(200).json({ success: true, data }); + return res.status(200).json({ success: true, data }); } catch (error) { console.error(error); res.status(400).json({ success: false, error: error.message }); From 428251f228b96624093dfb495a3f07499b7d0bfe Mon Sep 17 00:00:00 2001 From: Jan-Willem van Bremen Date: Thu, 25 Apr 2024 01:25:42 +0200 Subject: [PATCH 02/76] Added db disconnect --- lib/dbConnect.js | 10 ++++++++++ lib/migrations/populateManualStances.js | 4 +++- pages/api/combos/[_id].js | 4 +++- pages/api/combos/index.js | 4 +++- pages/api/combos/search/index.js | 5 +++-- pages/api/flatgroundtricks/[_id].js | 4 +++- pages/api/flatgroundtricks/index.js | 6 ++++-- pages/api/grinds/[_id].js | 4 +++- pages/api/grinds/index.js | 4 +++- pages/api/manuals/[_id].js | 4 +++- pages/api/manuals/index.js | 4 +++- pages/api/profiles/index.js | 4 +++- pages/api/profiles/mine/[endpoint].js | 4 +++- pages/api/profiles/mine/index.js | 5 +++-- pages/api/stats/[endpoint].js | 4 +++- pages/api/stats/mine/[endpoint].js | 4 +++- pages/api/stats/mine/combos/[stat].js | 4 +++- pages/api/stats/mine/profile/[stat].js | 4 +++- pages/api/stats/mine/stance/[stance].js | 4 +++- pages/combos/[_id]/index.jsx | 4 +++- pages/flatgroundtricks/[_id]/index.jsx | 4 +++- pages/grinds/[_id]/index.jsx | 4 +++- pages/manuals/[_id]/index.jsx | 4 +++- pages/profile/index.jsx | 4 +++- 24 files changed, 80 insertions(+), 26 deletions(-) diff --git a/lib/dbConnect.js b/lib/dbConnect.js index cf714cc..4969b41 100644 --- a/lib/dbConnect.js +++ b/lib/dbConnect.js @@ -34,4 +34,14 @@ async function dbConnect() { return cached.conn; } +export async function dbDisconnect() { + if (process.env.NODE_ENV === 'development') return; + + if (cached.conn) { + await cached.conn.disconnect(); + console.log('Disconnected from database 🚪'); + cached.conn = null; + } +} + export default dbConnect; diff --git a/lib/migrations/populateManualStances.js b/lib/migrations/populateManualStances.js index c976e89..1d25ef3 100644 --- a/lib/migrations/populateManualStances.js +++ b/lib/migrations/populateManualStances.js @@ -1,5 +1,5 @@ import './envSetup.js'; -import dbConnect from '../dbConnect.js'; +import dbConnect, { dbDisconnect } from '../dbConnect.js'; import Manual from '../../models/Manual.js'; // Set "type": module" in package.json to run this script @@ -15,6 +15,8 @@ const run = async () => { count++; } + await dbDisconnect(); + console.info(`Populated ${count} manual stances`); }; diff --git a/pages/api/combos/[_id].js b/pages/api/combos/[_id].js index db627db..1b7be7e 100644 --- a/pages/api/combos/[_id].js +++ b/pages/api/combos/[_id].js @@ -1,4 +1,4 @@ -import dbConnect from '../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; import Combo from '../../../models/Combo'; import { populateComboTrickName } from '../../../lib/commonUtils'; import { requireAuth, notFoundHandler } from '../../../lib/serverUtils'; @@ -56,4 +56,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/combos/index.js b/pages/api/combos/index.js index 08dccb8..dc1c80b 100644 --- a/pages/api/combos/index.js +++ b/pages/api/combos/index.js @@ -1,4 +1,4 @@ -import dbConnect from '../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; import Combo from '../../../models/Combo'; import { populateComboName, populateComboTrickName } from '../../../lib/commonUtils'; import { requireAuth } from '../../../lib/serverUtils'; @@ -39,4 +39,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/combos/search/index.js b/pages/api/combos/search/index.js index 3ea908f..a4f0e91 100644 --- a/pages/api/combos/search/index.js +++ b/pages/api/combos/search/index.js @@ -1,6 +1,5 @@ -import dbConnect from '../../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../../lib/dbConnect'; import Combo from '../../../../models/Combo'; -import { authOptions } from '../../auth/[...nextauth]'; import { populateComboName, populateComboTrickName } from '../../../../lib/commonUtils'; import { requireAuth } from '../../../../lib/serverUtils'; import Manual from '../../../../models/Manual'; @@ -44,4 +43,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/flatgroundtricks/[_id].js b/pages/api/flatgroundtricks/[_id].js index 93f6215..ba2e938 100644 --- a/pages/api/flatgroundtricks/[_id].js +++ b/pages/api/flatgroundtricks/[_id].js @@ -1,4 +1,4 @@ -import dbConnect from '../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; import FlatGroundTrick from '../../../models/FlatgroundTrick'; import { getFullTrickName } from '../../../lib/commonUtils'; import { checkForUsedCombos, requireAuth, notFoundHandler } from '../../../lib/serverUtils'; @@ -56,4 +56,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/flatgroundtricks/index.js b/pages/api/flatgroundtricks/index.js index 40bd2c2..531dbb9 100644 --- a/pages/api/flatgroundtricks/index.js +++ b/pages/api/flatgroundtricks/index.js @@ -1,4 +1,4 @@ -import dbConnect from '../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; import FlatgroundTrick from '../../../models/FlatgroundTrick'; import { getFullTrickName } from '../../../lib/commonUtils'; import { requireAuth } from '../../../lib/serverUtils'; @@ -17,7 +17,7 @@ export default async function handler(req, res) { ...flatgroundTrick, trick: getFullTrickName(flatgroundTrick), })); - return res.status(200).json({ success: true, data }); + res.status(200).json({ success: true, data }); } catch (error) { console.error(error); res.status(400).json({ success: false, error: error.message }); @@ -39,4 +39,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/grinds/[_id].js b/pages/api/grinds/[_id].js index 53670f7..f2fafdf 100644 --- a/pages/api/grinds/[_id].js +++ b/pages/api/grinds/[_id].js @@ -1,4 +1,4 @@ -import dbConnect from '../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; import Grind from '../../../models/Grind'; import { getFullGrindName } from '../../../lib/commonUtils'; import { checkForUsedCombos, requireAuth, notFoundHandler } from '../../../lib/serverUtils'; @@ -59,4 +59,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/grinds/index.js b/pages/api/grinds/index.js index 8f522db..8c0b9bd 100644 --- a/pages/api/grinds/index.js +++ b/pages/api/grinds/index.js @@ -1,4 +1,4 @@ -import dbConnect from '../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; import Grind from '../../../models/Grind'; import { getFullGrindName } from '../../../lib/commonUtils'; import { requireAuth } from '../../../lib/serverUtils'; @@ -36,4 +36,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/manuals/[_id].js b/pages/api/manuals/[_id].js index b200e1d..de70f83 100644 --- a/pages/api/manuals/[_id].js +++ b/pages/api/manuals/[_id].js @@ -1,4 +1,4 @@ -import dbConnect from '../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; import Manual from '../../../models/Manual'; import { checkForUsedCombos, requireAuth, notFoundHandler } from '../../../lib/serverUtils'; import { isValidObjectId } from 'mongoose'; @@ -53,4 +53,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/manuals/index.js b/pages/api/manuals/index.js index 7157642..cc4f84e 100644 --- a/pages/api/manuals/index.js +++ b/pages/api/manuals/index.js @@ -1,4 +1,4 @@ -import dbConnect from '../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; import Manual from '../../../models/Manual'; import { getFullManualName } from '../../../lib/commonUtils'; import { requireAuth } from '../../../lib/serverUtils'; @@ -36,4 +36,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/profiles/index.js b/pages/api/profiles/index.js index 88a5fea..1e03608 100644 --- a/pages/api/profiles/index.js +++ b/pages/api/profiles/index.js @@ -1,4 +1,4 @@ -import dbConnect from '../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; import Profile from '../../../models/Profile'; export default async function handler(req, res) { @@ -20,4 +20,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/profiles/mine/[endpoint].js b/pages/api/profiles/mine/[endpoint].js index 26dee75..0f31822 100644 --- a/pages/api/profiles/mine/[endpoint].js +++ b/pages/api/profiles/mine/[endpoint].js @@ -1,4 +1,4 @@ -import dbConnect from '../../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../../lib/dbConnect'; import { ensureProfile, requireAuth } from '../../../../lib/serverUtils'; export default async function handler(req, res) { @@ -31,4 +31,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/profiles/mine/index.js b/pages/api/profiles/mine/index.js index 12e88f5..8edf3f6 100644 --- a/pages/api/profiles/mine/index.js +++ b/pages/api/profiles/mine/index.js @@ -1,6 +1,5 @@ -import dbConnect from '../../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../../lib/dbConnect'; import { ensureProfile, requireAuth, notFoundHandler } from '../../../../lib/serverUtils'; -import { authOptions } from '../../auth/[...nextauth]'; import Profile from '../../../../models/Profile'; export default async function handler(req, res) { @@ -33,4 +32,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/stats/[endpoint].js b/pages/api/stats/[endpoint].js index 21de99e..ee6c53a 100644 --- a/pages/api/stats/[endpoint].js +++ b/pages/api/stats/[endpoint].js @@ -1,4 +1,4 @@ -import dbConnect from '../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; import Combo from '../../../models/Combo'; import FlatgroundTrick from '../../../models/FlatgroundTrick'; import Grind from '../../../models/Grind'; @@ -76,4 +76,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/stats/mine/[endpoint].js b/pages/api/stats/mine/[endpoint].js index 402d347..d115fa5 100644 --- a/pages/api/stats/mine/[endpoint].js +++ b/pages/api/stats/mine/[endpoint].js @@ -1,4 +1,4 @@ -import dbConnect from '../../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../../lib/dbConnect'; import Combo from '../../../../models/Combo'; import FlatgroundTrick from '../../../../models/FlatgroundTrick'; import Grind from '../../../../models/Grind'; @@ -62,4 +62,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/stats/mine/combos/[stat].js b/pages/api/stats/mine/combos/[stat].js index 0843da3..6ba547f 100644 --- a/pages/api/stats/mine/combos/[stat].js +++ b/pages/api/stats/mine/combos/[stat].js @@ -1,4 +1,4 @@ -import dbConnect from '../../../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../../../lib/dbConnect'; import Combo from '../../../../../models/Combo'; import { requireAuth } from '../../../../../lib/serverUtils'; import { TRICK_TYPES_MODELS } from '../../../../../models/constants/trickTypes'; @@ -61,4 +61,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${stat}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/stats/mine/profile/[stat].js b/pages/api/stats/mine/profile/[stat].js index c9897a8..b8b1d0f 100644 --- a/pages/api/stats/mine/profile/[stat].js +++ b/pages/api/stats/mine/profile/[stat].js @@ -1,4 +1,4 @@ -import dbConnect from '../../../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../../../lib/dbConnect'; import { requireAuth } from '../../../../../lib/serverUtils'; import Profile from '../../../../../models/Profile'; import { formatDate } from '../../../../../lib/commonUtils'; @@ -44,4 +44,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${stat}` }); break; } + + await dbDisconnect(); } diff --git a/pages/api/stats/mine/stance/[stance].js b/pages/api/stats/mine/stance/[stance].js index 5f568ed..fd419f1 100644 --- a/pages/api/stats/mine/stance/[stance].js +++ b/pages/api/stats/mine/stance/[stance].js @@ -1,4 +1,4 @@ -import dbConnect from '../../../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../../../lib/dbConnect'; import Combo from '../../../../../models/Combo'; import FlatgroundTrick from '../../../../../models/FlatgroundTrick'; import Grind from '../../../../../models/Grind'; @@ -58,4 +58,6 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${stance}` }); break; } + + await dbDisconnect(); } diff --git a/pages/combos/[_id]/index.jsx b/pages/combos/[_id]/index.jsx index 1f089c6..46c6b4d 100644 --- a/pages/combos/[_id]/index.jsx +++ b/pages/combos/[_id]/index.jsx @@ -1,4 +1,4 @@ -import dbConnect from '../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; import { getCombos, requireAuth } from '../../../lib/serverUtils'; import Combo from '../../../models/Combo'; import { isValidObjectId, Model } from 'mongoose'; @@ -15,6 +15,8 @@ export async function getServerSideProps({ params, req, res }) { if (!combo) return { notFound: true }; + await dbDisconnect(); + return { props: { combo } }; } diff --git a/pages/flatgroundtricks/[_id]/index.jsx b/pages/flatgroundtricks/[_id]/index.jsx index 3054860..492af7b 100644 --- a/pages/flatgroundtricks/[_id]/index.jsx +++ b/pages/flatgroundtricks/[_id]/index.jsx @@ -1,4 +1,4 @@ -import dbConnect from '../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; import { getTricks, requireAuth } from '../../../lib/serverUtils'; import FlatgroundTrickDetails from '../../../components/cards/FlatgroundTrickDetails'; import FlatGroundTrick from '../../../models/FlatgroundTrick'; @@ -15,6 +15,8 @@ export async function getServerSideProps({ params, req, res }) { if (!flatgroundTrick) return { notFound: true }; + await dbDisconnect(); + return { props: { flatgroundTrick } }; } diff --git a/pages/grinds/[_id]/index.jsx b/pages/grinds/[_id]/index.jsx index 45d8f40..deb1591 100644 --- a/pages/grinds/[_id]/index.jsx +++ b/pages/grinds/[_id]/index.jsx @@ -1,4 +1,4 @@ -import dbConnect from '../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; import { getTricks, requireAuth } from '../../../lib/serverUtils'; import GrindDetails from '../../../components/cards/GrindDetails'; import Grind from '../../../models/Grind'; @@ -15,6 +15,8 @@ export async function getServerSideProps({ params, req, res }) { if (!grind) return { notFound: true }; + await dbDisconnect(); + return { props: { grind } }; } diff --git a/pages/manuals/[_id]/index.jsx b/pages/manuals/[_id]/index.jsx index 320042f..cc85b4f 100644 --- a/pages/manuals/[_id]/index.jsx +++ b/pages/manuals/[_id]/index.jsx @@ -1,4 +1,4 @@ -import dbConnect from '../../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; import { getTricks, requireAuth } from '../../../lib/serverUtils'; import Manual from '../../../models/Manual'; import { isValidObjectId, Model } from 'mongoose'; @@ -15,6 +15,8 @@ export async function getServerSideProps({ params, req, res }) { if (!manual) return { notFound: true }; + await dbDisconnect(); + return { props: { manual } }; } diff --git a/pages/profile/index.jsx b/pages/profile/index.jsx index a482503..39af225 100644 --- a/pages/profile/index.jsx +++ b/pages/profile/index.jsx @@ -2,7 +2,7 @@ import { getServerSession } from 'next-auth'; import { authOptions } from '../api/auth/[...nextauth]'; import ProfileForm from '../../components/forms/ProfileForm'; import { ensureProfile, serialize } from '../../lib/serverUtils'; -import dbConnect from '../../lib/dbConnect'; +import dbConnect, { dbDisconnect } from '../../lib/dbConnect'; export async function getServerSideProps(context) { await dbConnect(); @@ -12,6 +12,8 @@ export async function getServerSideProps(context) { const profile = serialize(await ensureProfile({ ...query })); + await dbDisconnect(); + return { props: { profile, From 803c3f3fda5633d9c60ceaa9e7f230052de5bfee Mon Sep 17 00:00:00 2001 From: Jan-Willem van Bremen Date: Thu, 25 Apr 2024 01:29:26 +0200 Subject: [PATCH 03/76] Revert "Added db disconnect" This reverts commit 428251f228b96624093dfb495a3f07499b7d0bfe. --- lib/dbConnect.js | 10 ---------- lib/migrations/populateManualStances.js | 4 +--- pages/api/combos/[_id].js | 4 +--- pages/api/combos/index.js | 4 +--- pages/api/combos/search/index.js | 5 ++--- pages/api/flatgroundtricks/[_id].js | 4 +--- pages/api/flatgroundtricks/index.js | 6 ++---- pages/api/grinds/[_id].js | 4 +--- pages/api/grinds/index.js | 4 +--- pages/api/manuals/[_id].js | 4 +--- pages/api/manuals/index.js | 4 +--- pages/api/profiles/index.js | 4 +--- pages/api/profiles/mine/[endpoint].js | 4 +--- pages/api/profiles/mine/index.js | 5 ++--- pages/api/stats/[endpoint].js | 4 +--- pages/api/stats/mine/[endpoint].js | 4 +--- pages/api/stats/mine/combos/[stat].js | 4 +--- pages/api/stats/mine/profile/[stat].js | 4 +--- pages/api/stats/mine/stance/[stance].js | 4 +--- pages/combos/[_id]/index.jsx | 4 +--- pages/flatgroundtricks/[_id]/index.jsx | 4 +--- pages/grinds/[_id]/index.jsx | 4 +--- pages/manuals/[_id]/index.jsx | 4 +--- pages/profile/index.jsx | 4 +--- 24 files changed, 26 insertions(+), 80 deletions(-) diff --git a/lib/dbConnect.js b/lib/dbConnect.js index 4969b41..cf714cc 100644 --- a/lib/dbConnect.js +++ b/lib/dbConnect.js @@ -34,14 +34,4 @@ async function dbConnect() { return cached.conn; } -export async function dbDisconnect() { - if (process.env.NODE_ENV === 'development') return; - - if (cached.conn) { - await cached.conn.disconnect(); - console.log('Disconnected from database 🚪'); - cached.conn = null; - } -} - export default dbConnect; diff --git a/lib/migrations/populateManualStances.js b/lib/migrations/populateManualStances.js index 1d25ef3..c976e89 100644 --- a/lib/migrations/populateManualStances.js +++ b/lib/migrations/populateManualStances.js @@ -1,5 +1,5 @@ import './envSetup.js'; -import dbConnect, { dbDisconnect } from '../dbConnect.js'; +import dbConnect from '../dbConnect.js'; import Manual from '../../models/Manual.js'; // Set "type": module" in package.json to run this script @@ -15,8 +15,6 @@ const run = async () => { count++; } - await dbDisconnect(); - console.info(`Populated ${count} manual stances`); }; diff --git a/pages/api/combos/[_id].js b/pages/api/combos/[_id].js index 1b7be7e..db627db 100644 --- a/pages/api/combos/[_id].js +++ b/pages/api/combos/[_id].js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; +import dbConnect from '../../../lib/dbConnect'; import Combo from '../../../models/Combo'; import { populateComboTrickName } from '../../../lib/commonUtils'; import { requireAuth, notFoundHandler } from '../../../lib/serverUtils'; @@ -56,6 +56,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/combos/index.js b/pages/api/combos/index.js index dc1c80b..08dccb8 100644 --- a/pages/api/combos/index.js +++ b/pages/api/combos/index.js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; +import dbConnect from '../../../lib/dbConnect'; import Combo from '../../../models/Combo'; import { populateComboName, populateComboTrickName } from '../../../lib/commonUtils'; import { requireAuth } from '../../../lib/serverUtils'; @@ -39,6 +39,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/combos/search/index.js b/pages/api/combos/search/index.js index a4f0e91..3ea908f 100644 --- a/pages/api/combos/search/index.js +++ b/pages/api/combos/search/index.js @@ -1,5 +1,6 @@ -import dbConnect, { dbDisconnect } from '../../../../lib/dbConnect'; +import dbConnect from '../../../../lib/dbConnect'; import Combo from '../../../../models/Combo'; +import { authOptions } from '../../auth/[...nextauth]'; import { populateComboName, populateComboTrickName } from '../../../../lib/commonUtils'; import { requireAuth } from '../../../../lib/serverUtils'; import Manual from '../../../../models/Manual'; @@ -43,6 +44,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/flatgroundtricks/[_id].js b/pages/api/flatgroundtricks/[_id].js index ba2e938..93f6215 100644 --- a/pages/api/flatgroundtricks/[_id].js +++ b/pages/api/flatgroundtricks/[_id].js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; +import dbConnect from '../../../lib/dbConnect'; import FlatGroundTrick from '../../../models/FlatgroundTrick'; import { getFullTrickName } from '../../../lib/commonUtils'; import { checkForUsedCombos, requireAuth, notFoundHandler } from '../../../lib/serverUtils'; @@ -56,6 +56,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/flatgroundtricks/index.js b/pages/api/flatgroundtricks/index.js index 531dbb9..40bd2c2 100644 --- a/pages/api/flatgroundtricks/index.js +++ b/pages/api/flatgroundtricks/index.js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; +import dbConnect from '../../../lib/dbConnect'; import FlatgroundTrick from '../../../models/FlatgroundTrick'; import { getFullTrickName } from '../../../lib/commonUtils'; import { requireAuth } from '../../../lib/serverUtils'; @@ -17,7 +17,7 @@ export default async function handler(req, res) { ...flatgroundTrick, trick: getFullTrickName(flatgroundTrick), })); - res.status(200).json({ success: true, data }); + return res.status(200).json({ success: true, data }); } catch (error) { console.error(error); res.status(400).json({ success: false, error: error.message }); @@ -39,6 +39,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/grinds/[_id].js b/pages/api/grinds/[_id].js index f2fafdf..53670f7 100644 --- a/pages/api/grinds/[_id].js +++ b/pages/api/grinds/[_id].js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; +import dbConnect from '../../../lib/dbConnect'; import Grind from '../../../models/Grind'; import { getFullGrindName } from '../../../lib/commonUtils'; import { checkForUsedCombos, requireAuth, notFoundHandler } from '../../../lib/serverUtils'; @@ -59,6 +59,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/grinds/index.js b/pages/api/grinds/index.js index 8c0b9bd..8f522db 100644 --- a/pages/api/grinds/index.js +++ b/pages/api/grinds/index.js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; +import dbConnect from '../../../lib/dbConnect'; import Grind from '../../../models/Grind'; import { getFullGrindName } from '../../../lib/commonUtils'; import { requireAuth } from '../../../lib/serverUtils'; @@ -36,6 +36,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/manuals/[_id].js b/pages/api/manuals/[_id].js index de70f83..b200e1d 100644 --- a/pages/api/manuals/[_id].js +++ b/pages/api/manuals/[_id].js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; +import dbConnect from '../../../lib/dbConnect'; import Manual from '../../../models/Manual'; import { checkForUsedCombos, requireAuth, notFoundHandler } from '../../../lib/serverUtils'; import { isValidObjectId } from 'mongoose'; @@ -53,6 +53,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/manuals/index.js b/pages/api/manuals/index.js index cc4f84e..7157642 100644 --- a/pages/api/manuals/index.js +++ b/pages/api/manuals/index.js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; +import dbConnect from '../../../lib/dbConnect'; import Manual from '../../../models/Manual'; import { getFullManualName } from '../../../lib/commonUtils'; import { requireAuth } from '../../../lib/serverUtils'; @@ -36,6 +36,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/profiles/index.js b/pages/api/profiles/index.js index 1e03608..88a5fea 100644 --- a/pages/api/profiles/index.js +++ b/pages/api/profiles/index.js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; +import dbConnect from '../../../lib/dbConnect'; import Profile from '../../../models/Profile'; export default async function handler(req, res) { @@ -20,6 +20,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/profiles/mine/[endpoint].js b/pages/api/profiles/mine/[endpoint].js index 0f31822..26dee75 100644 --- a/pages/api/profiles/mine/[endpoint].js +++ b/pages/api/profiles/mine/[endpoint].js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../../lib/dbConnect'; +import dbConnect from '../../../../lib/dbConnect'; import { ensureProfile, requireAuth } from '../../../../lib/serverUtils'; export default async function handler(req, res) { @@ -31,6 +31,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/profiles/mine/index.js b/pages/api/profiles/mine/index.js index 8edf3f6..12e88f5 100644 --- a/pages/api/profiles/mine/index.js +++ b/pages/api/profiles/mine/index.js @@ -1,5 +1,6 @@ -import dbConnect, { dbDisconnect } from '../../../../lib/dbConnect'; +import dbConnect from '../../../../lib/dbConnect'; import { ensureProfile, requireAuth, notFoundHandler } from '../../../../lib/serverUtils'; +import { authOptions } from '../../auth/[...nextauth]'; import Profile from '../../../../models/Profile'; export default async function handler(req, res) { @@ -32,6 +33,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/stats/[endpoint].js b/pages/api/stats/[endpoint].js index ee6c53a..21de99e 100644 --- a/pages/api/stats/[endpoint].js +++ b/pages/api/stats/[endpoint].js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; +import dbConnect from '../../../lib/dbConnect'; import Combo from '../../../models/Combo'; import FlatgroundTrick from '../../../models/FlatgroundTrick'; import Grind from '../../../models/Grind'; @@ -76,6 +76,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/stats/mine/[endpoint].js b/pages/api/stats/mine/[endpoint].js index d115fa5..402d347 100644 --- a/pages/api/stats/mine/[endpoint].js +++ b/pages/api/stats/mine/[endpoint].js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../../lib/dbConnect'; +import dbConnect from '../../../../lib/dbConnect'; import Combo from '../../../../models/Combo'; import FlatgroundTrick from '../../../../models/FlatgroundTrick'; import Grind from '../../../../models/Grind'; @@ -62,6 +62,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${method}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/stats/mine/combos/[stat].js b/pages/api/stats/mine/combos/[stat].js index 6ba547f..0843da3 100644 --- a/pages/api/stats/mine/combos/[stat].js +++ b/pages/api/stats/mine/combos/[stat].js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../../../lib/dbConnect'; +import dbConnect from '../../../../../lib/dbConnect'; import Combo from '../../../../../models/Combo'; import { requireAuth } from '../../../../../lib/serverUtils'; import { TRICK_TYPES_MODELS } from '../../../../../models/constants/trickTypes'; @@ -61,6 +61,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${stat}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/stats/mine/profile/[stat].js b/pages/api/stats/mine/profile/[stat].js index b8b1d0f..c9897a8 100644 --- a/pages/api/stats/mine/profile/[stat].js +++ b/pages/api/stats/mine/profile/[stat].js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../../../lib/dbConnect'; +import dbConnect from '../../../../../lib/dbConnect'; import { requireAuth } from '../../../../../lib/serverUtils'; import Profile from '../../../../../models/Profile'; import { formatDate } from '../../../../../lib/commonUtils'; @@ -44,6 +44,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${stat}` }); break; } - - await dbDisconnect(); } diff --git a/pages/api/stats/mine/stance/[stance].js b/pages/api/stats/mine/stance/[stance].js index fd419f1..5f568ed 100644 --- a/pages/api/stats/mine/stance/[stance].js +++ b/pages/api/stats/mine/stance/[stance].js @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../../../lib/dbConnect'; +import dbConnect from '../../../../../lib/dbConnect'; import Combo from '../../../../../models/Combo'; import FlatgroundTrick from '../../../../../models/FlatgroundTrick'; import Grind from '../../../../../models/Grind'; @@ -58,6 +58,4 @@ export default async function handler(req, res) { res.status(400).json({ success: false, error: `Unhandled request method: ${stance}` }); break; } - - await dbDisconnect(); } diff --git a/pages/combos/[_id]/index.jsx b/pages/combos/[_id]/index.jsx index 46c6b4d..1f089c6 100644 --- a/pages/combos/[_id]/index.jsx +++ b/pages/combos/[_id]/index.jsx @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; +import dbConnect from '../../../lib/dbConnect'; import { getCombos, requireAuth } from '../../../lib/serverUtils'; import Combo from '../../../models/Combo'; import { isValidObjectId, Model } from 'mongoose'; @@ -15,8 +15,6 @@ export async function getServerSideProps({ params, req, res }) { if (!combo) return { notFound: true }; - await dbDisconnect(); - return { props: { combo } }; } diff --git a/pages/flatgroundtricks/[_id]/index.jsx b/pages/flatgroundtricks/[_id]/index.jsx index 492af7b..3054860 100644 --- a/pages/flatgroundtricks/[_id]/index.jsx +++ b/pages/flatgroundtricks/[_id]/index.jsx @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; +import dbConnect from '../../../lib/dbConnect'; import { getTricks, requireAuth } from '../../../lib/serverUtils'; import FlatgroundTrickDetails from '../../../components/cards/FlatgroundTrickDetails'; import FlatGroundTrick from '../../../models/FlatgroundTrick'; @@ -15,8 +15,6 @@ export async function getServerSideProps({ params, req, res }) { if (!flatgroundTrick) return { notFound: true }; - await dbDisconnect(); - return { props: { flatgroundTrick } }; } diff --git a/pages/grinds/[_id]/index.jsx b/pages/grinds/[_id]/index.jsx index deb1591..45d8f40 100644 --- a/pages/grinds/[_id]/index.jsx +++ b/pages/grinds/[_id]/index.jsx @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; +import dbConnect from '../../../lib/dbConnect'; import { getTricks, requireAuth } from '../../../lib/serverUtils'; import GrindDetails from '../../../components/cards/GrindDetails'; import Grind from '../../../models/Grind'; @@ -15,8 +15,6 @@ export async function getServerSideProps({ params, req, res }) { if (!grind) return { notFound: true }; - await dbDisconnect(); - return { props: { grind } }; } diff --git a/pages/manuals/[_id]/index.jsx b/pages/manuals/[_id]/index.jsx index cc85b4f..320042f 100644 --- a/pages/manuals/[_id]/index.jsx +++ b/pages/manuals/[_id]/index.jsx @@ -1,4 +1,4 @@ -import dbConnect, { dbDisconnect } from '../../../lib/dbConnect'; +import dbConnect from '../../../lib/dbConnect'; import { getTricks, requireAuth } from '../../../lib/serverUtils'; import Manual from '../../../models/Manual'; import { isValidObjectId, Model } from 'mongoose'; @@ -15,8 +15,6 @@ export async function getServerSideProps({ params, req, res }) { if (!manual) return { notFound: true }; - await dbDisconnect(); - return { props: { manual } }; } diff --git a/pages/profile/index.jsx b/pages/profile/index.jsx index 39af225..a482503 100644 --- a/pages/profile/index.jsx +++ b/pages/profile/index.jsx @@ -2,7 +2,7 @@ import { getServerSession } from 'next-auth'; import { authOptions } from '../api/auth/[...nextauth]'; import ProfileForm from '../../components/forms/ProfileForm'; import { ensureProfile, serialize } from '../../lib/serverUtils'; -import dbConnect, { dbDisconnect } from '../../lib/dbConnect'; +import dbConnect from '../../lib/dbConnect'; export async function getServerSideProps(context) { await dbConnect(); @@ -12,8 +12,6 @@ export async function getServerSideProps(context) { const profile = serialize(await ensureProfile({ ...query })); - await dbDisconnect(); - return { props: { profile, From e1f0dbfdc837ac3a876c301f73107bd6a28583d7 Mon Sep 17 00:00:00 2001 From: Jan-Willem van Bremen Date: Thu, 25 Apr 2024 01:29:26 +0200 Subject: [PATCH 04/76] Revert "Add return" This reverts commit 79b3e540e5a1e8b88281b0624c6499308da9394a. --- pages/api/flatgroundtricks/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/api/flatgroundtricks/index.js b/pages/api/flatgroundtricks/index.js index 40bd2c2..92ad5ee 100644 --- a/pages/api/flatgroundtricks/index.js +++ b/pages/api/flatgroundtricks/index.js @@ -17,7 +17,7 @@ export default async function handler(req, res) { ...flatgroundTrick, trick: getFullTrickName(flatgroundTrick), })); - return res.status(200).json({ success: true, data }); + res.status(200).json({ success: true, data }); } catch (error) { console.error(error); res.status(400).json({ success: false, error: error.message }); From 4197f9f147fb484d6db1a3684797bafb9957ec6d Mon Sep 17 00:00:00 2001 From: Jan-Willem van Bremen Date: Fri, 26 Apr 2024 04:08:56 +0200 Subject: [PATCH 05/76] Improve trick naming --- __tests__/lib/capitalize.test.js | 10 ++++++++++ __tests__/lib/trickNames.test.js | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 __tests__/lib/capitalize.test.js diff --git a/__tests__/lib/capitalize.test.js b/__tests__/lib/capitalize.test.js new file mode 100644 index 0000000..9a17724 --- /dev/null +++ b/__tests__/lib/capitalize.test.js @@ -0,0 +1,10 @@ +import { capitalize } from '../../lib/commonUtils'; + +describe('capitalize', () => { + it('should be able to capitalize one word', () => { + expect(capitalize('ollie')).toBe('Ollie'); + }); + it('should be able to capitalize multiple words PascalCase style', () => { + expect(capitalize('ollie Kickflip treflip', true)).toBe('Ollie Kickflip Treflip'); + }); +}); diff --git a/__tests__/lib/trickNames.test.js b/__tests__/lib/trickNames.test.js index f6d85a4..c4fca1f 100644 --- a/__tests__/lib/trickNames.test.js +++ b/__tests__/lib/trickNames.test.js @@ -29,8 +29,9 @@ describe('Should resolve trick names correctly', () => { ${{ stance: fakie, direction: none, rotation: '0', name: heelflip }} | ${'Fakie Heelflip'} ${{ stance: regular, direction: backside, rotation: '360', name: kickflip }} | ${'Backside 360 Kickflip'} ${{ stance: regular, direction: backside, rotation: '180', name: shove_it }} | ${'Bigspin'} - ${{ stance: regular, direction: backside, rotation: '180', name: shove_it }} | ${'Bigspin'} - ${{ stance: regular, direction: frontside, rotation: '360', name: shove_it }} | ${'Biggerspin'} + ${{ stance: regular, direction: frontside, rotation: '360', name: shove_it }} | ${'Frontside Biggerspin'} + ${{ stance: regular, direction: frontside, rotation: '180', name: shove_it }} | ${'Frontside Bigspin'} + ${{ stance: regular, direction: frontside, rotation: '180', name: varial_heelflip }} | ${'Bigspin Heelflip'} ${{ stance: regular, direction: backside, rotation: '180', name: varial_kickflip }} | ${'Bigspin Kickflip'} ${{ stance: regular, direction: backside, rotation: '360', name: varial_kickflip }} | ${'Biggerflip'} ${{ stance: regular, direction: frontside, rotation: '180', name: varial_heelflip }} | ${'Bigspin Heelflip'} @@ -38,6 +39,7 @@ describe('Should resolve trick names correctly', () => { ${{ stance: nollie, direction: backside, rotation: '180', name: varial_heelflip }} | ${'Nollie Bigspin Heelflip'} ${{ stance: fakie, direction: backside, rotation: '180', name: varial_kickflip }} | ${'Fakie Bigspin Kickflip'} ${{ stance: nollie, direction: frontside, rotation: '180', name: varial_kickflip }} | ${'Nollie Bigspin Kickflip'} + ${{ stance: nollie, direction: none, rotation: '0', name: ollie }} | ${'Nollie'} `('should resolve trick name correctly: $expected', ({ trick, expected }) => { expect(getFullTrickName(trick)).toBe(expected); }); From 32a55cbeb43aa4952d3d7ca6f88c04294bd54df9 Mon Sep 17 00:00:00 2001 From: Jan-Willem van Bremen Date: Fri, 26 Apr 2024 04:09:05 +0200 Subject: [PATCH 06/76] Added AddAnotherCheckBox.jsx component --- components/common/AddAnotherCheckBox.jsx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 components/common/AddAnotherCheckBox.jsx diff --git a/components/common/AddAnotherCheckBox.jsx b/components/common/AddAnotherCheckBox.jsx new file mode 100644 index 0000000..25789d6 --- /dev/null +++ b/components/common/AddAnotherCheckBox.jsx @@ -0,0 +1,16 @@ +function AddAnotherCheckBox({ checked, onChange }) { + return ( + + ); +} + +export default AddAnotherCheckBox; From 77cc62a318364ff1a2dcfa1faa0078995cd2ae85 Mon Sep 17 00:00:00 2001 From: Jan-Willem van Bremen Date: Fri, 26 Apr 2024 04:09:35 +0200 Subject: [PATCH 07/76] Added AddAnotherCheckBox to forms and added GenerateComboName.jsx component --- components/forms/ComboForm.jsx | 49 +++++++++--------------- components/forms/FlatgroundTrickForm.jsx | 16 +++++++- components/forms/GenerateComboName.jsx | 39 +++++++++++++++++++ components/forms/GrindForm.jsx | 15 +++++++- components/forms/ManualForm.jsx | 2 +- 5 files changed, 86 insertions(+), 35 deletions(-) create mode 100644 components/forms/GenerateComboName.jsx diff --git a/components/forms/ComboForm.jsx b/components/forms/ComboForm.jsx index 74ff917..bb244fd 100644 --- a/components/forms/ComboForm.jsx +++ b/components/forms/ComboForm.jsx @@ -25,6 +25,10 @@ import LoaderButton from '../common/LoaderButton'; import { apiCall, baseStyle, hiddenStyle } from '../../lib/clientUtils'; import Link from 'next/link'; import TransitionScroll from 'react-transition-scroll'; +import { newComboObj } from '../../pages/new-combo'; +import AddAnotherCheckBox from '../common/AddAnotherCheckBox'; +import GenerateComboName from './GenerateComboName'; + const { TRICK_TYPES_MODELS: TRICK_TYPES_MODELS_CONSTANT } = require('../../models/constants/trickTypes'); const TRICK_TYPES_MAP = { @@ -74,19 +78,16 @@ const ComboForm = ({ combo, newCombo = true }) => { const [stance, setStance] = useState('all'); const [direction, setDirection] = useState('all'); const [loading, setLoading] = useState(true); - const [trickArrayRef] = useAutoAnimate(); const [tricksRef] = useAutoAnimate(); const [form, setForm] = useState({ trickArray: combo.trickArray }); - const [dots, setDots] = useState('...'); const [searchString, setSearchString] = useState(''); const [searchActive, setSearchActive] = useState(false); + const [addAnother, setAddAnother] = useState(false); const { trickArray } = form; useAsyncEffect(async () => { for (const trickType of TRICK_TYPES) await fetchTrickType(trickType); // Fetch all trick types - const interval = setInterval(() => setDots((prev) => (prev === '...' ? '.' : prev + '.')), 1000); - return () => clearInterval(interval); }, []); useEffect(() => { @@ -135,7 +136,11 @@ const ComboForm = ({ combo, newCombo = true }) => { method: 'POST', data: { ...form, trickArray: trickArray.map(({ _id, trickRef }) => ({ trick: _id, trickRef })) }, }); - await router.back(); + if (addAnother) { + setForm(newComboObj); + } else { + await router.back(); + } closeAfterAdd(); toast.success(`Successfully created combo: ${getFullComboName(form)}`); } catch (error) { @@ -205,37 +210,18 @@ const ComboForm = ({ combo, newCombo = true }) => { {/*Trick name*/} -
- {trickArray.map((trick, index) => ( -
- {trick.trick} - {trickArray[index + 1] ? ( - - ) : ( - trick.trickRef === TRICK_TYPES_MODELS[TRICK_TYPES_MAP.flatground] && - trickArray.length > 1 && Out - )} - {trickArray.length === 1 && ( - - - {dots} - - )} -
- ))} - {!trickArray.length && Select first trick{dots}} -
+