Skip to content

Commit

Permalink
Use absolute imports and move files
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangvvo committed Nov 25, 2020
1 parent a5eb61b commit 78c8379
Show file tree
Hide file tree
Showing 27 changed files with 91 additions and 77 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
]
}
],
"jsx-a11y/click-events-have-key-events": "off",
"react/prop-types": "off",
"react/jsx-props-no-spreading": "off",
"import/prefer-default-export": "off",
"no-param-reassign": "off",
"no-nested-ternary": "off"
"no-nested-ternary": "off",
"import/no-unresolved": "off"
}
}
2 changes: 1 addition & 1 deletion components/layout.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import Head from 'next/head';
import Link from 'next/link';
import { useCurrentUser } from '../lib/hooks';
import { useCurrentUser } from '@/hooks/index';

export default function Layout({ children }) {
const [user, { mutate }] = useCurrentUser();
Expand Down
2 changes: 1 addition & 1 deletion components/post/editor.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { useCurrentUser } from '../../lib/hooks';
import { useCurrentUser } from '@/hooks/index';

export default function PostEditor() {
const [user] = useCurrentUser();
Expand Down
4 changes: 2 additions & 2 deletions components/post/posts.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { useSWRInfinite } from 'swr';
import Link from 'next/link';
import { useUser } from '../../lib/hooks';
import fetcher from '../../lib/fetch';
import { useUser } from '@/hooks/index';
import fetcher from '@/lib/fetch';

function Post({ post }) {
const user = useUser(post.creatorId);
Expand Down
1 change: 1 addition & 0 deletions hooks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './user';
2 changes: 1 addition & 1 deletion lib/hooks.jsx → hooks/user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import useSWR from 'swr';
import fetcher from './fetch';
import fetcher from '@/lib/fetch';

export function useCurrentUser() {
const { data, mutate } = useSWR('/api/user', fetcher);
Expand Down
11 changes: 11 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/components/*": ["components/*"],
"@/lib/*": ["lib/*"],
"@/middlewares/*": ["middlewares/*"],
"@/hooks/*": ["hooks/*"]
}
}
}
10 changes: 10 additions & 0 deletions middlewares/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import nc from 'next-connect';
import passport from 'middlewares/passport';
import database from './database';
import session from './session';

const all = nc();

all.use(database).use(session).use(passport.initialize()).use(passport.session());

export default all;
3 changes: 3 additions & 0 deletions middlewares/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as database } from './database';
export { default as all } from './all';
export { default as session } from './session';
10 changes: 0 additions & 10 deletions middlewares/middleware.js

This file was deleted.

File renamed without changes.
2 changes: 1 addition & 1 deletion pages/_app.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import Head from 'next/head';
import Layout from '../components/layout';
import Layout from '@/components/layout';

export default function MyApp({ Component, pageProps }) {
return (
Expand Down
12 changes: 6 additions & 6 deletions pages/api/auth.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import nextConnect from 'next-connect';
import middleware from '../../middlewares/middleware';
import passport from '../../lib/passport';
import { extractUser } from '../../lib/api-helpers';
import nc from 'next-connect';
import { all } from '@/middlewares/index';
import passport from 'middlewares/passport';
import { extractUser } from '@/lib/api-helpers';

const handler = nextConnect();
const handler = nc();

handler.use(middleware);
handler.use(all);

handler.post(passport.authenticate('local'), (req, res) => {
res.json({ user: extractUser(req) });
Expand Down
8 changes: 4 additions & 4 deletions pages/api/posts/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import nextConnect from 'next-connect';
import nc from 'next-connect';
import { nanoid } from 'nanoid';
import middleware from '../../../middlewares/middleware';
import { all } from '@/middlewares/index';

const handler = nextConnect();
const handler = nc();

handler.use(middleware);
handler.use(all);

handler.get(async (req, res) => {
// Pagination: Fetch posts from before the input date or fetch from newest
Expand Down
10 changes: 5 additions & 5 deletions pages/api/user/email/verify.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import crypto from 'crypto';
import nextConnect from 'next-connect';
import { sendMail } from '../../../../lib/mail';
import middleware from '../../../../middlewares/middleware';
import nc from 'next-connect';
import { sendMail } from '@/lib/mail';
import { all } from '@/middlewares/index';

const handler = nextConnect();
const handler = nc();

handler.use(middleware);
handler.use(all);

handler.post(async (req, res) => {
if (!req.user) { res.json(401).send('you need to be authenticated'); return; }
Expand Down
10 changes: 5 additions & 5 deletions pages/api/user/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import nextConnect from 'next-connect';
import nc from 'next-connect';
import multer from 'multer';
import { v2 as cloudinary } from 'cloudinary';
import middleware from '../../../middlewares/middleware';
import { extractUser } from '../../../lib/api-helpers';
import { all } from '@/middlewares/index';
import { extractUser } from '@/lib/api-helpers';

const upload = multer({ dest: '/tmp' });
const handler = nextConnect();
const handler = nc();

/* eslint-disable camelcase */
const {
Expand All @@ -20,7 +20,7 @@ cloudinary.config({
api_secret,
});

handler.use(middleware);
handler.use(all);

handler.get(async (req, res) => res.json({ user: extractUser(req) }));

Expand Down
8 changes: 4 additions & 4 deletions pages/api/user/password/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import nextConnect from 'next-connect';
import nc from 'next-connect';
import bcrypt from 'bcryptjs';
import middleware from '../../../../middlewares/middleware';
import { all } from '@/middlewares/index';

const handler = nextConnect();
handler.use(middleware);
const handler = nc();
handler.use(all);

handler.put(async (req, res) => {
if (!req.user) { res.json(401).send('you need to be authenticated'); return; }
Expand Down
8 changes: 4 additions & 4 deletions pages/api/user/password/reset.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import crypto from 'crypto';
import bcrypt from 'bcryptjs';
import nextConnect from 'next-connect';
import { sendMail } from '../../../../lib/mail';
import database from '../../../../middlewares/database';
import nc from 'next-connect';
import { sendMail } from '@/lib/mail';
import { database } from '@/middlewares/index';

const handler = nextConnect();
const handler = nc();

handler.use(database);

Expand Down
10 changes: 5 additions & 5 deletions pages/api/users.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import nextConnect from 'next-connect';
import nc from 'next-connect';
import isEmail from 'validator/lib/isEmail';
import normalizeEmail from 'validator/lib/normalizeEmail';
import bcrypt from 'bcryptjs';
import { nanoid } from 'nanoid';
import middleware from '../../middlewares/middleware';
import { extractUser } from '../../lib/api-helpers';
import { all } from '@/middlewares/index';
import { extractUser } from '@/lib/api-helpers';

const handler = nextConnect();
const handler = nc();

handler.use(middleware);
handler.use(all);

handler.post(async (req, res) => {
const { name, password } = req.body;
Expand Down
11 changes: 5 additions & 6 deletions pages/api/users/[userId]/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import nc from 'next-connect';
import { all } from '@/middlewares/index';
import { getUser } from '@/lib/db';

import nextConnect from 'next-connect';
import middleware from '../../../../middlewares/middleware';
import { getUser } from '../../../../lib/db';
const handler = nc();

const handler = nextConnect();

handler.use(middleware);
handler.use(all);

handler.get(async (req, res) => {
const user = await getUser(req, req.query.userId);
Expand Down
8 changes: 4 additions & 4 deletions pages/forget-password/[token].jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import Head from 'next/head';
import nextConnect from 'next-connect';
import nc from 'next-connect';
import Router from 'next/router';
import database from '../../middlewares/database';
import { database } from '@/middlewares/index';

const ResetPasswordTokenPage = ({ valid, token }) => {
async function handleSubmit(event) {
Expand Down Expand Up @@ -56,9 +56,9 @@ const ResetPasswordTokenPage = ({ valid, token }) => {
};

export async function getServerSideProps(ctx) {
const handler = nextConnect();
const handler = nc();
handler.use(database);
await handler.apply(ctx.req, ctx.res);
await handler.run(ctx.req, ctx.res);
const { token } = ctx.query;

const tokenDoc = await ctx.req.db.collection('tokens').findOne({
Expand Down
6 changes: 3 additions & 3 deletions pages/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { useCurrentUser } from '../lib/hooks';
import PostEditor from '../components/post/editor';
import Posts from '../components/post/posts';
import { useCurrentUser } from '@/hooks/index';
import PostEditor from '@/components/post/editor';
import Posts from '@/components/post/posts';

const IndexPage = () => {
const [user] = useCurrentUser();
Expand Down
2 changes: 1 addition & 1 deletion pages/login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useEffect } from 'react';
import Head from 'next/head';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { useCurrentUser } from '../lib/hooks';
import { useCurrentUser } from '@/hooks/index';

const LoginPage = () => {
const router = useRouter();
Expand Down
2 changes: 1 addition & 1 deletion pages/settings.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useEffect, useRef } from 'react';
import Head from 'next/head';
import { useCurrentUser } from '../lib/hooks';
import { useCurrentUser } from '@/hooks/index';

const ProfileSection = () => {
const [user, { mutate }] = useCurrentUser();
Expand Down
2 changes: 1 addition & 1 deletion pages/signup.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useEffect } from 'react';
import Head from 'next/head';
import Router from 'next/router';
import { useCurrentUser } from '../lib/hooks';
import { useCurrentUser } from '@/hooks/index';

const SignupPage = () => {
const [user, { mutate }] = useCurrentUser();
Expand Down
10 changes: 5 additions & 5 deletions pages/user/[userId]/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React from 'react';
import Head from 'next/head';
import Link from 'next/link';
import Error from 'next/error';
import middleware from '../../../middlewares/middleware';
import { useCurrentUser } from '../../../lib/hooks';
import Posts from '../../../components/post/posts';
import { getUser } from '../../../lib/db';
import { all } from '@/middlewares/index';
import { useCurrentUser } from '@/hooks/index';
import Posts from '@/components/post/posts';
import { getUser } from '@/lib/db';

export default function UserPage({ user }) {
if (!user) return <Error statusCode={404} />;
Expand Down Expand Up @@ -77,7 +77,7 @@ export default function UserPage({ user }) {
}

export async function getServerSideProps(context) {
await middleware.apply(context.req, context.res);
await all.run(context.req, context.res);
const user = await getUser(context.req, context.params.userId);
if (!user) context.res.statusCode = 404;
return {
Expand Down
10 changes: 5 additions & 5 deletions pages/verify-email/[token].jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import Head from 'next/head';
import nextConnect from 'next-connect';
import database from '../../middlewares/database';
import nc from 'next-connect';
import { all } from '@/middlewares/index';

export default function EmailVerifyPage({ success }) {
return (
Expand All @@ -22,9 +22,9 @@ export default function EmailVerifyPage({ success }) {
}

export async function getServerSideProps(ctx) {
const handler = nextConnect();
handler.use(database);
await handler.apply(ctx.req, ctx.res);
const handler = nc();
handler.use(all);
await handler.run(ctx.req, ctx.res);

const { token } = ctx.query;
const { value: tokenDoc } = await ctx.req.db
Expand Down

1 comment on commit 78c8379

@vercel
Copy link

@vercel vercel bot commented on 78c8379 Nov 25, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.