Building a Cardstack app - Part 3

19 January 2018

We ended Part 2 with a working rental listing and rental details page. All information was on the page except for the image of the rental. So in this part, we’re going to add that, make sure that we can switch between the wide and normal image variations and then implement the city filter for searching rentals.

Adding the image field

We start by adding an image field to the Rental model in our model definition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// cardstack/seeds/development/models.js
const Factory = require('@cardstack/test-support/jsonapi-factory');
(...)

function initialModels() {
  let factory = new Factory();
  (...)
  factory.addResource('content-types', 'rentals')
    .withRelated('fields', [
      factory.addResource('fields', 'title')
        .withAttributes({
          'field-type': '@cardstack/core-types::string'
        }),
      (...)
      factory.addResource('fields', 'image')
        .withAttributes({
          'field-type': '@cardstack/core-types::string'
        }),
  ]);
}

We can now display the image in both the listing and the page format for rental cards. Let’s start with the main page where each rental is displayed in its rental format.

We can copy over the markup used by the Super Rentals app for the image:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- app/templates/components/cardstack/rental-listing.hbs -->
<article class="listing">
+  <a {{action (mut isWide) (not isWide)}} class="image {{if isWide "wide"}}">
+    <img src={{content.image}} alt="">
+    <small>View Larger</small>
+  </a>
  <h3><a href={{cardstack-url content}} >{{content.title}}</a></h3>
  <div class="detail owner">
    <span>Owner:</span> {{content.owner}}
  </div>
  <div class="detail type">
    <span>Type:</span> {{rental-property-type content.propertyType}} - {{content.propertyType}}
  </div>
  <div class="detail location">
    <span>Location:</span> {{content.city}}
  </div>
  <div class="detail bedrooms">
    <span>Number of bedrooms:</span> {{content.bedrooms}}
  </div>
  <div class="detail bedrooms">
    <span>Sleeps:</span> {{content.sleeps}}
  </div>
</article>

We only made two small changes in the new snippet. Instead of handling the click with an explicit toggleImageSize action, we implemented it in the template, to avoid having to create a component just for this reason. The other change is the usual one: inside cardstack components, the “model” object passed in is bound to the content property, so content.image needs to be written.

After an app restart (remember, we changed the model schema), the images now show up:

Listing with images

Oh, while we’re at it, let’s add the little map to each location listing. There’s nothing cardstacky about this step, so I’ll skip any explanations:

1
2
3
4
5
<!-- app/templates/components/cardstack/rental-listing.hbs -->
<article class="listing">
  (...)
  {{location-map location=content.city}}
</article>

Listing with images and map

Showing the image on rental details page is even simpler, it only takes one extra line:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!-- app/templates/components/cardstack/rental-page.hbs -->
<div class="jumbo show-listing">
  <h2 class="title">{{content.title}}</h2>
  <div class="right detail-section">
    <div class="detail owner">
      <strong>Owner:</strong> {{content.owner}}
    </div>
    <div class="detail">
      <strong>Type:</strong> {{rental-property-type content.propertyType}} - {{content.propertyType}}
    </div>
    <div class="detail">
      <strong>Location:</strong> {{content.city}}
    </div>
    <div class="detail">
      <strong>Number of bedrooms:</strong> {{content.bedrooms}}
    </div>
    <div class="detail bedrooms">
      <strong>Sleeps:</strong> {{content.sleeps}}
    </div>
    <p>&nbsp;</p>
    <p class="description">{{content.description}}</p>
  </div>
+  <img src={{content.image}} class="rental-pic" />
</div>

Rental details page with image

Filtering by city

You might notice that our Cardstack app is still missing the ability to filter by city name, so let’s add it.

The pure-Ember solution uses a list-filter component which yields its results to the wrapped element (and uses store.query behind the scenes). We already have the cardstack-search component that does this, so we just have to add the input and create an action handler that updates the mainQuery property of the main page.

First, let’s add the input:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- app/templates/components/cardstack/page-page.hbs -->
<h1>{{content.title}}</h1>

+ <div class="list-filter">
+   {{input value=cityFilter key-up=(action 'updateQuery') class="light" placeholder="Filter By City"}}
+ </div>

{{#with content.mainQuery as |query|}}
  {{#cardstack-search query=query as |item|}}
    {{cardstack-content content=item format="listing"}}
+  {{else}}
+    <p class="empty-list-blurb">There are no rentals that match the search criteria.</p>
  {{/cardstack-search}}
{{/with}}

We defined the updateQuery action for updating the query when the input changes its value but it doesn’t exist yet so we’d receive an error. Let’s proactively define the action. We’ll first have to create the component file itself as we haven’t needed it yet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// app/components/cardstack/page-page.js
import Component from '@ember/component';

export default Component.extend({
  actions: {
    updateQuery() {
      let query = { type: 'rentals' };
      let city = this.get('cityFilter');
      if (city) {
        query.filter = {
          city: {
            prefix: city
          }
        }
      }
      this.set('content.mainQuery', query);
    }
  }
});

So if the input, and thus cityFilter, has a non-empty value, we’ll update the query with a filter for the city field. The filter parameter is part of JSON API and is fully supported by Cardstack endpoints, so we expect the above to “just work” as it is.

We set the query to be a prefix one, so that if the user only types ’s’, both the Seattle and the San Francisco rentals should be returned. (As the underlying search engine is Elastic Search, we could choose to have a different type of search like an exact or partial one.)

After the app reloads and we enter a search term, we can see that the right query param is appended to the request,

Rental query in DevTools

and the cardstack-search component indeed correctly returns the rentals that have a matching city name:

Rental listing with city filter

In the next part

We have now reached feature parity with the original Super Rentals app. In the next part(s), we’ll enhance the app with CMS features, so that we can create rentals and edit their details.

If you’re interested in learning more about Cardstack, or contributing to it, check out the official Cardstack site and the source code repository. If you have any questions, hop into our chat and ask away!