Created
April 27, 2020 21:13
-
-
Save thomir/6e817f09a5dba19120c75fb1a28c16aa 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 _ from 'lodash'; | |
import { createSelector, defaultMemoize } from 'reselect'; | |
import * as Account from 'app/entities/Account'; | |
import * as Zap from 'app/entities/Zap'; | |
import nodes from 'app/contact-us/content/nodes'; | |
import { MAX_SERVICE_LINKS_TO_DISPLAY } from 'app/contact-us/constants'; | |
import { getIncidents } from 'app/beta-editor/Selectors'; | |
import { isHidden } from 'app/service/ServiceUtils'; | |
import { selectNodesInPath } from 'app/contact-us/utils/tree'; | |
import type { Incident as AppIncident } from 'app/beta-editor/flowState'; | |
import type { DataSet, SelectedNode } from 'app/contact-us/utils/tree/types'; | |
import type { ServicesMap } from 'app/service/types'; | |
import type { State } from 'app/common/types'; | |
import { type Incident } from 'app/contact-us/components/common/Incidents'; | |
export const selectCurrentAccountZaps = createSelector( | |
state => state, | |
state => Account.selectors.all.currentAccountId(state), | |
(state, currentAccountId) => | |
Zap.selectors.all.inAccount(currentAccountId, state) | |
); | |
export const selectSelectedZaps = createSelector( | |
selectCurrentAccountZaps, | |
zaps => zaps.filter(Zap.selectors.isSelectedForMoving) | |
); | |
export const selectSelectedZapIds = createSelector(selectSelectedZaps, zaps => | |
zaps.map(Zap.selectors.id) | |
); | |
export const selectZapsFromIds = ( | |
state: State, | |
props: { zapIds: Array<number> } | |
) => { | |
return props.zapIds.map(id => Zap.selectors.all.entity(id, state)); | |
}; | |
export const selectSelectedApisFromZapIds = createSelector( | |
selectZapsFromIds, | |
(zaps): Array<string> => { | |
return _.uniq( | |
_.flatten(zaps.map(zap => Zap.selectors.nodeApis(zap))) | |
).filter(selected_api => selected_api !== 'unknown'); | |
} | |
); | |
export const selectServices = ( | |
state: State, | |
props: { services: ServicesMap } | |
) => { | |
return props.services || {}; | |
}; | |
export const selectSelectedServices = createSelector( | |
[selectSelectedApisFromZapIds, selectServices], | |
(selectedApis, services) => { | |
if ( | |
// We don't have service data yet. | |
!Object.keys(services).length || | |
// User hasn't selected any Zaps with selectedApis yet. | |
!selectedApis || | |
!selectedApis.length | |
) { | |
return []; | |
} | |
return selectedApis | |
.reduce((selectedServices, selectedApi) => { | |
const service = _.get(services, [selectedApi], {}); | |
const slug = _.get(service, ['slug']); | |
const name = _.get(service, ['name']); | |
if (slug && name) { | |
selectedServices.push({ | |
name, | |
selectedApi, | |
slug, | |
}); | |
} | |
return selectedServices; | |
}, []) | |
.slice(0, MAX_SERVICE_LINKS_TO_DISPLAY); | |
} | |
); | |
// TODO: consolidate with selectSelectedApisFromZapIds | |
export const selectSelectedZapApis = createSelector( | |
selectSelectedZaps, | |
zaps => { | |
return _.uniq( | |
_.flatten(zaps.map(zap => Zap.selectors.nodeApis(zap))) | |
).filter(selected_api => selected_api !== 'unknown'); | |
} | |
); | |
export const selectSelectedZapIncidents = createSelector( | |
getIncidents, | |
selectSelectedZapApis, | |
(_state, services?: ServicesMap): ?ServicesMap => services, | |
(incidents, selectedApis, services?: ?ServicesMap): Incident[] => { | |
if ( | |
incidents.length === 0 || | |
selectedApis.length === 0 || | |
!services || | |
Object.keys(services).length === 0 | |
) { | |
return []; | |
} | |
return incidents | |
.filter( | |
({ service: { key } }: AppIncident): boolean => | |
selectedApis.indexOf(key) !== -1 | |
) | |
.map( | |
({ | |
name, | |
status, | |
message, | |
updated_at, | |
statuspageio_id, | |
service: { key }, | |
}: AppIncident): Incident => ({ | |
name, | |
status, | |
body: message, | |
updated_at, | |
shortlink: | |
statuspageio_id && | |
`https://status.zapier.com/incidents/${statuspageio_id}`, | |
selected_api: key, | |
}) | |
); | |
} | |
); | |
export const selectVisibleServicesOrdered = createSelector( | |
// not using getVisibleServices because that wants a map :( | |
services => services.filter(service => !isHidden(service)), | |
services => | |
services.sort((a, b) => { | |
if (a.isPublic === b.isPublic) { | |
return (a.name || a.key || '').localeCompare(b.name || b.key || ''); | |
} | |
return a.isPublic ? 1 : -1; | |
}) | |
); | |
export const selectNodes = (defaultMemoize((meta, dataSet, maxDepth = 0) => | |
selectNodesInPath(nodes, meta, dataSet, maxDepth) | |
): (meta: DataSet, dataSet: DataSet, maxDepth?: number) => SelectedNode[]); | |
export const selectAttemptId = (state: State): ?number => | |
_.get(state, ['contact', 'attemptId']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment