import { NextPage } from 'next';
import MainLayout from '../../layouts/main';
import dynamic from 'next/dynamic';
import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';
import { BusinessApi } from '../../modules/company/company.api';
import { CompanyDTO, IOffer } from '../../modules/company/dto/company';
import Loader from '../../component/loader/Loader';
import { useTypedSelector } from '../../hooks/useTypedSelector';
import { UserDTO } from '../../modules/auth/dto/user';
import { AppData } from '../../services/app';
const OfferContent = dynamic(() => import('../../modules/offer/OfferPage'), {
ssr: false
});
const OfferPage: NextPage = () => {
const router = useRouter();
const businessId = router.query.businessId as string;
const offerId = router.query.id as string;
const [offer, setOffer] = useState<IOffer>();
const [company, setCompany] = useState<CompanyDTO | UserDTO>();
const { user } = useTypedSelector((state) => state.user);
useEffect(() => {
const getOffer = async () => {
if (user?.id === businessId) {
await BusinessApi.GetUserOffer(offerId).then((res) => {
setOffer(res);
});
setCompany(user);
} else {
await BusinessApi.GetOfferInfo(offerId).then((res) => {
setOffer(res);
setCompany(res.business);
});
}
};
if (businessId && offerId) {
getOffer();
}
}, [businessId, offerId, user]);
return (
<MainLayout
protectedRoute={false}
title={company?.name as string}
description={offer?.description}
image={AppData.avatarUrlPath(offer?.attachments[0].url)}
>
{!offer || !company ? (
<Loader />
) : (
<OfferContent offer={offer} company={company} />
)}
</MainLayout>
);
};
export default OfferPage;