Advertisement
Guest User

buyer-matching/index.js

a guest
Apr 6th, 2020
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useEffect, useCallback } from 'react';
  2. import { Form } from 'react-final-form';
  3. import { useQuery, useMutation } from 'react-apollo';
  4. import { get, isEmpty } from 'lodash';
  5. import Toast from 'components/toast';
  6. import ProgressButton from '@leadhome/oucl/ProgressButton';
  7. import FormSubscription from 'services/form-manager/form-subscription';
  8. import { TabListLoader } from 'components/list-loader';
  9. import { Container } from './style';
  10. import BuyerMatchingForm from './components/form';
  11. import BUYER_MATCHING_PATCH_MUTATION from './BUYER_MATCHING_PATCH_MUTATION.graphql';
  12. import BUYER_MATCHING_LISTING_RESULTS from './BUYER_MATCHING_LISTING_RESULTS.graphql';
  13. import BUYER_MATCHING_PROFILE_QUERY from './BUYER_MATCHING_PROFILE_QUERY.graphql';
  14. import { bathroomOptionsMap } from './utils';
  15.  
  16. function BuyerMatchingProfile({ id }) {
  17.   const [initialValues, setInitialValues] = useState(null);
  18.   const [showErrorModal, setShowErrorModal] = useState(false);
  19.   const [errorText, setErrorText] = useState(false);
  20.   const [searchQuery, setSearchQuery] = useState({});
  21.  
  22.   const clearError = useCallback(() => {
  23.     setShowErrorModal(false);
  24.   }, [setShowErrorModal]);
  25.  
  26.   const { data: buyerMatchingProfileData, loading, error } = useQuery(
  27.     BUYER_MATCHING_PROFILE_QUERY,
  28.     {
  29.       variables: {
  30.         id
  31.       }
  32.     }
  33.   );
  34.  
  35.   const {
  36.     data: buyerMatchingListingResults,
  37.     loading: searching,
  38.     error: searchError
  39.   } = useQuery(BUYER_MATCHING_LISTING_RESULTS, {
  40.     variables: {
  41.       id,
  42.       query: searchQuery
  43.     },
  44.     skip: !id || isEmpty(searchQuery)
  45.   });
  46.  
  47.   const buyerMatchingProfile = get(
  48.     buyerMatchingProfileData,
  49.     'person.byId.buyerProfile',
  50.     {}
  51.   );
  52.   const buyerMatchingListings = get(
  53.     buyerMatchingListingResults,
  54.     'listing.matchingBuyerProfile.results',
  55.     []
  56.   );
  57.  
  58.   useEffect(() => {
  59.     if (!initialValues && !isEmpty(buyerMatchingProfile))
  60.       setInitialValues(buyerMatchingProfile);
  61.   }, [setInitialValues, initialValues, buyerMatchingProfile]);
  62.  
  63.   useEffect(() => {
  64.     if (error || searchError) {
  65.       if (error)
  66.         setErrorText(
  67.           'Failed to load buyer matching profile. Please reload the page top try again'
  68.         );
  69.       if (searchError)
  70.         setErrorText('Failed to load search results. Please try again');
  71.       setShowErrorModal(true);
  72.       setTimeout(() => {
  73.         clearError();
  74.       }, 5000);
  75.     }
  76.   }, [searchError, error, clearError, setShowErrorModal, setErrorText]);
  77.  
  78.   const [saveBuyerMatchingProfile, { loading: saving }] = useMutation(
  79.     BUYER_MATCHING_PATCH_MUTATION,
  80.     {
  81.       onError: () => {
  82.         setErrorText('Failed to save buyer matching profile. Please try again');
  83.         setShowErrorModal(true);
  84.         setTimeout(() => {
  85.           clearError();
  86.         }, 5000);
  87.       },
  88.       onCompleted: data => {
  89.         const buyerMatchingProfile = get(
  90.           data,
  91.           'person.byId.buyerProfile.patch',
  92.           {}
  93.         );
  94.  
  95.         const {
  96.           personId,
  97.           sendListingAlerts,
  98.           listingTypes,
  99.           townIds,
  100.           suburbIds,
  101.           priceMin,
  102.           priceMax,
  103.           propertyTypes,
  104.           numberOfFloors,
  105.           unitFloor,
  106.           floorSize,
  107.           unitSize,
  108.           landSize,
  109.           minBaths,
  110.           minLivingAreas,
  111.           minBeds,
  112.           maxBeds,
  113.           minUncoveredParking,
  114.           minCoveredParking,
  115.           minStudies,
  116.           mustBeFixerUpper,
  117.           petsAllowed,
  118.           internetTypes,
  119.           includePrivateAmenityIds,
  120.           excludePrivateAmenityIds,
  121.           includeCommunalAmenityIds,
  122.           excludeCommunalAmenityIds,
  123.           minParking
  124.         } = buyerMatchingProfile;
  125.         setSearchQuery({
  126.           personId,
  127.           sendListingAlerts,
  128.           listingTypes,
  129.           townIds,
  130.           suburbIds,
  131.           priceMin,
  132.           priceMax,
  133.           propertyTypes,
  134.           numberOfFloors,
  135.           unitFloor,
  136.           floorSize,
  137.           unitSize,
  138.           landSize,
  139.           minBaths,
  140.           minLivingAreas,
  141.           minBeds,
  142.           maxBeds,
  143.           minUncoveredParking,
  144.           minCoveredParking,
  145.           minStudies,
  146.           mustBeFixerUpper,
  147.           petsAllowed,
  148.           internetTypes,
  149.           includePrivateAmenityIds,
  150.           excludePrivateAmenityIds,
  151.           includeCommunalAmenityIds,
  152.           excludeCommunalAmenityIds,
  153.           minParking
  154.         });
  155.       }
  156.     }
  157.   );
  158.  
  159.   const onSave = useCallback(
  160.     ({
  161.       personId,
  162.       sendListingAlerts,
  163.       listingTypes,
  164.       townIds,
  165.       suburbIds,
  166.       priceMin,
  167.       priceMax,
  168.       propertyTypes,
  169.       numberOfFloors,
  170.       unitFloor,
  171.       floorSize,
  172.       unitSize,
  173.       landSize,
  174.       minBaths,
  175.       minLivingAreas,
  176.       minBeds,
  177.       maxBeds,
  178.       minUncoveredParking,
  179.       minCoveredParking,
  180.       minStudies,
  181.       mustBeFixerUpper,
  182.       petsAllowed,
  183.       internetTypes,
  184.       includePrivateAmenityIds,
  185.       excludePrivateAmenityIds,
  186.       includeCommunalAmenityIds,
  187.       excludeCommunalAmenityIds,
  188.       minParking
  189.     }) => {
  190.       saveBuyerMatchingProfile({
  191.         variables: {
  192.           id,
  193.           buyerProfile: {
  194.             personId,
  195.             sendListingAlerts,
  196.             listingTypes,
  197.             townIds,
  198.             suburbIds,
  199.             priceMin,
  200.             priceMax,
  201.             propertyTypes,
  202.             numberOfFloors,
  203.             unitFloor,
  204.             floorSize,
  205.             unitSize,
  206.             landSize,
  207.             minBaths: bathroomOptionsMap[minBaths],
  208.             minLivingAreas,
  209.             minBeds,
  210.             maxBeds,
  211.             minUncoveredParking,
  212.             minCoveredParking,
  213.             minStudies,
  214.             mustBeFixerUpper,
  215.             petsAllowed,
  216.             internetTypes,
  217.             includePrivateAmenityIds,
  218.             excludePrivateAmenityIds,
  219.             includeCommunalAmenityIds,
  220.             excludeCommunalAmenityIds,
  221.             minParking
  222.           }
  223.         }
  224.       });
  225.     },
  226.     [saveBuyerMatchingProfile, id]
  227.   );
  228.  
  229.   return (
  230.     <Container>
  231.       {loading ? (
  232.         <TabListLoader />
  233.       ) : (
  234.         <Form onSubmit={onSave} initialValues={initialValues}>
  235.           {({ handleSubmit, form, formId }) => (
  236.             <>
  237.               <form onSubmit={handleSubmit}>
  238.                 <FormSubscription form={form} id={formId} />
  239.                 <BuyerMatchingForm />
  240.                 <ProgressButton loading={saving || searching}>
  241.                   Save and search
  242.                 </ProgressButton>
  243.               </form>
  244.               {searching
  245.                 ? 'Loading search results...'
  246.                 : `Number of results: ${buyerMatchingListings.length}`}
  247.             </>
  248.           )}
  249.         </Form>
  250.       )}
  251.       <Toast
  252.         isOpen={showErrorModal}
  253.         message={errorText}
  254.         type='fail'
  255.         onClose={clearError}
  256.       />
  257.     </Container>
  258.   );
  259. }
  260.  
  261. export default BuyerMatchingProfile;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement