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

DEPLOY #43

Merged
merged 7 commits into from
Jul 11, 2023
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@vercel/analytics": "1.0.1",
"autoprefixer": "10.4.14",
"axios": "1.4.0",
"clsx": "1.2.1",
"framer-motion": "10.12.18",
"next": "13.4.7",
"next-mdx-remote": "4.4.1",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions src/components/Toast.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// toast provider

import {
CheckCircleIcon,
ExclamationCircleIcon,
} from '@heroicons/react/24/solid';
import clsx from 'clsx';
import { AnimatePresence, motion } from 'framer-motion';
import { createContext, ReactNode, useContext, useState } from 'react';

interface IToastContext {
setToast: (type: 'error' | 'success', message: string) => void;
data: { message: string; type: 'error' | 'success' } | null;
}

const ToastContext = createContext<IToastContext | null>(null);

export const useToast = () => {
return useContext(ToastContext);
};

function getStyle(type: 'error' | 'success') {
switch (type) {
case 'error':
return 'bg-red-500';
case 'success':
return 'bg-green-500';
default:
return 'bg-blue-500';
}
}

export const ToastProvider = ({ children }: { children: ReactNode }) => {
const toast = useProvideToast();

return (
<ToastContext.Provider value={toast}>
{toast.data?.message && (
<AnimatePresence>
<motion.div
initial={{ opacity: 0, y: -300, x: '-50%' }}
animate={{ opacity: 1, y: 25, x: '-50%' }}
exit={{ opacity: 0, y: -300, x: '-50%' }}
className={clsx(
getStyle(toast.data?.type),
'fixed left-1/2 z-[101] rounded-full px-4 py-2 text-white'
)}
>
<div className="flex items-center space-x-1">
{toast.data?.type === 'error' && (
<ExclamationCircleIcon className="h-5 w-5" />
)}
{toast.data?.type === 'success' && (
<CheckCircleIcon className="h-5 w-5" />
)}
<span className="font-display text-sm">
{toast.data?.message}
</span>
</div>
</motion.div>
</AnimatePresence>
)}
{children}
</ToastContext.Provider>
);
};

const TOAST_TIMEOUT = 3000;

function useProvideToast() {
const [toast, setToast] = useState<null | {
message: string;
type: 'error' | 'success';
}>(null);

const setToastDisplay = (type: 'error' | 'success', message: string) => {
if (toast) {
setToast(null);
setTimeout(() => {
setToast({ type, message });

window.setTimeout(() => {
setToast(null);
}, TOAST_TIMEOUT);
}, 1000);
}

setToast({ type, message });
window.setTimeout(() => {
setToast(null);
}, TOAST_TIMEOUT);
};

return { setToast: setToastDisplay, data: toast };
}
5 changes: 5 additions & 0 deletions src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ const AuthProvider = ({ children }: { children: ReactNode }) => {

useEffect(() => {
const alreadyRegistered = user && user.email && user.email !== '';

if (isFetching.current) {
return;
}

switch (router.pathname) {
case '/':
if (user) {
Expand Down
41 changes: 23 additions & 18 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Analytics } from '@vercel/analytics/react';
import Head from 'next/head';
import Footer from '@/components/Footer';
import Navbar from '@/components/Navbar';
import { ToastProvider } from '@/components/Toast';

const ibmPlexSansThai = IBM_Plex_Sans_Thai({
subsets: ['latin'],
Expand Down Expand Up @@ -62,32 +63,36 @@ function MetaData() {
export default function App({ Component, pageProps }: AppProps) {
return (
<AuthProvider>
<MetaData />
<ToastProvider>
<MetaData />

<main className={`${ibmPlexSansThai.variable} font-ibm text-white`}>
<Navbar />
<Component {...pageProps} />
<Background />
<Footer />
</main>
<main
className={`${ibmPlexSansThai.variable} font-ibm text-white`}
>
<Navbar />
<Component {...pageProps} />
<Background />
<Footer />
</main>

<Analytics />
<Script
async
src="https://www.googletagmanager.com/gtag/js?id=G-0ZFDD1EKVW"
/>
<Script
id="google-analytics"
dangerouslySetInnerHTML={{
__html: `
<Analytics />
<Script
async
src="https://www.googletagmanager.com/gtag/js?id=G-0ZFDD1EKVW"
/>
<Script
id="google-analytics"
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-0ZFDD1EKVW');
`,
}}
/>
}}
/>
</ToastProvider>
</AuthProvider>
);
}
20 changes: 13 additions & 7 deletions src/pages/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { PencilSquareIcon, XMarkIcon } from '@heroicons/react/24/solid';
import Image from 'next/image';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { ChangeEvent, FormEvent, useState } from 'react';
import { ChangeEvent, FormEvent, useEffect, useState } from 'react';
import { AnimatePresence, motion } from 'framer-motion';
import { useToast } from '@/components/Toast';

const profilePicPlaceholderURL = '/images/pfp-placeholder.svg';

const Register = () => {
const toast = useToast();

const { user, refreshContext } = useAuth();
const router = useRouter();
const [bottleModal, setBottleModal] = useState(false);
Expand All @@ -21,7 +24,10 @@ const Register = () => {

async function handleImageUpload(file: File) {
const formData = new FormData();
formData.append('file', file);
const filename = `${Date.now()}.${file.name.split('.').pop()}${
user?.studentID
}`;
formData.append('file', file, filename);
formData.append('tag', '1');
formData.append('type', '1');

Expand All @@ -35,7 +41,7 @@ const Register = () => {

setPreviewImage(data.url);
} catch (error) {
// todo handle error
toast?.setToast('error', 'เกิดข้อผิดพลาดในการอัปโหลดรูปภาพ');
}
}

Expand Down Expand Up @@ -190,7 +196,7 @@ const Register = () => {
};

return (
<div className="relative flex lg:justify-end">
<div className="relative flex pt-8 lg:justify-center">
{bottleModal && (
<AnimatePresence>
<motion.div
Expand Down Expand Up @@ -249,7 +255,7 @@ const Register = () => {
)}

<form
className="mt-40 flex w-full flex-col items-center justify-start rounded-t-3xl bg-white text-purple lg:mt-0 lg:w-2/3 lg:rounded-tr-none"
className="mt-40 flex w-full flex-col items-center justify-start rounded-t-3xl bg-white text-purple lg:mt-0 lg:w-2/3"
onSubmit={onFormDone}
noValidate={true}
id="register-form"
Expand All @@ -271,7 +277,7 @@ const Register = () => {
id="image"
name="image"
type="file"
accept="images/*"
accept="image/*"
className="hidden"
onChange={async (
e: ChangeEvent<HTMLInputElement>
Expand Down Expand Up @@ -300,7 +306,7 @@ const Register = () => {
<select
name="nametitle"
id="nametitle"
className="mb-4 w-28 rounded-full border-r-8 border-transparent bg-gray-100 py-2 pl-3 pr-8 outline-4 outline-gray-100"
className="mb-4 w-36 rounded-full border-r-8 border-transparent bg-gray-100 py-2 pl-3 pr-8 outline-4 outline-gray-100"
required
>
<option value="">เลือกคำนำหน้าชื่อ</option>
Expand Down