Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip mirating app router #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
12 changes: 9 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"cSpell.words": ["signin", "sobabase", "supabase"],
"commentTranslate.source": "Google"
}
"cSpell.words": [
"pico",
"signin",
"sobabase",
"supabase"
],
"commentTranslate.source": "Google",
"typescript.enablePromptUseWorkspaceTsdk": true
}
6 changes: 6 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
experimental: {
appDir: true,
serverActions: true,
},
};
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@
"next": "^13.0.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-error-boundary": "^4.0.9",
"react-hook-form": "^7.44.3",
"zod": "^3.21.4"
},
"devDependencies": {
"@babel/core": "^7.19.6",
"@types/node": "^18.11.9",
"@types/react": "^18.0.24",
"@types/react-dom": "^18.0.8",
"@types/react": "^18.2.8",
"@types/react-dom": "^18.2.4",
"dotenv": "^16.0.3",
"eslint": "^8.26.0",
"eslint-config-next": "13.4.4",
"generate-password": "^1.7.0",
"prettier": "^2.7.1",
"ts-loader": "^9.4.1",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
"typescript": "^5.1.3"
}
}
20 changes: 10 additions & 10 deletions src/styles/theme.css → src/app/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
:root {
--background-color: #11191f;
--color: #ffffff;
--h1-color: #00b478;
--h2-color: #00b478;
--h3-color: #00b478;
--h4-color: #00b478;
--h5-color: #00b478;
--h6-color: #00b478;
--h1-color: #00f064;
--h2-color: #00f064;
--h3-color: #00f064;
--h4-color: #00f064;
--h5-color: #00f064;
--h6-color: #00f064;
--muted-color: hsl(205, 10%, 50%);
--muted-border-color: #1f2d38;
--primary: var(--pico-primary);
--primary: #00c864;
--primary-hover: var(--pico-primary-hover);
--primary-focus: rgba(16, 149, 193, 0.25);
--primary-inverse: #fff;
Expand Down Expand Up @@ -51,10 +51,10 @@
--switch-color: var(--primary-inverse);
--switch-checked-background-color: var(--primary);
--pico-text-selection-color: rgba(0, 180, 120, 0.1875);
--pico-primary: #00b478;
--pico-primary: #00be00;
--pico-primary-background: #007a50;
--pico-primary-underline: rgba(0, 180, 120, 0.5);
--pico-primary-hover: #00cc88;
--pico-primary-hover: #00ce80;
--pico-primary-hover-background: #00895a;
--pico-primary-focus: rgba(0, 180, 120, 0.25);
--pico-primary-inverse: #fff;
Expand Down Expand Up @@ -115,4 +115,4 @@ dd {
display: flex;
flex-direction: column;
align-items: flex-end;
}
}
12 changes: 5 additions & 7 deletions src/pages/home.tsx → src/app/home/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
'use server';
import Head from 'next/head';
import Home from 'src/features/home';
import { UserProvider } from 'src/hooks/useUser';

const HomePage = () => {
export default async function HomePage() {
return (
<UserProvider>
<>
<Head>
<title>Sobabase | Home</title>
</Head>
<Home />
</UserProvider>
</>
);
};

export default HomePage;
}
41 changes: 41 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { PropsWithChildren } from 'react';
import Script from 'next/script';
import './global.css';

export default function RootLayout({ children }: PropsWithChildren) {
return (
<html lang="en" data-theme="dark">
<head>
<Script async src="https://www.googletagmanager.com/gtag/js?id=G-9G211QMFKT" />
<Script id="ga">{`
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-9G211QMFKT');
`}</Script>
<link
rel="stylesheet"
href="https://unpkg.com/@picocss/pico@latest/css/pico.min.css"
/>
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content="http://sobabase.vercel.app/image.jpg" />
<meta name="twitter:title" content="Sobabase" />
<meta name="twitter:description" content="My Supabase Sandbox" />
<meta property="og:image" content="https://sobabase.vercel.app/favicon.ico" />
<meta property="og:title" content="Sobabase" />
<meta property="og:description" content="My Supabase Sandbox" />
</head>
<body className="container">
<main>{children}</main>
<footer>
<small>
Built with <a href="https://picocss.com">Pico.css</a> •
<a href="https://github.com/caburchin/sobabase">Source code</a>
</small>
</footer>
</body>
</html>
);
}
File renamed without changes.
18 changes: 12 additions & 6 deletions src/pages/questionnaire.tsx → src/app/questionnaire/page.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { QueryClientProvider } from '@tanstack/react-query';
import { redirect } from 'next/navigation';
import Head from 'next/head';
import { QuestionnaireForm } from 'src/features/questonnaire';
import { UserProvider } from 'src/hooks/useUser';
import { getUser } from 'src/resources/user';

