All files / ext.wikilambda.app/store/modules/wikidata items.js

100% Statements 103/103
93.33% Branches 14/15
100% Functions 8/8
100% Lines 103/103

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 1047x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 4x 4x 4x 4x 4x 8x 8x 4x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 7x 7x  
/*!
 * WikiLambda Vue editor: Vuex Wikidata Items module
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
 
const Constants = require( '../../../Constants.js' ),
	apiUtils = require( '../../../mixins/api.js' ).methods;
 
module.exports = exports = {
	state: {
		items: {}
	},
	getters: {
		/**
		 * Returns the Wikidata Item data given its Id,
		 * the fetch Promise if the fetch request is on the fly,
		 * or undefined if it hasn't been requested yet.
		 *
		 * @param {Object} state
		 * @return {Function}
		 */
		getItemData: function ( state ) {
			/**
			 * @param {string} id
			 * @return {Object|Promise|undefined}
			 */
			function findItemData( id ) {
				return state.items[ id ];
			}
			return findItemData;
		},
 
		/**
		 * Given the rowId of the Wikidata Item entity
		 * returns the rowId of the Wikidata Item Id string.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getItemIdRow: function ( _state, getters ) {
			/**
			 * @param {number} rowId
			 * @return {Object|undefined}
			 */
			function findItemId( rowId ) {
				return getters.getWikidataEntityIdRow( rowId, Constants.Z_WIKIDATA_ITEM );
			}
			return findItemId;
		}
	},
	mutations: {
		/**
		 * Stores the Wikidata item data indexed by its Id
		 *
		 * @param {Object} state
		 * @param {Object} payload
		 * @param {string} payload.id
		 * @param {Object} payload.data
		 */
		setItemData: function ( state, payload ) {
			// Select only subset of Wikidata Item data; title and labels
			const unwrap = ( ( { title, labels } ) => ( { title, labels } ) );
			state.items[ payload.id ] = unwrap( payload.data );
		}
	},
	actions: {
		/**
		 * Calls Wikidata Action API to fetch Wikidata Items
		 * given their Ids.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {Array} payload.ids
		 * @return {Promise}
		 */
		fetchItems: function ( context, payload ) {
			// Filter out the fetched or fetching Wikidata Item Ids
			const ids = payload.ids.filter( ( id ) => context.getters.getItemData( id ) === undefined );
			if ( ids.length === 0 ) {
				// If list is empty, do nothing
				return;
			}
			const request = {
				language: context.getters.getUserLangCode,
				ids: ids.join( '|' )
			};
			const promise = apiUtils.fetchWikidataEntities( request )
				.then( ( data ) => {
					// Once received, store Wikidata Item Ids with their data
					const fetched = data.entities ? Object.keys( data.entities ) : [];
					fetched.forEach( ( id ) => context.commit( 'setItemData', { id, data: data.entities[ id ] } ) );
					return data;
				} );
 
			// Store Wikidata Item Ids with their resolving promise
			ids.forEach( ( id ) => context.commit( 'setItemData', { id, data: promise } ) );
			return promise;
		}
	}
};