Skip to content

Commit

Permalink
refactor(AuthContext): Refactor to TypeScript and change dir
Browse files Browse the repository at this point in the history
  • Loading branch information
EnzoVieira committed Dec 7, 2022
1 parent d446f20 commit 0964fbe
Show file tree
Hide file tree
Showing 29 changed files with 85 additions and 60 deletions.
4 changes: 0 additions & 4 deletions components/Auth/index.js

This file was deleted.

4 changes: 0 additions & 4 deletions components/Auth/useAuth.js

This file was deleted.

2 changes: 1 addition & 1 deletion components/moonstone/staff/utils/Base/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Link from "next/link";
import { Dialog, Transition } from "@headlessui/react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faBars, faTimes } from "@fortawesome/free-solid-svg-icons";
import { useAuth } from "/components/Auth";
import { useAuth } from "@context/Auth";
import Return from "/components/moonstone/utils/Return";

function classNames(...classes) {
Expand Down
2 changes: 1 addition & 1 deletion components/moonstone/staff/utils/Dashboard/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Link from "next/link";
import { Dialog, Transition } from "@headlessui/react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faBars, faTimes } from "@fortawesome/free-solid-svg-icons";
import { useAuth } from "/components/Auth";
import { useAuth } from "@context/Auth";
import Return from "/components/moonstone/utils/Return";

const navigation = ["redeem prizes", "scan qr"];
Expand Down
2 changes: 1 addition & 1 deletion components/moonstone/user/utils/Dashboard/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Dialog, Transition } from "@headlessui/react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faBars, faTimes } from "@fortawesome/free-solid-svg-icons";
import { classNames } from "/lib/css";
import { useAuth } from "/components/Auth";
import { useAuth } from "@context/Auth";
import Return from "/components/moonstone/utils/Return";

const navigation = [
Expand Down
2 changes: 1 addition & 1 deletion components/website/home/Hero/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dynamic from "next/dynamic";

import { useAuth } from "/components/Auth";
import { useAuth } from "@context/Auth";

import Social from "/components/website/utils/Social";

Expand Down
2 changes: 1 addition & 1 deletion components/website/utils/Navbar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Disclosure, Menu, Transition } from "@headlessui/react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faBars, faTimes } from "@fortawesome/free-solid-svg-icons";

import { useAuth } from "/components/Auth";
import { useAuth } from "@context/Auth";
import JoinUs from "/components/website/utils/JoinUs";
import styles from "./style.module.css";

Expand Down
79 changes: 56 additions & 23 deletions components/Auth/AuthProvider.js → context/Auth/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
import { createContext, useEffect, useMemo, useState } from "react";
import { createContext, useContext, useEffect, useState } from "react";
import { useRouter } from "next/router";
import API from "/lib/api";
import * as api from "/lib/api";
import * as USER from "/lib/user";
import API from "../../lib/api";
import * as api from "../../lib/api";
import * as USER from "../../lib/user";

export const AuthContext = createContext();
interface ILoginDTO {
email: string;
password: string;
}

// FIXME: Change this types from any
type IAttendee = any;
type ISopnsor = any;
type IManager = any;

type IUser = IAttendee | ISopnsor | IManager;

interface IAuthContext {
user: IUser;
errors?: string;

// Booleans
isAuthenticated: boolean;
isLoading: boolean;

// Updates user in state
updateUser: (user: IUser) => void;
refetchUser: () => void;

// Api calls
login: (params: ILoginDTO) => void;
logout: () => void;
editUser: (username: string) => void;
sign_up: (fields: any) => void;
}

export const AuthContext = createContext({} as IAuthContext);

const TOKEN_KEY_NAME = "safira_token";

// Function that consumes the context
export const useAuth = () => useContext(AuthContext);

