File size: 1,489 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { memoize, pick } from 'lodash';

class WpcomTaskList {
	constructor( tasks = [] ) {
		this.tasks = tasks;
	}

	getAll() {
		return this.tasks;
	}

	getAllSorted = memoize( ( comparator ) => [ ...this.tasks ].sort( comparator ) );

	get( taskId ) {
		return this.tasks.find( ( task ) => task.id === taskId );
	}

	getIds() {
		return this.getAll().map( ( { id } ) => id );
	}

	isCompleted( taskId ) {
		const task = this.get( taskId );
		return task ? task.isCompleted : false;
	}

	has( taskId ) {
		return !! this.get( taskId );
	}

	remove( taskId ) {
		const found = this.get( taskId );
		if ( ! found ) {
			return null;
		}
		this.tasks = this.tasks.filter( ( task ) => task.id !== taskId );
		return found;
	}

	removeTasksWithoutUrls( taskUrls ) {
		const hasUrl = ( task ) => ! ( task.id in taskUrls ) || taskUrls[ task.id ];

		this.tasks = this.tasks.filter( hasUrl );
	}

	getFirstIncompleteTask() {
		return this.tasks.find( ( task ) => ! task.isCompleted );
	}

	getCompletionStatus() {
		const completed = this.tasks.filter( ( task ) => task.isCompleted ).length;
		const total = this.tasks.length;

		return {
			completed,
			total,
			percentage: Math.round( ! total ? 0 : ( completed / total ) * 100 ),
		};
	}
}

export const getTaskList = memoize(
	( params ) => new WpcomTaskList( params?.taskStatuses ),
	( params ) => {
		const key = pick( params, [ 'taskStatuses', 'designType', 'siteIsUnlaunched', 'siteSegment' ] );
		return JSON.stringify( key );
	}
);