From 0de0fda98dc35bd6503f1a45a52878b154a94c75 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 23 Feb 2023 10:52:40 +0700 Subject: fox --- src/lib/auth/components/Activate.jsx | 166 +++++++++++++++++++++++++++++ src/lib/auth/components/CompanyProfile.jsx | 120 +++++++++++++++------ src/lib/auth/components/Register.jsx | 36 ++++++- 3 files changed, 287 insertions(+), 35 deletions(-) create mode 100644 src/lib/auth/components/Activate.jsx (limited to 'src/lib/auth/components') diff --git a/src/lib/auth/components/Activate.jsx b/src/lib/auth/components/Activate.jsx new file mode 100644 index 00000000..7970524c --- /dev/null +++ b/src/lib/auth/components/Activate.jsx @@ -0,0 +1,166 @@ +import Image from 'next/image' +import Link from 'next/link' +import { useRouter } from 'next/router' +import { useEffect, useState } from 'react' +import IndoteknikLogo from '@/images/logo.png' +import axios from 'axios' +import { setAuth } from '@/core/utils/auth' +import Alert from '@/core/components/elements/Alert/Alert' +import odooApi from '@/core/api/odooApi' + +const Activate = () => { + const router = useRouter() + const { token } = router.query + + const [isLoading, setIsLoading] = useState(false) + const [alert, setAlert] = useState() + + const [email, setEmail] = useState(router.query?.email || '') + + useEffect(() => { + const activateIfTokenExist = async () => { + if (token) { + let isActivated = await odooApi('POST', '/api/v1/user/activation', { token }) + if (isActivated.activation) { + setAuth(isActivated.user) + setAlert({ + children: ( + <> + Selamat, akun anda berhasil diaktifkan,{' '} + + kembali ke beranda + + . + + ), + type: 'success' + }) + } else { + setAlert({ + children: ( + <> + Mohon maaf token sudah tidak aktif, lakukan permintaan aktivasi akun kembali atau{' '} + + masuk + {' '} + jika sudah memiliki akun. + + ), + type: 'info' + }) + } + } + } + activateIfTokenExist() + }, [token]) + + useEffect(() => { + if (router.query.email) setEmail(router.query.email) + }, [router]) + + const activationRequest = async (e) => { + e.preventDefault() + setIsLoading(true) + let activationRequest = await axios.post(`${process.env.SELF_HOST}/api/activation-request`, { + email + }) + if (activationRequest.data.activationRequest) { + setAlert({ + children: <>Mohon cek email anda untuk aktivasi akun Indoteknik, + type: 'success' + }) + } else { + switch (activationRequest.data.reason) { + case 'NOT_FOUND': + setAlert({ + children: ( + <> + Email tersebut belum terdaftar,{' '} + + daftar sekarang + + . + + ), + type: 'info' + }) + break + case 'ACTIVE': + setAlert({ + children: ( + <> + Email tersebut sudah terdaftar dan sudah aktif,{' '} + + masuk sekarang + + . + + ), + type: 'info' + }) + break + } + } + setIsLoading(false) + } + + return ( +
+ + Logo Indoteknik + + +

Aktivasi Akun Indoteknik

+ + {alert && ( + + {alert.children} + + )} + +
+ setEmail(e.target.value)} + placeholder='Masukan alamat email' + autoFocus + /> + +
+
+ ) +} + +export default Activate diff --git a/src/lib/auth/components/CompanyProfile.jsx b/src/lib/auth/components/CompanyProfile.jsx index d66a0209..95575c87 100644 --- a/src/lib/auth/components/CompanyProfile.jsx +++ b/src/lib/auth/components/CompanyProfile.jsx @@ -1,32 +1,66 @@ +import odooApi from '@/core/api/odooApi' +import HookFormSelect from '@/core/components/elements/Select/HookFormSelect' import useAuth from '@/core/hooks/useAuth' import addressApi from '@/lib/address/api/addressApi' import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline' import { useEffect, useState } from 'react' -import { useForm } from 'react-hook-form' +import { Controller, useForm } from 'react-hook-form' -const PersonalProfile = () => { +const CompanyProfile = () => { const auth = useAuth() const [isOpen, setIsOpen] = useState(false) const toggle = () => setIsOpen(!isOpen) - const { register, setValue } = useForm({ + const { register, setValue, control, handleSubmit } = useForm({ defaultValues: { - email: '', + industry: '', + companyType: '', name: '', - mobile: '', - password: '' + taxName: '', + npwp: '' } }) + const [industries, setIndustries] = useState([]) + useEffect(() => { + const loadIndustries = async () => { + const dataIndustries = await odooApi('GET', '/api/v1/partner/industry') + setIndustries(dataIndustries?.map((o) => ({ value: o.id, label: o.name }))) + } + loadIndustries() + }, []) + + const [companyTypes, setCompanyTypes] = useState([]) + useEffect(() => { + const loadCompanyTypes = async () => { + const dataCompanyTypes = await odooApi('GET', '/api/v1/partner/company_type') + setCompanyTypes(dataCompanyTypes?.map((o) => ({ value: o.id, label: o.name }))) + } + loadCompanyTypes() + }, []) + useEffect(() => { const loadProfile = async () => { - const dataProfile = await addressApi({ id: auth.partnerId }) - setValue('email', dataProfile?.email) - setValue('name', dataProfile?.name) - setValue('mobile', dataProfile?.mobile) + const dataProfile = await addressApi({ id: auth.parentId }) + setValue('name', dataProfile.name) + setValue('industry', dataProfile.industryId) + setValue('companyType', dataProfile.companyTypeId) + setValue('taxName', dataProfile.taxName) + setValue('npwp', dataProfile.npwp) } if (auth) loadProfile() }, [auth, setValue]) + const onSubmitHandler = async (values) => { + const data = { + ...values, + company_type_id: values.companyType, + industry_id: values.industry, + tax_name: values.taxName + }; + const isUpdated = await odooApi('PUT', `/api/v1/partner/${auth.parentId}`, data) + console.log(isUpdated); + } + return ( <> {isOpen && ( -
+
- - Klasifikasi Jenis Usaha + ( + + )} />
-
- - +
+
Nama Usaha
+
+ ( + + )} + /> +
+
+ +
- +
- +
@@ -94,4 +146,4 @@ const PersonalProfile = () => { ) } -export default PersonalProfile +export default CompanyProfile diff --git a/src/lib/auth/components/Register.jsx b/src/lib/auth/components/Register.jsx index 135972d3..d02081ce 100644 --- a/src/lib/auth/components/Register.jsx +++ b/src/lib/auth/components/Register.jsx @@ -3,6 +3,8 @@ import Link from '@/core/components/elements/Link/Link' import IndoteknikLogo from '@/images/logo.png' import { useState } from 'react' import registerApi from '../api/registerApi' +import Alert from '@/core/components/elements/Alert/Alert' +import axios from 'axios' const Register = () => { const [fullname, setFullname] = useState('') @@ -10,9 +12,12 @@ const Register = () => { const [password, setPassword] = useState('') const [companyName, setCompanyName] = useState('') const [isLoading, setIsLoading] = useState('') + const [alert, setAlert] = useState(null) const handleSubmit = async (e) => { e.preventDefault() + setAlert(null) + setIsLoading(true) const data = { name: fullname, company: companyName, @@ -20,7 +25,27 @@ const Register = () => { password } const isRegistered = await registerApi({ data }) - console.log(isRegistered) + setIsLoading(false) + if (isRegistered.register) { + await axios.post(`${process.env.SELF_HOST}/api/activation-request`, { email }) + setAlert({ + children: 'Berhasil mendaftarkan akun anda, cek email untuk melakukan aktivasi akun', + type: 'success' + }) + setCompanyName('') + setFullname('') + setEmail('') + setPassword('') + } else { + switch (isRegistered.reason) { + case 'EMAIL_USED': + setAlert({ + children: 'Email telah digunakan', + type: 'info' + }) + break + } + } } return ( @@ -39,6 +64,15 @@ const Register = () => { Buat akun sekarang lebih mudah dan terverifikasi + {alert && ( + + {alert.children} + + )} +