export function AuthProvider({ children }) {
const router = useRouter();
const [user, setUser] = useState(null);
Expand Down Expand Up @@ -144,26 +178,25 @@ export function AuthProvider({ children }) {
setRefetch((needsRefetch) => !needsRefetch);
}

// Make the provider update only when it should
const values = useMemo(
() => ({
user,
isAuthenticated,
isLoading,
setUser,
errors,
login,
logout,
editUser,
refetchUser,
sign_up,
}),
// eslint-disable-next-line
[user, isAuthenticated, isLoading, errors]
);
function updateUser(updatedUser: IUser) {
setUser(updatedUser);
}

return (
<AuthContext.Provider value={values}>
<AuthContext.Provider
value={{
user,
isAuthenticated,
isLoading,
updateUser: updateUser,
errors,
login,
logout,
editUser,
refetchUser,
sign_up,
}}
>
{!isFirstLoading && children}
</AuthContext.Provider>
);
Expand Down
4 changes: 4 additions & 0 deletions context/Auth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { AuthProvider } from "./AuthContext";
export { useAuth } from "./AuthContext";
export { withAuth } from "./withAuth";
export { withoutAuth } from "./withoutAuth";
2 changes: 1 addition & 1 deletion components/Auth/withAuth.js → context/Auth/withAuth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useRouter } from "next/router";
import { useAuth } from "./useAuth";
import { useAuth } from "./AuthContext";
import * as USER from "/lib/user";

export function withAuth(WrappedComponent) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { faUserAstronaut } from "@fortawesome/free-solid-svg-icons";
import { Router, useRouter } from "next/router";
import { useAuth } from "./useAuth";
import { useAuth } from "./AuthContext";

export function withoutAuth(WrappedComponent) {
// eslint-disable-next-line react/display-name
Expand Down
2 changes: 1 addition & 1 deletion pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AuthProvider } from "@components/Auth";
import { AuthProvider } from "@context/Auth";
import Header from "@components/utils/Header";

import "../styles/globals.css";
Expand Down
3 changes: 1 addition & 2 deletions pages/attendee/badgedex.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useState, useEffect } from "react";
import { withAuth } from "/components/Auth";
import { useAuth } from "/components/Auth";
import { useAuth, withAuth } from "@context/Auth";

import { getAllBadges } from "/lib/api";

Expand Down
3 changes: 1 addition & 2 deletions pages/attendee/leaderboard.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState, useEffect } from "react";

import { withAuth } from "/components/Auth";
import { useAuth } from "/components/Auth/useAuth";
import { useAuth, withAuth } from "@context/Auth";

import Dashboard from "/components/moonstone/user/utils/Dashboard";
import Table from "/components/moonstone/user/leaderboard/Table";
Expand Down
2 changes: 1 addition & 1 deletion pages/attendee/profile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from "react";

import { withAuth, useAuth } from "/components/Auth";
import { withAuth, useAuth } from "@context/Auth";

import Form from "/components/moonstone/utils/Form";
import Input from "/components/moonstone/utils/Input";
Expand Down
2 changes: 1 addition & 1 deletion pages/attendee/store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect } from "react";

import { withAuth, useAuth } from "/components/Auth";
import { withAuth, useAuth } from "@context/Auth";

import * as api from "/lib/api";

Expand Down
2 changes: 1 addition & 1 deletion pages/attendee/vault.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect } from "react";

import { withAuth, useAuth } from "/components/Auth";
import { withAuth, useAuth } from "@context/Auth";

import * as api from "/lib/api";

Expand Down
2 changes: 1 addition & 1 deletion pages/attendee/wheel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect } from "react";
import { withAuth, useAuth } from "/components/Auth";
import { withAuth, useAuth } from "@context/Auth";

import Heading from "/components/moonstone/utils/Heading";

Expand Down
2 changes: 1 addition & 1 deletion pages/attendees/[uuid].js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect } from "react";
import { useRouter } from "next/router";
import { withAuth, useAuth } from "/components/Auth";
import { withAuth } from "@context/Auth";

