2018-10-30 07:40:21 -07:00
import { UserProposal , UserComment , TeamMember } from 'types' ;
2018-10-04 21:27:02 -07:00
import types from './types' ;
2018-10-30 07:40:21 -07:00
import { getUser , updateUser as apiUpdateUser , getProposals } from 'api/api' ;
2018-10-03 10:11:44 -07:00
import { Dispatch } from 'redux' ;
2018-10-04 21:27:02 -07:00
import { Proposal } from 'types' ;
2018-10-03 10:11:44 -07:00
import BN from 'bn.js' ;
2018-10-30 07:40:21 -07:00
import { cleanClone } from 'utils/helpers' ;
import { INITIAL_TEAM_MEMBER_STATE } from 'modules/users/reducers' ;
2018-10-03 10:11:44 -07:00
export function fetchUser ( userFetchId : string ) {
return async ( dispatch : Dispatch < any > ) = > {
dispatch ( { type : types . FETCH_USER_PENDING , payload : { userFetchId } } ) ;
try {
const { data : user } = await getUser ( userFetchId ) ;
dispatch ( {
type : types . FETCH_USER_FULFILLED ,
payload : { userFetchId , user } ,
} ) ;
} catch ( error ) {
dispatch ( { type : types . FETCH_USER_REJECTED , payload : { userFetchId , error } } ) ;
}
} ;
}
2018-10-30 07:40:21 -07:00
export function updateUser ( user : TeamMember ) {
const userClone = cleanClone ( INITIAL_TEAM_MEMBER_STATE , user ) ;
return async ( dispatch : Dispatch < any > ) = > {
dispatch ( { type : types . UPDATE_USER_PENDING , payload : { user } } ) ;
try {
const { data : updatedUser } = await apiUpdateUser ( userClone ) ;
dispatch ( {
type : types . UPDATE_USER_FULFILLED ,
payload : { user : updatedUser } ,
} ) ;
} catch ( error ) {
dispatch ( { type : types . UPDATE_USER_REJECTED , payload : { user , error } } ) ;
}
} ;
}
2018-10-03 10:11:44 -07:00
export function fetchUserCreated ( userFetchId : string ) {
return async ( dispatch : Dispatch < any > ) = > {
dispatch ( { type : types . FETCH_USER_CREATED_PENDING , payload : { userFetchId } } ) ;
try {
// temporary, grab all proposals
const proposalsRes = await getProposals ( ) ;
const proposals = proposalsRes . data . map ( mockModifyProposals ) ;
dispatch ( {
type : types . FETCH_USER_CREATED_FULFILLED ,
payload : { userFetchId , proposals } ,
} ) ;
} catch ( error ) {
dispatch ( {
type : types . FETCH_USER_CREATED_REJECTED ,
payload : { userFetchId , error } ,
} ) ;
}
} ;
}
export function fetchUserFunded ( userFetchId : string ) {
return async ( dispatch : Dispatch < any > ) = > {
dispatch ( { type : types . FETCH_USER_FUNDED_PENDING , payload : { userFetchId } } ) ;
try {
// temporary, grab all proposals
const proposalsRes = await getProposals ( ) ;
const proposals = proposalsRes . data . map ( mockModifyProposals ) ;
dispatch ( {
type : types . FETCH_USER_FUNDED_FULFILLED ,
payload : { userFetchId , proposals } ,
} ) ;
} catch ( error ) {
dispatch ( {
type : types . FETCH_USER_FUNDED_REJECTED ,
payload : { userFetchId , error } ,
} ) ;
}
} ;
}
export function fetchUserComments ( userFetchId : string ) {
return async ( dispatch : Dispatch < any > ) = > {
dispatch ( { type : types . FETCH_USER_COMMENTS_PENDING , payload : { userFetchId } } ) ;
try {
// temporary, grab all proposals, mock comments
const proposalsRes = await getProposals ( ) ;
const proposals = proposalsRes . data . map ( mockModifyProposals ) ;
const comments = mockComments ( proposals ) ;
comments . sort ( ( a , b ) = > ( a . dateCreated > b . dateCreated ? - 1 : 1 ) ) ;
dispatch ( {
type : types . FETCH_USER_COMMENTS_FULFILLED ,
payload : { userFetchId , comments } ,
} ) ;
} catch ( error ) {
dispatch ( {
type : types . FETCH_USER_COMMENTS_REJECTED ,
payload : { userFetchId , error } ,
} ) ;
}
} ;
}
const mockModifyProposals = ( p : Proposal ) : UserProposal = > {
const { proposalId , title , team } = p ;
return {
proposalId ,
title ,
team ,
funded : new BN ( '5000000000000000000' ) ,
target : new BN ( '10000000000000000000' ) ,
brief : genBrief ( title ) ,
} ;
} ;
const genBrief = ( title : string ) = > {
return title . indexOf ( 'T-Shirts' ) > - 1
? 'Stylish, classy logo tees for Grant.io! Show everyone your love for the future of crowdfunding!'
: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' ;
} ;
const mockComments = ( ps : UserProposal [ ] ) : UserComment [ ] = > {
return ps . reduce ( ( a : UserComment [ ] , p ) = > a . concat ( mockComment ( p ) ) , [ ] ) ;
} ;
const mockComment = ( p : UserProposal ) : UserComment [ ] = > {
return p . title . indexOf ( 'T-Shirts' ) > - 1
? [
{
commentId : Math.random ( ) ,
body : "I can't WAIT to get my t-shirt!" ,
dateCreated : Date.now ( ) - Math . floor ( Math . random ( ) * 1000 * 60 * 60 * 24 * 30 ) ,
proposal : p ,
} ,
{
commentId : Math.random ( ) ,
body : 'I love the new design. Will they still be available next month?' ,
dateCreated : Date.now ( ) - Math . floor ( Math . random ( ) * 1000 * 60 * 60 * 24 * 30 ) ,
proposal : p ,
} ,
]
: [
{
commentId : Math.random ( ) ,
body : 'Ut labore et dolore magna aliqua.' ,
dateCreated : Date.now ( ) - Math . floor ( Math . random ( ) * 1000 * 60 * 60 * 24 * 30 ) ,
proposal : p ,
} ,
{
commentId : Math.random ( ) ,
body :
'Adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' ,
dateCreated : Date.now ( ) - Math . floor ( Math . random ( ) * 1000 * 60 * 60 * 24 * 30 ) ,
proposal : p ,
} ,
{
commentId : Math.random ( ) ,
body :
'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' ,
dateCreated : Date.now ( ) - Math . floor ( Math . random ( ) * 1000 * 60 * 60 * 24 * 30 ) ,
proposal : p ,
} ,
{
commentId : Math.random ( ) ,
body :
'Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.' ,
dateCreated : Date.now ( ) - Math . floor ( Math . random ( ) * 1000 * 60 * 60 * 24 * 30 ) ,
proposal : p ,
} ,
] ;
} ;