VtEcostudies / VAL_Data_Explorers

Other
1 stars 1 forks source link

Literature widget #10

Open MortenHofft opened 2 years ago

MortenHofft commented 2 years ago

We actually have a literature data widgets as well. Perhaps you should use that instead of linking to GBIF.org? You can see an exmaple here https://hp-bison.gbif-staging.org/literature https://github.com/gbif/hp-bison

jloomisVCE commented 2 years ago

Yes I think so? I assume you mean to replace our Citations target? @kpmcfarland what do you think?

kpmcfarland commented 2 years ago

Sure. Sounds good to me.

MortenHofft commented 2 years ago

@jloomisVCE you can see how it is done by looking at the source code for https://hp-bison.gbif-staging.org/literature - it is the same process as for the occurrence search, just a different component and different configuration https://hp-bison.gbif-staging.org/assets/theme/js/config.js

jloomisVCE commented 2 years ago

Thanks. I think I got it to work: https://val.vtecostudies.org/gbif-literature/

Question: is the list of eg. dataSets under 'More...' filtered by our root predicate, or is that a global list?

MortenHofft commented 2 years ago

Global I'm afraid. It is a general challenge we have with suggests, that they use the same endpoints for everyone.

jloomisVCE commented 2 years ago

Thanks. It works very well as it is!

I was able to filter the species suggest to our species checklist as you noted. So hypothetically would it be possible to filter these lists using the root predicate?

MortenHofft commented 2 years ago

As a general rule it isn't really possible to constrain the suggests to your root scope. But in your specific case it might actually be possible as your scope is as simple as a single publisher. And I notice that the suggest API supports filtering by publishers. https://www.gbif.org/developer/registry#datasetSearch

Then we just need a way to overwrite the default suggest query to include the publisher as an extra parameter. I'll see if I can find a way

MortenHofft commented 2 years ago

In your configuration for the literature widget you can do https://github.com/VtEcostudies/VAL_GBIF_Wordpress/blob/c632de904f050866a36c0fc92468189ffe410d8f/gbif_lit_widget.js#L56

// create a custom suggest
function getSuggests({ client, suggestStyle }) {
  return {
    vceDatasetSuggest: {
      //What placeholder to show
      placeholder: 'search.placeholders.default', //pointing to the translation file
      // how to get the list of suggestion data
      getSuggestions: ({ q }) => client.v1Get(`/dataset/suggest?publishingOrg=b6d09100-919d-4026-b35b-22be3dae7156&limit=8&q=${q}`), // this is the only part we really care to change
      // how to map the results to a single string value
      getValue: suggestion => suggestion.title,
      // how to display the individual suggestions in the list
      render: function DatasetSuggestItem(suggestion) {
        return <div style={{}}>
          <div style={suggestStyle}>
            {suggestion.title}
          </div>
        </div>
      }
    },
  };
}

// overwrite the default dataset filter to use the custom suggest
const filters = {
  datasetKey: {
    type: 'SUGGEST',
    config: {
      std: {
        filterHandle: 'datasetKey',
        translations: {
          count: 'filters.datasetKey.count', // translation path to display names with counts. e.g. "3 scientific names"
          name: 'filters.datasetKey.name',// translation path to a title for the popover and the button
          description: 'filters.datasetKey.description', // translation path for the filter description
        },
      },
      specific: {
        suggestHandle: 'vceDatasetSuggest', // <=== this is where we change the suggest
        allowEmptyQueries: true
      }
    }
  }
}

and then in your site config add. As a type this I realise that this mean that you cannot reuse your config for occurrences and literature. But you have two different as it is, so it isn't an issue.

getSuggests: getSuggests, 
filters: filters, 

That should reduce the dataset suggest on literature to only suggest datasets by the VCE publisher. I'm curious to see if this works. I've added the option for some degree of overwriting, but this is really the first time someone needs it.

jloomisVCE commented 2 years ago

Thanks for this! I tried it and it appears to work. I had only one edit - quotes around the render function's return value.

MortenHofft commented 2 years ago

Oh yes, I forgot that part. I'm surprised if it works with the quotes. I would not expect either version to work. I wrote it like that because it will work for me as a I have a transpiler that transforms it.

Do you have a public link to the version you have running with it? Else perhaps just change it to?

render: function DatasetSuggestItem(suggestion) {return suggestion.title;}
jloomisVCE commented 2 years ago

Here's that line. I will also make the suggested change.