vue.jsvuejs2flickr

Searching through Flickr API using tags in Vue


I have a simple Flickr photo feed, which i'm trying to add a search bar to, so that as a user types a search term, the app returns any photos that have tags that include that search term.

The app loads fine with no console errors, but as soon as i type anything in the search bar all the photos disappear, no new ones are returned but I don't get any console errors, so i'm not sure where i'm going wrong.

My code:

<template>
  <b-container>
    <b-row>
      <b-col md="12">
        <b-input-group v-model="search" size="lg" prepend="Search" class="flickr-search">
          <b-form-input></b-form-input>
        </b-input-group>
      </b-col>
    </b-row>
    <b-row>
      <b-card-group columns>
        <b-col v-for="photo in filteredPhotos" class="item" md="12">
          <b-card :title="photo.title"
                  :img-src="photo.media.m"
                  img-alt="Image"
                  img-top
                  img-fluid
                  tag="article"
                  style="max-width: 20rem;"
                  class="mb-2">
            <span class="item-date">31 May 2017</span>
            <hr/>
            <p>By <a :href="'https://www.flickr.com/photos/' + photo.author_id" :title="formatAuthor(photo.author)" target="_blank">{{ formatAuthor(photo.author) }}</a></p>
            <ul class="tags">
              <li v-for="tag in splitTags(photo.tags)" class="item-tag">
                <a :href="'https://www.flickr.com/photos/tags/' + tag" target="_blank" class="item-taglink">{{ tag }}</a>
              </li>
            </ul>
          </b-card>
        </b-col>
      </b-card-group>
    </b-row>
  </b-container>
</template>

<script>
    import jsonp from "jsonp";

export default {
    name: 'PhotoFeed',
    data: function () {
        return {
            Photos: [],
            apiURL: "https://api.flickr.com/services/feeds/photos_public.gne?format=json",
            search: ''
        }
    },
    mounted(){
        this.getFlickrFeed();
    },
    methods: {
        getFlickrFeed(){
            let jsonp = require('jsonp');
            var self = this;

            jsonp(this.apiURL, {name: 'jsonFlickrFeed'}, (err, data) => {
                if (err) {
                    console.log(err.message);
                }
                else {
                    self.Photos = data.items;
                }
            })
        },
        formatAuthor(authorString){
            if (authorString) return authorString.split("\"")[1];
            return "Author";
        },
        splitTags(tagsString) {
            if (tagsString) return tagsString.split(" ");
        }
    },
    computed: {
        filteredPhotos(){
            return this.Photos.filter(photo => {
                return photo.tags.includes(this.search);
            })
        }
    }
}
</script>

Solution

  • Your core problem is v-model="search" is attached to the <b-input-group> instead of the <b-form-input> which contains the search string, so this.search isn't the string you're hoping for. Change the search input to:

    <b-input-group size="lg" prepend="Search" class="flickr-search">
        <b-form-input v-model="search"></b-form-input>
    </b-input-group>