import { forEach } from 'lodash-es';
import { RECEIVE_WIKIDATA_LABELS } from '../constants';

const initialState = {
  labels: {}
};

export default function wikidataLabels(state = initialState, action) {
  switch (action.type) {
    case RECEIVE_WIKIDATA_LABELS: {
      const newLabels = { ...state.labels };
      forEach(action.data.entities, (entity) => {
        const label = entity.labels[action.language] || entity.labels.en;
        if (!label) { return; }
        newLabels[entity.id] = label.value;
      });
      return { labels: newLabels };
    }
    default:
      return state;
  }
}

import { forEach } from 'lodash-es';
import { RECEIVE_WIKIDATA_LABELS } from '../constants';

const initialState = {
  labels: {}
};

export default function wikidataLabels(state = initialState, action) {
  switch (action.type) {
    case RECEIVE_WIKIDATA_LABELS: {
      const newLabels = { ...state.labels };
      forEach(action.data.entities, (entity) => {
        if (!entity.labels) { return; }
        const label = entity.labels[action.language] || entity.labels.en;
        if (!label) { return; }
        newLabels[entity.id] = label.value;
      });
      return { labels: newLabels };
    }
    default:
      return state;
  }
}

Fix error caused by a deleted Wikidata item
Fix error caused by a deleted Wikidata item

If an edited item was deleted, the request for labels for that item will not include a `labels` property for it.