const QuestionnairePage = async () => {
const user = await getUser();

if (!user) {
redirect('/');
}

const QuestionnairePage = () => {
return (
<UserProvider>
<>
<Head>
<title>Sobabase | Questionnaire</title>
</Head>
<div className="container">
<h1>Questionnaire</h1>
<QuestionnaireForm />
<QuestionnaireForm user={user} />
</div>
</UserProvider>
</>
);
};

Expand Down
21 changes: 13 additions & 8 deletions src/features/home/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { useUser } from 'src/hooks/useUser';
import { useQuestionnaires } from 'src/resources/useQuestionaires';
'use server';
import { redirect } from 'next/navigation';
import { getQuestionnaires } from 'src/resources/useQuestionaires';
import { getUser } from 'src/resources/user';

export default async function Home() {
const user = await getUser();
const profiles = await getQuestionnaires(user?.id);

if (!user) {
redirect('/');
}

const Home = () => {
const user = useUser();
const profiles = useQuestionnaires(user?.id);
return (
<>
<h1>Hello {user.user_metadata.name}</h1>
Expand All @@ -26,6 +33,4 @@ const Home = () => {
</section>
</>
);
};

export default Home;
}
14 changes: 9 additions & 5 deletions src/features/questonnaire/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use client';
import { useForm } from 'react-hook-form';
import Head from 'next/head';
import { Questionnaire } from './types';
import { useUser } from 'src/hooks/useUser';
import { usePostQuestionnaire } from './usePostQuestionnaire';
import { User } from '@supabase/supabase-js';

const defaultValues: Questionnaire = {
name: '',
Expand All @@ -12,12 +12,16 @@ const defaultValues: Questionnaire = {
uid: '',
};

export const QuestionnaireForm = () => {
const user = useUser();
type Props = {
user: User;
};

export const QuestionnaireForm = ({ user }: Props) => {
const { register, handleSubmit } = useForm({
defaultValues: { ...defaultValues, uid: user?.id },
});
const { postQuestionnaire, isLoading, isSuccess, isError, error } = usePostQuestionnaire();
const { postQuestionnaire, isLoading, isSuccess, isError, error } =
usePostQuestionnaire();
const onSubmit = (data: Questionnaire) => {
postQuestionnaire(data);
};
Expand Down
3 changes: 2 additions & 1 deletion src/features/signin.component.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Head from 'next/head';
'use client';

import { useSignIn } from 'src/hooks/useSignIn';

export const SignInPage = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useSignIn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useRouter } from 'next/router';
import { ComponentProps, useState } from 'react';
import { supabase } from '../supabase';
import { useRouter } from 'next/navigation';

export const useSignIn = () => {
const [loading, setLoading] = useState(false);
Expand Down
32 changes: 0 additions & 32 deletions src/hooks/useUser.tsx

This file was deleted.

13 changes: 0 additions & 13 deletions src/pages/_app.tsx

This file was deleted.

40 changes: 0 additions & 40 deletions src/pages/_document.tsx

This file was deleted.

19 changes: 7 additions & 12 deletions src/resources/useQuestionaires.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { QuestionnaireSchema } from 'src/features/questonnaire/types';
import { useSuspense } from 'src/hooks/useSuspense';
import { supabase } from 'src/supabase';
import z from 'zod';
import ck from 'camelcase-keys';

const ref = {};

export const useQuestionnaires = (uid?: string) => {
return useSuspense(async () => {
if (!uid) return [];
const { data, error } = await supabase.from('questionnaires').select().eq('uid', uid);
if (error) throw error;
const result = z.array(QuestionnaireSchema).safeParse(ck(data, { deep: true }));
if (!result.success) throw result.error;
return result.data;
}, ref);
export const getQuestionnaires = async (uid?: string) => {
if (!uid) return [];
const { data, error } = await supabase.from('questionnaires').select().eq('uid', uid);
if (error) throw error;
const result = z.array(QuestionnaireSchema).safeParse(ck(data, { deep: true }));
if (!result.success) throw result.error;
return result.data;
};
10 changes: 10 additions & 0 deletions src/resources/user/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use server';
import { supabase } from 'src/supabase';

export const getUser = async () => {
const {
data: { user },
} = await supabase.auth.getUser();

return user;
};
Loading