import Dashboard from "/components/moonstone/user/utils/Dashboard";
import Heading from "/components/moonstone/utils/Heading";
Expand Down
2 changes: 1 addition & 1 deletion pages/badge/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Link from "next/link";

import { useState, useEffect } from "react";
import { useRouter } from "next/router";
import { withAuth, useAuth } from "/components/Auth";
import { withAuth, useAuth } from "@context/Auth";

import { getBadge } from "/lib/api";

Expand Down
2 changes: 1 addition & 1 deletion pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { withoutAuth } from "@components/Auth";
import { withoutAuth } from "@context/Auth";
import Hero from "@components/website/home/Hero";
import Sponsors from "@components/website/home/Sponsors";
import Hackathon from "@components/website/home/Hackathon";
Expand Down
2 changes: 1 addition & 1 deletion pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useRef } from "react";
import { motion as Motion } from "framer-motion";

import { useAuth, withoutAuth } from "@components/Auth";
import { useAuth, withoutAuth } from "@context/Auth";

import Button from "@components/utils/Button";
import Card from "@components/utils/Card";
Expand Down
5 changes: 1 addition & 4 deletions pages/manager/prizes/[uuid].js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { useState, useEffect } from "react";

import { withAuth, useAuth } from "/components/Auth";
import { withAuth, useAuth } from "@context/Auth";
import { useRouter } from "next/router";

import * as api from "/lib/api";

import Base from "/components/moonstone/staff/utils/Base";
import Prizes from "/components/moonstone/user/vault/wheel/Prizes";
import Button from "/components/moonstone/utils/Button";

import Balance from "/components/moonstone/user/store/Balance";

const navigation = ["badges", "prizes", "identifier"];

function ManagerRedeemables() {
Expand Down
2 changes: 1 addition & 1 deletion pages/manager/prizes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { giveBadge } from "/lib/api";

import Base from "/components/moonstone/staff/utils/Base";
import QRScanner, { FEEDBACK } from "/components/moonstone/utils/QRScanner";
import { useAuth } from "/components/Auth/useAuth";
import { useAuth } from "@context/Auth";
import { useRouter } from "next/router";

const navigation = ["badges", "prizes", "identifier"];
Expand Down
2 changes: 1 addition & 1 deletion pages/product/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState, useEffect } from "react";
import { useRouter } from "next/router";
import Link from "next/link";

import { withAuth, useAuth } from "/components/Auth";
import { withAuth, useAuth } from "@context/Auth";

import { getProduct, buyProduct } from "/lib/api";

Expand Down
2 changes: 1 addition & 1 deletion pages/register/[uuid].js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";
import { useRouter } from "next/router";

import { withoutAuth, useAuth } from "/components/Auth";
import { withoutAuth, useAuth } from "@context/Auth";
import { motion as Motion } from "framer-motion";

import Button from "/components/utils/Button";
Expand Down
2 changes: 1 addition & 1 deletion pages/signup.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useRef } from "react";

import { withoutAuth, useAuth } from "@components/Auth";
import { withoutAuth, useAuth } from "@context/Auth";
import { motion as Motion } from "framer-motion";

import Button from "@components/utils/Button";
Expand Down
2 changes: 1 addition & 1 deletion pages/sponsor/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { giveBadge } from "/lib/api";

import Base from "/components/moonstone/staff/utils/Base";
import QRScanner, { FEEDBACK } from "/components/moonstone/utils/QRScanner";
import { useAuth } from "../../components/Auth/useAuth";
import { useAuth } from "@context/Auth";

const navigation = ["badges"];

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"jsx": "preserve",
"baseUrl": ".",
"paths": {
"@context/*": ["context/*"],
"@components/*": ["components/*"],
"@data/*": ["data/*"],
"@lib/*": ["lib/*"]
Expand Down

0 comments on commit 0964fbe

Please sign in to comment.