Created
February 12, 2020 16:24
-
-
Save chrisoverstreet/47504878568fd6557b64e9d7efec914a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
import PropTypes from 'prop-types'; | |
import React from 'react'; | |
import { connect } from 'react-redux'; | |
import { useApolloClient } from '@apollo/react-hooks'; | |
import type { Dispatch } from 'redux'; | |
import consumerRequestActions from './consumerRequestActionsMap'; | |
import jobActions from './jobActionsMap'; | |
import type { GET_JOB_getJob as Job } from './workbench/queries/__generated__/GET_JOB'; | |
import type { GET_CONSUMER_REQUEST_getConsumerRequest as ConsumerRequest } from './workbench/queries/__generated__/GET_CONSUMER_REQUEST'; | |
type Props = {| | |
consumerRequest?: ConsumerRequest, | |
dispatch: Dispatch<any>, | |
job?: Job, | |
|}; | |
const Actions = ({ consumerRequest, dispatch, job }: Props) => { | |
const client = useApolloClient(); | |
let actions = []; | |
if (consumerRequest) { | |
actions = consumerRequestActions.get(consumerRequest.status) || []; | |
} else if (job) { | |
actions = jobActions.get(job.status) || []; | |
} | |
return ( | |
<div> | |
{actions.map(({ action, label }) => ( | |
<button | |
onClick={() => { | |
if (consumerRequest) { | |
action({ client, dispatch, data: consumerRequest }); | |
} else if (job) { | |
action({ client, dispatch, data: job }); | |
} | |
}} | |
key={label} | |
type="button" | |
> | |
{label} | |
</button> | |
))} | |
</div> | |
); | |
}; | |
Actions.propTypes = { | |
consumerRequest: PropTypes.object, | |
dispatch: PropTypes.func.isRequired, | |
job: PropTypes.object, | |
}; | |
Actions.defaultProps = { | |
consumerRequest: undefined, | |
job: undefined, | |
}; | |
export default connect()(Actions); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
import type ApolloClient from 'apollo-client'; | |
import type { Dispatch } from 'redux'; | |
import type { GET_JOB_getJob as Job } from './components/workbench/queries/__generated__/GET_JOB'; | |
import type { GET_CONSUMER_REQUEST_getConsumerRequest as ConsumerRequest } from './components/workbench/queries/__generated__/GET_CONSUMER_REQUEST'; | |
export type Action = {| | |
action: ({ | |
client: ApolloClient, | |
dispatch: Dispatch<any>, | |
data: Job | ConsumerRequest, | |
}) => any, | |
label: string, | |
|}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
import { CONSUMER_REQUEST_STATUS } from '../constants/consumerRequestStatuses'; | |
import type { Action } from './common'; | |
const consumerRequestActions: Map<number, Action[]> = new Map([ | |
[ | |
CONSUMER_REQUEST_STATUS.pending_approval, | |
[ | |
{ | |
action: () => {}, | |
label: 'Test', | |
}, | |
], | |
], | |
]); | |
export default consumerRequestActions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ flow | |
import { JOB_STATUS } from '../constants/jobStatuses'; | |
import { | |
requestSuspendJob, | |
updateJobStatus, | |
} from '../redux/actions/jobActions'; | |
import type { Action } from './common'; | |
const jobActions: Map<number, Action[]> = new Map([ | |
[ | |
JOB_STATUS.provider_paused, | |
[ | |
{ | |
action: ({ dispatch, client, data }) => | |
dispatch( | |
updateJobStatus( | |
data.id, | |
client, | |
JOB_STATUS.provider_resume_requested, | |
), | |
), | |
label: 'Resume', | |
}, | |
{ | |
action: ({ dispatch, data }) => dispatch(requestSuspendJob(data.id)), | |
label: 'Suspend', | |
}, | |
{ | |
action: ({ dispatch, client, data }) => | |
dispatch( | |
updateJobStatus(data.id, client, JOB_STATUS.provider_completed), | |
), | |
label: 'Complete', | |
}, | |
], | |
], | |
]); | |
export default jobActions; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment