From d19b40f2972cc74105cfb8390a3b0fe3a4695b3a Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 17 Nov 2022 17:30:03 +0700 Subject: Login process to Odoo Api --- src/pages/_app.js | 10 ++++++++ src/pages/api/login.js | 17 ++++++++++++++ src/pages/index.js | 10 ++++---- src/pages/login.js | 51 +++++++++++++++++++++++++++++++++------- src/pages/register.js | 6 ++--- src/pages/shop/product/[slug].js | 6 ++--- 6 files changed, 81 insertions(+), 19 deletions(-) create mode 100644 src/pages/api/login.js (limited to 'src/pages') diff --git a/src/pages/_app.js b/src/pages/_app.js index 7950628e..c0899ea9 100644 --- a/src/pages/_app.js +++ b/src/pages/_app.js @@ -1,9 +1,19 @@ import '../styles/globals.css'; import NextProgress from "next-progress"; +import { ToastContainer, Slide } from 'react-toastify'; +import 'react-toastify/dist/ReactToastify.css'; function MyApp({ Component, pageProps }) { return ( <> + diff --git a/src/pages/api/login.js b/src/pages/api/login.js new file mode 100644 index 00000000..1236aea3 --- /dev/null +++ b/src/pages/api/login.js @@ -0,0 +1,17 @@ +import axios from "axios"; +import apiOdoo from "../../helpers/apiOdoo"; + +export default async function handler(req, res) { + try { + const { email, password } = req.body; + let result = await apiOdoo( + 'POST', + '/api/v1/login', + {email, password} + ); + res.status(200).json(result); + } catch (error) { + console.log(error); + res.status(400).json({ error: error.message }); + } +} \ No newline at end of file diff --git a/src/pages/index.js b/src/pages/index.js index 9d26d8ee..7fa538d1 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -3,7 +3,7 @@ import { LazyLoadImage } from "react-lazy-load-image-component"; import { Swiper, SwiperSlide } from "swiper/react"; import { Pagination, Autoplay } from "swiper"; import Header from "../components/Header"; -import { getOdoo } from "../helpers/apiOdoo"; +import apiOdoo from "../helpers/apiOdoo"; import "react-lazy-load-image-component/src/effects/blur.css"; import "swiper/css"; import "swiper/css/pagination"; @@ -18,25 +18,25 @@ export default function Home() { useEffect(() => { const getHeroBanners = async () => { - const dataHeroBanners = await getOdoo(`/api/v1/banner?type=index-a-1`); + const dataHeroBanners = await apiOdoo('GET', `/api/v1/banner?type=index-a-1`); setHeroBanners(dataHeroBanners); } getHeroBanners(); const getManufactures = async () => { - const dataManufactures = await getOdoo(`/api/v1/manufacture?level=prioritas`); + const dataManufactures = await apiOdoo('GET', `/api/v1/manufacture?level=prioritas`); setManufactures(dataManufactures); } getManufactures(); const getReadyStockProducts = async () => { - const dataReadyStockProducts = await getOdoo(`/api/v1/product?ready_stock=1&limit=30`); + const dataReadyStockProducts = await apiOdoo('GET', `/api/v1/product?ready_stock=1&limit=30`); setReadyStockProducts(dataReadyStockProducts); } getReadyStockProducts(); const getPopularProducts = async () => { - const dataPopularProducts = await getOdoo(`/api/v1/product?manufactures=10&limit=30`); + const dataPopularProducts = await apiOdoo('GET', `/api/v1/product?manufactures=10&limit=30`); setPopularProducts(dataPopularProducts); } getPopularProducts(); diff --git a/src/pages/login.js b/src/pages/login.js index 971dc38e..315146a5 100644 --- a/src/pages/login.js +++ b/src/pages/login.js @@ -2,39 +2,74 @@ import axios from "axios"; import Head from "next/head"; import Image from "next/image"; import Link from "next/link"; +import { useRouter } from "next/router"; +import { useEffect, useState } from "react"; +import { toast } from "react-toastify"; +import Spinner from "../components/Spinner"; +import { setAuth } from "../helpers/auth"; import Logo from "../images/logo.png"; export default function Login() { - const login = (e) => { + const router = useRouter(); + const [email, setEmail] = useState(''); + const [password, setPassword] = useState(''); + const [isInputFulfilled, setIsInputFulfilled] = useState(false); + const [isLoading, setIsLoading] = useState(false); + + useEffect(() => { + setIsInputFulfilled(email && password); + }, [email, password]); + + const login = async (e) => { e.preventDefault(); - // axios.post(`${process.env.SELF_HOST}/api/login`, {email: 'it@fixcomart.co', password: 'Fixcomart378'}); + setIsLoading(true); + let login = await axios.post(`${process.env.SELF_HOST}/api/login`, {email, password}); + if (login.data.is_auth) { + setAuth(login.data.user); + router.push('/'); + } else { + toast.info('Email atau Password tidak cocok'); + toast.clearWaitingQueue(); + setIsLoading(false); + } } + return ( <> Masuk - Indoteknik -
+
Logo Indoteknik -

Mulai Belanja Sekarang

+

Mulai Belanja Sekarang

Masuk ke akun kamu untuk belanja

-
+ setEmail(e.target.value)} /> setPassword(e.target.value)} />
- Lupa kata sandi + Lupa kata sandi
- +

Belum punya akun Indoteknik? Daftar

diff --git a/src/pages/register.js b/src/pages/register.js index 86208dc0..38ea972f 100644 --- a/src/pages/register.js +++ b/src/pages/register.js @@ -9,11 +9,11 @@ export default function Login() { Daftar - Indoteknik -
+
Logo Indoteknik -

Mudahkan Pembelian dengan Indoteknik

+

Mudahkan Pembelian dengan Indoteknik

Daftar untuk melanjutkan

Sudah punya akun Indoteknik? Masuk

diff --git a/src/pages/shop/product/[slug].js b/src/pages/shop/product/[slug].js index f96aba34..bcbebbdf 100644 --- a/src/pages/shop/product/[slug].js +++ b/src/pages/shop/product/[slug].js @@ -2,7 +2,7 @@ import Link from "next/link"; import { useRouter } from "next/router"; import { useEffect, useState } from "react"; import Header from "../../../components/Header"; -import { getOdoo } from "../../../helpers/apiOdoo"; +import apiOdoo from "../../../helpers/apiOdoo"; import { createSlug, getId } from "../../../helpers/slug"; import currencyFormat from "../../../helpers/currencyFormat"; import { LazyLoadImage } from "react-lazy-load-image-component"; @@ -11,7 +11,7 @@ import ProductSlider from "../../../components/product/ProductSlider"; export async function getServerSideProps(context) { const { slug } = context.query; - let product = await getOdoo('/api/v1/product/' + getId(slug)); + let product = await apiOdoo('GET', '/api/v1/product/' + getId(slug)); if (product.length == 1) { product = product[0]; } @@ -42,7 +42,7 @@ export default function ProductDetail({product}) { useEffect(() => { setSimilarProducts(null); const getSimilarProducts = async () => { - const dataSimilarProducts = await getOdoo(`/api/v1/product/${getId(slug)}/similar?limit=20`); + const dataSimilarProducts = await apiOdoo('GET', `/api/v1/product/${getId(slug)}/similar?limit=20`); setSimilarProducts(dataSimilarProducts); } getSimilarProducts(); -- cgit v1.2.3