import { Dispatch } from 'redux' ; import socket from 'utils/socket' ; import { CLEAR_ALL_ERRORS } from './errors' ; import { ApiAction } from 'store/middlewares/apiMiddleware' ; import { createAPIAction } from 'store/helpers' ; import { toggleArrayItem } from 'utils' ; import { normalize , schema } from 'normalizr' ; import { commentSchema , bugSchema } from 'store/schemas' ; export const API = 'API' ; export const CLEAR_BUG_DATA = 'singlebug/CLEAR_BUG_DATA' ; export const FETCH_BUG = createAPIAction ( 'singlebug/FETCH_BUG' ) ; export const TOGGLE_BUG = createAPIAction ( 'singlebug/TOGGLE_BUG' ) ; export const EDIT_LABELS = createAPIAction ( 'singlebug/EDIT_LABELS' ) ; export const UPDATE_BUG = createAPIAction ( 'singlebug/UPDATE_BUG' ) ; export const UPDATE_REACTIONS = createAPIAction ( 'singlebug/UPDATE_REACTIONS' ) ; export const ADD_COMMENT = createAPIAction ( 'singlebug/ADD_COMMENT' ) ; export const EDIT_COMMENT = createAPIAction ( 'singlebug/EDIT_COMMENT' ) ; export const UPDATE_COMMENT_REACTIONS = createAPIAction ( 'singlebug/UPDATE_COMMENT_REACTIONS' ) ; export const COMMENT_REACTIONS_OPTI = 'singlebug/COMMENT_REACTIONS_OPTI' ; export interface SinglebugReducerState { result ? : { [ x ] : any ; } ; entities ? : { [ x ] : any ; } ; } const DEFAULT_STATE = { } ; const reducer = ( state = DEFAULT_STATE , action ) => { switch ( action . type ) { case FETCH_BUG . SUCCESS : return { ... state , ... action . payload , } ; case ADD_COMMENT . SUCCESS : return { ... state , entities : { reactions : { ... state ?. entities ?. reactions , } , comments : { ... state ?. entities ?. comments , } , } , result : { ... state . result , comments : [ ... state ?. result ?. comments , action . payload [ action . payload . length - 1 ] . id , ] , } , } ; case EDIT_COMMENT . SUCCESS : return { ... state , entities : { comments : { ... state ?. entities ?. comments , } , } , } ; case UPDATE_BUG . SUCCESS : return { ... state , result : { ... state . result , body : action . payload . body , } , } ; case TOGGLE_BUG . SUCCESS : return { ... state , result : { ... state . result , activities : action . payload . data , isOpen : action . payload . bug_state === 'open' ? true : false , } , } ; case EDIT_LABELS . SUCCESS : return { ... state , result : { ... state . result , labels : action . payload , } , } ; case UPDATE_REACTIONS . SUCCESS : return { ... state , reactions : action . payload , } ; case UPDATE_COMMENT_REACTIONS . SUCCESS : return { ... state , comments : action . payload , } ; case COMMENT_REACTIONS_OPTI : return { ... state , } ; case CLEAR_BUG_DATA : return { ... state , bug : null } ; default : return state ; } } ; export default reducer ; export const fetchBugWithId = ( bugId ) : ApiAction => ( { type : API , payload : { method : 'GET' , url : `/api/bugs/${ bugId }` , } , onRequest : ( dispatch ) => { dispatch ( { type : CLEAR_ALL_ERRORS } ) ; dispatch ( { type : CLEAR_BUG_DATA } ) ; dispatch ( { type : FETCH_BUG . REQUEST } ) ; } , onSuccess : ( dispatch , data ) => { let norm = normalize ( data , [ bugSchema ] ) ; console . log ( norm ) ; dispatch ( { type : FETCH_BUG . SUCCESS , payload : norm , } ) ; } , onFailure : FETCH_BUG . FAILURE , } ) ; export const addComment = ( bugId , formData ) : ApiAction => ( { type : API , payload : { method : 'PATCH' , url : `/api/bugs/${ bugId }/comments` , formData : formData , } , onRequest : ADD_COMMENT . REQUEST , onSuccess : ( dispatch , data ) => { dispatch ( { type : ADD_COMMENT . SUCCESS , payload : data } ) ; socket . emit ( 'send-notification' , { message : 'Add comment' } ) ; } , onFailure : ADD_COMMENT . FAILURE , } ) ; export const editComment = ( bugId , commentId , formData ) : ApiAction => ( { type : API , payload : { method : 'PATCH' , url : `/api/bugs/${ bugId }/comments/${ commentId }` , formData : formData , } , onRequest : EDIT_COMMENT . REQUEST , onSuccess : EDIT_COMMENT . SUCCESS , onFailure : EDIT_COMMENT . FAILURE , } ) ; export const updateBug = ( bugId , formData ) : ApiAction => ( { type : API , payload : { method : 'PATCH' , url : `/api/bugs/${ bugId }` , formData : formData , } , onRequest : UPDATE_BUG . REQUEST , onSuccess : UPDATE_BUG . SUCCESS , onFailure : UPDATE_BUG . FAILURE , } ) ; export const openOrCloseBug = ( bugId , state ) : ApiAction => ( { type : API , payload : { method : 'PATCH' , url : `/api/bugs/${ bugId }/${ state }` , } , onRequest : TOGGLE_BUG . REQUEST , onSuccess : ( dispatch , data ) => { dispatch ( { type : TOGGLE_BUG . SUCCESS , payload : { data , bug_state : state } , } ) ; socket . emit ( 'send-notification' , { message : 'Bug Open/Closed' } ) ; } , onFailure : TOGGLE_BUG . FAILURE , } ) ; export const editLabels = ( bugId , labelData ) : ApiAction => ( { type : API , payload : { method : 'PATCH' , url : `/api/bugs/${ bugId }/labels` , formData : { labels : labelData } , } , onRequest : EDIT_LABELS . REQUEST , onSuccess : ( dispatch , data ) => { dispatch ( { type : EDIT_LABELS . SUCCESS , payload : data } ) ; } , onFailure : EDIT_LABELS . FAILURE , } ) ; export const addReferences = ( bugId , references ) : ApiAction => ( { type : API , payload : { method : 'PATCH' , url : `/api/bugs/${ bugId }/references` , formData : { references } , } , onSuccess : ( ) => { socket . emit ( 'send-notification' , { message : 'Bugs Referenced' } ) ; } , } ) ; export const mentionPeople = ( bugId , mentions ) : ApiAction => ( { type : API , payload : { method : 'POST' , url : `/api/notifications/mentions/${ bugId }` , formData : { mentions } , } , onSuccess : ( ) => { socket . emit ( 'send-notification' , { message : 'Users Mentioned' } ) ; } , } ) ; export const addOrRemoveReacts = ( bugId , emoji ) : ApiAction => ( { type : API , payload : { method : 'PATCH' , url : `/api/bugs/${ bugId }/reactions` , formData : { emoji } , } , onSuccess : UPDATE_REACTIONS . SUCCESS , onFailure : UPDATE_REACTIONS . FAILURE , } ) ; export const addOrRemoveReactsComment = ( bugId , commentId , emoji ) : ApiAction => ( { type : API , payload : { method : 'PATCH' , url : `/api/bugs/${ bugId }/comments/${ commentId }/reactions` , formData : { emoji } , } , onSuccess : UPDATE_COMMENT_REACTIONS . SUCCESS , onFailure : UPDATE_COMMENT_REACTIONS . FAILURE , } ) ;