Skip to content

Commit

Permalink
Squash - Floor plans Entrata Updates (#33)
Browse files Browse the repository at this point in the history
* floor-plans: add filter for metaboxes

* floor-plans: configure CPT for entrata

* floor-plans: update floor plans cpt entrata metaboxes

* floor-plans: combine entrata data with wp floor plans

* floor-plans: show new floor plan data in the app

* floor-plans: add hook for entrata unit type names
  • Loading branch information
mattwills8 committed Mar 14, 2019
1 parent 15a8d63 commit f4a6205
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 78 deletions.
10 changes: 10 additions & 0 deletions plugins/torque-floor-plans/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@ A Floor Plans React app can then be included via a shortcode in the Wordpress si

*Filter* | *Function* | *Value Type*
--- | --- | ---
`torque_floor_plans_cpt_metaboxes` | Filter Floor Plans CPT metaboxes | array
`torque_floor_plans_data_source` | Change the floor plans data source | 'entrata' or false (default - uses WP CPT)
`torque_floor_plans_entrata_property_id` | (Entrata Specific) set the entrata property id | int
`floor_plans_entrata_unit_type_name` | (Entrata Specific) change unit type names | string

<!-- prettier-ignore-end -->

# Changelog

## [2.1.0]

### Added

- Metaboxes filter hook
- Unit type names filter hook for entrata
- Entrata data source uses WP CPT for setting the floor plan images

## [2.0.0]

### Added
Expand Down
2 changes: 1 addition & 1 deletion plugins/torque-floor-plans/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "torque-floor-plans",
"version": "2.0.0",
"version": "2.1.0",
"author": "Torque",
"license": "ISC",
"scripts": {
Expand Down
31 changes: 17 additions & 14 deletions plugins/torque-floor-plans/src/app/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import style from "./App.scss";
import React, { memo, useState, useEffect } from "react";
import React, { memo, useState, useEffect, useCallback } from "react";
import PropTypes from "prop-types";
import DataSource from "./data-sources";
import FloorPlanSelector from "./FloorPlanSelector";
Expand All @@ -11,25 +11,28 @@ const App = ({ site, dataSource }) => {
const [floorPlans, setFloorPlans] = useState([]);
const [selected, setSelected] = useState(0);

const getFloorPlans = async () => {
try {
const source = new DataSource({ site, dataSource });
const floorPlans = await source.getFloorPlans();
const getFloorPlans = useCallback(
async () => {
try {
const source = new DataSource({ site, dataSource });
const floorPlans = await source.getFloorPlans();

setFloorPlans(floorPlans);
setSelected(0);
} catch (e) {
console.log(e);
setFloorPlans([]);
setSelected(0);
}
};
setFloorPlans(floorPlans);
setSelected(0);
} catch (e) {
console.log(e);
setFloorPlans([]);
setSelected(0);
}
},
[site, dataSource]
);

useEffect(
() => {
getFloorPlans();
},
[site, dataSource]
[getFloorPlans]
);

const selectedFloorPlan = floorPlans[selected];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import classnames from "classnames";
import printer from "./icons/eleven33-printer.png";
import search from "./icons/eleven33-search.png";
import share from "./icons/eleven33-share.png";
import keyplan from "./icons/Eleven33-Keyplans.png";

const FloorPlanInfo = ({ floorPlan: { post_title, rsf, thumbnail } }) => {
const FloorPlanInfo = ({
floorPlan: { post_title, rsf, thumbnail, key_plan_src: keyPlanSrc }
}) => {
const [isModalOpen, setModalOpen] = useState(false);
const toggleModalOpen = () => setModalOpen(!isModalOpen);

Expand Down Expand Up @@ -69,7 +70,7 @@ const FloorPlanInfo = ({ floorPlan: { post_title, rsf, thumbnail } }) => {
</div>

<div className="floor-plan-info-block floor-plan-key">
<img src={keyplan} />
<img src={keyPlanSrc} />
</div>

{isModalOpen && (
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@import "../../../../../node_modules/react-datepicker/dist/react-datepicker-cssmodules";
@import "../../../../../../../node_modules/react-datepicker/dist/react-datepicker-cssmodules";
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo, useState, useEffect } from "react";
import React, { memo, useState, useEffect, useCallback } from "react";
import classnames from "classnames";
import Entrata from "..";
import DatePicker from "react-datepicker";
Expand All @@ -9,19 +9,22 @@ const SearchBar = ({ setFloorPlans, site }) => {
const [selectedUnitTypes, setSelectedUnitTypes] = useState([]);
const [startDate, setStartDate] = useState();

const getUnitTypes = async () => {
const entrata = new Entrata({ site });
const unitTypes = await entrata.getUnitTypes();
const getUnitTypes = useCallback(
async () => {
const entrata = new Entrata({ site });
const unitTypes = await entrata.getUnitTypes();

setUnitTypes(unitTypes);
setSelectedUnitTypes([]);
};
setUnitTypes(unitTypes);
setSelectedUnitTypes([]);
},
[site]
);

useEffect(
() => {
getUnitTypes();
},
[site]
[getUnitTypes]
);

const handleUnitTypeClick = name => () => {
Expand Down Expand Up @@ -57,19 +60,11 @@ const SearchBar = ({ setFloorPlans, site }) => {
});

const entrata = new Entrata({ site });
const floorPlansResults = await entrata.getFloorPlans({
const floorPlans = await entrata.getFloorPlans({
unitTypeIds: unitTypeIds.length ? unitTypeIds : undefined,
startDate: formattedStartDate
});

const floorPlans = floorPlansResults.map(floorPlan => ({
downloads: { pdf: "" },
floor_number: 0,
post_title: floorPlan?.UnitTypes?.UnitType[0]["@value"],
rsf: floorPlan?.SquareFeet["@attributes"]?.Max.toString(),
thumbnail: floorPlan?.File[0]?.Src
}));

setFloorPlans(floorPlans);
};

Expand All @@ -88,7 +83,7 @@ const SearchBar = ({ setFloorPlans, site }) => {
})}
onClick={handleUnitTypeClick(unitType)}
>
{unitType}
{unitType.toUpperCase()}
</div>
))}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class Torque_Floor_Plan_CPT {

public static $METABOXES_FILTER_HOOK = 'torque_floor_plans_cpt_metaboxes';

/**
* Holds the floor plan cpt object
*
Expand Down Expand Up @@ -29,7 +31,6 @@ class Torque_Floor_Plan_CPT {
protected $floor_plan_options = array(
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail',
),
Expand All @@ -42,53 +43,65 @@ class Torque_Floor_Plan_CPT {
function __construct() {
$this->floor_plan = new PremiseCPT( self::$floor_plan_labels, $this->floor_plan_options );

pwp_add_metabox(
array(
'title' => 'Floor Number',
'context' => 'side',
'priority' => 'high',
),
array( self::$floor_plan_labels['post_type_name'] ),
array(
add_action('init', array($this, 'add_metaboxes'));
}

public function add_metaboxes() {
$maybe_metaboxes = array(
'floor_number' => array(
array(
'title' => 'Floor Number',
'context' => 'side',
'priority' => 'high',
),
array( self::$floor_plan_labels['post_type_name'] ),
array(
'type' => 'number',
'step' => 1,
'context' => 'post',
'name' => 'floor_plan_floor_number',
'label' => 'Floor Number',
array(
'type' => 'number',
'step' => 1,
'context' => 'post',
'name' => 'floor_plan_floor_number',
'label' => 'Floor Number',
),
),
'floor_plan_floor_number'
),
'floor_plan_floor_number'
);

pwp_add_metabox(
'RSF',
self::$floor_plan_labels['post_type_name'],
array(
'rsf' => array(
'RSF',
self::$floor_plan_labels['post_type_name'],
array(
'type' => 'number',
'step' => 1,
'context' => 'post',
'name' => 'floor_plan_rsf',
'label' => 'Floor Plan RSF',
array(
'type' => 'number',
'step' => 1,
'context' => 'post',
'name' => 'floor_plan_rsf',
'label' => 'Floor Plan RSF',
),
),
'floor_plan_rsf'
),
'floor_plan_rsf'
);

pwp_add_metabox(
'Floor Plan Downloads',
array( self::$floor_plan_labels['post_type_name'] ),
array(
'name_prefix' => 'floor_plan_downloads',
'downloads' => array (
'Floor Plan Downloads',
array( self::$floor_plan_labels['post_type_name'] ),
array(
'type' => 'wp_media',
'context' => 'post',
'name' => '[pdf]',
'label' => 'PDF',
'name_prefix' => 'floor_plan_downloads',
array(
'type' => 'wp_media',
'context' => 'post',
'name' => '[pdf]',
'label' => 'PDF',
),
),
'floor_plan_downloads'
),
'floor_plan_downloads'
);

$metaboxes = apply_filters( self::$METABOXES_FILTER_HOOK, $maybe_metaboxes );

foreach ($metaboxes as $key => $value) {
pwp_add_metabox(...$value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class Entrata_API {

public static $UNIT_TYPE_NAME_FILTER_HANDLE = 'floor_plans_entrata_unit_type_name';

public static $instance = NULL;

public static function get_inst() {
Expand Down Expand Up @@ -40,7 +42,7 @@ public function get_unit_types() {
$cleaned_unit_types = array();
foreach ($unit_types as $unit_type) {
// group unit types by name
$name = $unit_type->name;
$name = apply_filters(self::$UNIT_TYPE_NAME_FILTER_HANDLE, $unit_type->name);
$id = $unit_type->identificationType->idValue;

if ( ! array_key_exists($name, $cleaned_unit_types) ) {
Expand Down Expand Up @@ -133,12 +135,41 @@ public function get_floor_plans($unit_type_ids, $start_date) {
}
');

return $response->FloorPlans->FloorPlan;
return $this->combine_floorplans($response->FloorPlans->FloorPlan);
} else {
return [];
}
}

private function combine_floorplans($floor_plans_from_entrata) {
$floor_plans = [];

foreach ($floor_plans_from_entrata as $floor_plan_entrata) {
$name = $floor_plan_entrata->Name;
if (!$name) { continue; }

$floor_plans_query = new WP_Query( array(
'post_type' => Torque_Floor_Plan_CPT::$floor_plan_labels['post_type_name'],
'meta_key' => 'entrata_name',
'meta_value' => $name
) );
if ($floor_plans_query->found_posts === 0) { continue; }

$floor_plan_wp = $floor_plans_query->post;
$images = get_post_meta($floor_plan_wp->ID, 'entrata_additional_images', true);
$rsf = get_post_meta($floor_plan_wp->ID, 'floor_plan_rsf', true);

$floor_plans[] = array(
'post_title' => $floor_plan_wp->post_title,
'thumbnail' => get_the_post_thumbnail_url($floor_plan_wp->ID, 'large') ?? '',
'key_plan_src' => $images['key_plan'] ?? '',
'rsf' => $rsf
);
}

return $floor_plans;
}

private function create_GET_request($endpoint, $method, $prevent_exec = false) {
$resCurl = curl_init();

Expand Down
Loading

0 comments on commit f4a6205

Please sign in to comment.