import faker from 'faker' ; import { Issue , Priority , Status , User } from 'types/issue' ; let globalID = 0 ; function generateId ( ) { globalID ++ ; return globalID ; } /**
 * fake API calls
 */ const repo = { [ Status . BACKLOG ] : randomIssues ( Status . BACKLOG ) , [ Status . TODO ] : randomIssues ( Status . TODO ) , [ Status . IN_PROGRESS ] : randomIssues ( Status . IN_PROGRESS ) , [ Status . DONE ] : randomIssues ( Status . DONE ) , [ Status . CANCELED ] : randomIssues ( Status . CANCELED ) , } ; function randomIssue ( status ? ) { var keyPriority = Object . values ( Priority ) [ Math . floor ( Math . random ( ) * Object . keys ( Priority ) . length ) ] ; let keyStatus = status || Object . values ( Status ) [ Math . floor ( Math . random ( ) * Object . keys ( Status ) . length ) ] ; return { id : 'GIT-' + generateId ( ) , priority : keyPriority , description : '' , status : keyStatus , title : faker . lorem . words ( 20 ) , owner : randomOwner ( ) , createdAt : new Date ( ) , } ; } function randomIssues ( status ) { let numIssues = Math . floor ( Math . random ( ) * 10 ) ; var issues = [ ] ; for ( let i = 0 ; i < numIssues ; i ++ ) { let issue = randomIssue ( status ) ; issues . push ( issue ) ; } return issues ; } function randomOwner ( ) { return { name : faker . name . findName ( ) , avatar : faker . image . avatar ( ) } ; } function loadIssues ( ) { return repo ; } function createIssue ( issue ) { issue . id = 'GIT-' + generateId ( ) ; issue . createdAt = new Date ( ) ; issue . owner = { name : 'Tuan Nguyen' } ; let oldIssues = repo [ issue . status ] ; oldIssues . push ( issue ) ; repo [ issue . status ] = [ ... oldIssues ] ; } function updateIssueStatusAndPos ( issueId , srcStatus , destStatus , srcPos , destPos ) { let sourceList = repo [ srcStatus ] ; let destList = repo [ destStatus ] ; if ( srcStatus == destStatus ) { const issues = sourceList . splice ( srcPos , 1 ) ; issues . splice ( destPos , 0 , issues [ 0 ] ) ; repo [ srcStatus ] = [ ... issues ] ; } else { const issues = sourceList . splice ( srcPos , 1 ) ; issues [ 0 ] . status = destStatus ; destList . splice ( destPos , 0 , issues [ 0 ] ) ; repo [ srcStatus ] = [ ... sourceList ] ; repo [ destStatus ] = [ ... destList ] ; } } function updateIssuePriority ( issueId , status , newPriority ) { let list = repo [ status ] ; list . forEach ( ( issue , index ) => { if ( issue . id == issueId ) { issue . priority = newPriority ; issue = { ... issue } ; list [ index ] = issue ; } } ) ; repo [ status ] = [ ... list ] ; } function updateIssueStatus ( issueId , status , newStatus ) { if ( status == newStatus ) return ; let list = repo [ status ] ; let pos = 0 ; list . forEach ( ( issue , index ) => { if ( issue . id == issueId ) { pos = index ; } } ) ; let issues = list . splice ( pos , 1 ) ; issues [ 0 ] . status = newStatus ; repo [ status ] = [ ... list ] ; repo [ newStatus ] . push ( issues [ 0 ] ) ; } export default { loadIssues , updateIssueStatusAndPos , createIssue , updateIssuePriority , updateIssueStatus } ;