@@ -42,20 +42,33 @@ export default class Overview extends Observable {
4242 * @returns {undefined } Injects the data object with the response data
4343 */
4444 async fetchAllLogs ( offset = 0 ) {
45- this . logs = RemoteData . loading ( ) ;
46- this . notify ( ) ;
45+ if ( ! this . model . tags . getTags ( ) . isSuccess ( ) ) {
46+ this . logs = RemoteData . loading ( ) ;
47+ this . notify ( ) ;
48+ }
4749
48- const endpoint = `/api/logs?page[offset]=${ offset } &page[limit]=${ this . logsPerPage } ` ;
50+ const params = {
51+ 'page[offset]' : offset ,
52+ 'page[limit]' : this . logsPerPage ,
53+ ...this . filterTags . length > 0 && {
54+ 'filter[tag][values]' : this . filterTags . join ( ) ,
55+ 'filter[tag][operation]' : this . filterOperation . toLowerCase ( ) ,
56+ } ,
57+ } ;
58+
59+ const endpoint = `/api/logs?${ new URLSearchParams ( params ) . toString ( ) } ` ;
4960 const response = await fetchClient ( endpoint , { method : 'GET' } ) ;
5061 const result = await response . json ( ) ;
5162
5263 if ( result . data ) {
5364 this . logs = RemoteData . success ( result . data ) ;
54- this . filteredLogs = RemoteData . success ( result . data ) ;
5565 this . totalPages = result . meta . page . pageCount ;
5666 } else {
5767 this . logs = RemoteData . failure ( result . errors ) ;
58- this . filteredLogs = RemoteData . failure ( result . errors ) ;
68+ }
69+
70+ if ( this . model . tags . getTags ( ) . isNotAsked ( ) ) {
71+ await this . model . tags . fetchAllTags ( ) ;
5972 }
6073
6174 this . notify ( ) ;
@@ -75,10 +88,8 @@ export default class Overview extends Observable {
7588
7689 if ( result . data ) {
7790 this . logs = RemoteData . success ( [ result . data ] ) ;
78- this . filteredLogs = RemoteData . success ( [ result . data ] ) ;
7991 } else {
8092 this . logs = RemoteData . failure ( result . errors ) ;
81- this . filteredLogs = RemoteData . failure ( result . errors ) ;
8293 }
8394 this . notify ( ) ;
8495 }
@@ -164,7 +175,15 @@ export default class Overview extends Observable {
164175 * @returns {RemoteData } Returns all of the filtered logs
165176 */
166177 getLogs ( ) {
167- return this . filteredLogs ;
178+ return this . logs ;
179+ }
180+
181+ /**
182+ * Getter for the filter operation
183+ * @returns {String } The filter operation to be performed (AND, OR)
184+ */
185+ getFilterOperation ( ) {
186+ return this . filterOperation ;
168187 }
169188
170189 /**
@@ -221,8 +240,8 @@ export default class Overview extends Observable {
221240 * @returns {Array } Sets the data according to the filters applied
222241 */
223242 addFilter ( tag ) {
224- this . filterCriteria = [ ...this . filterCriteria , tag ] ;
225- this . setFilteredData ( ) ;
243+ this . filterTags = [ ...this . filterTags , tag ] ;
244+ this . fetchAllLogs ( ) ;
226245 }
227246
228247 /**
@@ -231,28 +250,8 @@ export default class Overview extends Observable {
231250 * @returns {Array } Sets the array with the new filter criteria
232251 */
233252 removeFilter ( condition ) {
234- this . filterCriteria = this . filterCriteria . filter ( ( tag ) => tag !== condition ) ;
235- this . setFilteredData ( ) ;
236- }
237-
238- /**
239- * Filter the data
240- * @returns {Array } Sets the data according to the amount of applied filters
241- */
242- setFilteredData ( ) {
243- this . filterCriteria . length !== 0
244- ? this . filterByTags ( )
245- : this . filteredLogs = RemoteData . success ( this . logs . payload ) ;
246-
247- this . notify ( ) ;
248- }
249-
250- /**
251- * Filter data by tags if applicable
252- * @returns {Array } Sets the filtered data on the criteria applied by the user
253- */
254- filterByTags ( ) {
255- this . filteredLogs = RemoteData . success ( this . logs . payload . filter ( ( entry ) => this . hasExistingTag ( entry ) ) ) ;
253+ this . filterTags = this . filterTags . filter ( ( tag ) => tag !== condition ) ;
254+ this . fetchAllLogs ( ) ;
256255 }
257256
258257 /**
@@ -261,33 +260,28 @@ export default class Overview extends Observable {
261260 * @return {Boolean } Returns the status of the existence of a tag in the data entry
262261 */
263262 hasExistingTag ( entry ) {
264- return this . filterCriteria . filter ( ( tag ) => entry . tags . filter ( ( { text } ) => tag === text ) . length > 0 ) . length > 0 ;
263+ return this . filterTags . filter ( ( tag ) => entry . tags . filter ( ( { text } ) => tag === text ) . length > 0 ) . length > 0 ;
265264 }
266265
267266 /**
268- * Counts the tags with their total appearances
269- * @return {Object } Returns the count of each tag
267+ * Checks if a tag is already defined within the user's filter criteria
268+ * @param {String } tag The tag to check on
269+ * @return {Boolean } Whether the tag is in the user's filter criteria
270270 */
271- getTagCounts ( ) {
272- if ( this . logs . isSuccess ( ) ) {
273- return this . logs . payload . reduce ( ( accumulator , currentValue ) => {
274- currentValue . tags . forEach ( ( { text : tag } ) => {
275- accumulator [ tag ] = ( accumulator [ tag ] || 0 ) + 1 ;
276- } ) ;
277- return accumulator ;
278- } , { } ) ;
279- } else {
280- return { } ;
281- }
271+ isTagInFilter ( tag ) {
272+ return this . filterTags . includes ( tag ) ;
282273 }
283274
284275 /**
285- * Checks if a tag is already defined within the user's filter criteria
286- * @param {String } tag The tag to check on
287- * @return { Boolean } Whether the tag is in the user's filter criteria
276+ * Sets the filter operation according to the user input
277+ * @param {String } operation The filter operation to be performed (AND, OR)
278+ * @returns { undefined }
288279 */
289- isTagInFilterCriteria ( tag ) {
290- return this . filterCriteria . includes ( tag ) ;
280+ setFilterOperation ( operation ) {
281+ this . filterOperation = operation ;
282+ if ( this . filterTags . length > 0 ) {
283+ this . fetchAllLogs ( ) ;
284+ }
291285 }
292286
293287 /**
@@ -343,8 +337,8 @@ export default class Overview extends Observable {
343337 */
344338 clearLogs ( ) {
345339 this . logs = RemoteData . NotAsked ( ) ;
346- this . filteredLogs = RemoteData . NotAsked ( ) ;
347- this . filterCriteria = [ ] ;
340+ this . filterTags = [ ] ;
341+ this . filterOperation = 'AND' ;
348342 this . moreFilters = false ;
349343
350344 this . amountDropdownVisible = false ;
0 commit comments