diff --git a/frontend/common/dispatcher/action-constants.js b/frontend/common/dispatcher/action-constants.js index 87ef249fad24..393937f65cb0 100644 --- a/frontend/common/dispatcher/action-constants.js +++ b/frontend/common/dispatcher/action-constants.js @@ -32,7 +32,6 @@ const Actions = Object.assign({}, require('./base/_action-constants'), { 'GET_FEATURE_USAGE': 'GET_FEATURE_USAGE', 'GET_FLAGS': 'GET_FLAGS', 'GET_IDENTITY': 'GET_IDENTITY', - 'GET_IDENTITY_SEGMENTS': 'GET_IDENTITY_SEGMENTS', 'GET_ORGANISATION': 'GET_ORGANISATION', 'GET_PROJECT': 'GET_PROJECT', 'INVALIDATE_INVITE_LINK': 'INVALIDATE_INVITE_LINK', diff --git a/frontend/common/dispatcher/app-actions.js b/frontend/common/dispatcher/app-actions.js index 0eab0e1d1245..d972581e07de 100644 --- a/frontend/common/dispatcher/app-actions.js +++ b/frontend/common/dispatcher/app-actions.js @@ -260,13 +260,6 @@ const AppActions = Object.assign({}, require('./base/_app-actions'), { id, }) }, - getIdentitySegments(projectId, id) { - Dispatcher.handleViewAction({ - actionType: Actions.GET_IDENTITY_SEGMENTS, - id, - projectId, - }) - }, getOrganisation(organisationId) { Dispatcher.handleViewAction({ actionType: Actions.GET_ORGANISATION, diff --git a/frontend/common/providers/IdentitySegmentsProvider.js b/frontend/common/providers/IdentitySegmentsProvider.js deleted file mode 100644 index 97dd67786403..000000000000 --- a/frontend/common/providers/IdentitySegmentsProvider.js +++ /dev/null @@ -1,42 +0,0 @@ -import React from 'react' -import IdentitySegmentsStore from 'common/stores/identity-segments-store' - -const IdentitySegmentsProvider = class extends React.Component { - static displayName = 'IdentitySegmentsProvider' - - constructor(props, context) { - super(props, context) - this.state = { - isLoading: IdentitySegmentsStore.isLoading, - segments: IdentitySegmentsStore.model[props.id], - segmentsPaging: IdentitySegmentsStore.paging, - } - ES6Component(this) - } - - componentDidMount() { - if (this.props.fetch) { - AppActions.getIdentitySegments(this.props.projectId, this.props.id) - } - this.listenTo(IdentitySegmentsStore, 'change', () => { - this.setState({ - isLoading: IdentitySegmentsStore.isLoading, - segments: IdentitySegmentsStore.model[this.props.id], - segmentsPaging: IdentitySegmentsStore.paging, - }) - }) - } - - render() { - return this.props.children({ ...this.state }, {}) - } -} - -IdentitySegmentsProvider.propTypes = { - children: OptionalFunc, - fetch: OptionalBool, - id: RequiredString, - projectId: RequiredString, -} - -export default IdentitySegmentsProvider diff --git a/frontend/common/services/useIdentitySegment.ts b/frontend/common/services/useIdentitySegment.ts new file mode 100644 index 000000000000..01a2c927d1e8 --- /dev/null +++ b/frontend/common/services/useIdentitySegment.ts @@ -0,0 +1,59 @@ +import { Res } from 'common/types/responses' +import { Req } from 'common/types/requests' +import { service } from 'common/service' +import Utils from 'common/utils/utils' +import transformCorePaging from 'common/transformCorePaging' +import { sortBy } from 'lodash' + +export const identitySegmentService = service + .enhanceEndpoints({ addTagTypes: ['IdentitySegment'] }) + .injectEndpoints({ + endpoints: (builder) => ({ + getIdentitySegments: builder.query< + Res['identitySegments'], + Req['getIdentitySegments'] + >({ + providesTags: (res) => [{ id: res?.id, type: 'IdentitySegment' }], + query: ({ projectId, ...query }: Req['getIdentitySegments']) => ({ + url: `/projects/${projectId}/segments/?${Utils.toParam(query)}`, + }), + transformResponse: ( + res: Res['identitySegments'], + _, + req: Req['getIdentitySegments'], + ) => + transformCorePaging(req, { + ...res, + results: sortBy(res.results, 'name'), + }), + }), + // END OF ENDPOINTS + }), + }) + +export async function getIdentitySegments( + store: any, + data: Req['getIdentitySegments'], + options?: Parameters< + typeof identitySegmentService.endpoints.getIdentitySegments.initiate + >[1], +) { + return store.dispatch( + identitySegmentService.endpoints.getIdentitySegments.initiate( + data, + options, + ), + ) +} +// END OF FUNCTION_EXPORTS + +export const { + useGetIdentitySegmentsQuery, + // END OF EXPORTS +} = identitySegmentService + +/* Usage examples: +const { data, isLoading } = useGetIdentitySegmentsQuery({ id: 2 }, {}) //get hook +const [createIdentitySegments, { isLoading, data, isSuccess }] = useCreateIdentitySegmentsMutation() //create hook +identitySegmentService.endpoints.getIdentitySegments.select({id: 2})(store.getState()) //access data from any function +*/ diff --git a/frontend/common/stores/identity-segments-store.js b/frontend/common/stores/identity-segments-store.js deleted file mode 100644 index 79fe7dbf3739..000000000000 --- a/frontend/common/stores/identity-segments-store.js +++ /dev/null @@ -1,46 +0,0 @@ -const Dispatcher = require('../dispatcher/dispatcher') -const BaseStore = require('./base/_store') -const data = require('../data/base/_data') - -const controller = { - getIdentitySegments: (projectId, id, page) => { - store.loading() - const endpoint = - page || `${Project.api}projects/${projectId}/segments/?identity=${id}` - return data - .get(endpoint) - .then((res) => { - store.model[id] = res.results && _.sortBy(res.results, (r) => r.name) - store.paging.next = res.next - store.paging.count = res.count - store.paging.previous = res.previous - store.paging.currentPage = - endpoint.indexOf('?page=') !== -1 - ? parseInt(endpoint.substr(endpoint.indexOf('?page=') + 6)) - : 1 - store.loaded() - }) - .catch((e) => API.ajaxHandler(store, e)) - }, -} - -const store = Object.assign({}, BaseStore, { - id: 'identity-segments', - model: {}, - paging: {}, -}) - -store.dispatcherIndex = Dispatcher.register(store, (payload) => { - const action = payload.action // this is our action from handleViewAction - switch (action.actionType) { - case Actions.GET_IDENTITY_SEGMENTS: - controller.getIdentitySegments(action.projectId, action.id) - break - case Actions.GET_IDENTITY_SEGMENTS_PAGE: - controller.getIdentitySegments(null, null, action.page) - break - default: - } -}) -controller.store = store -module.exports = controller.store diff --git a/frontend/common/types/requests.ts b/frontend/common/types/requests.ts index 4857e8454ef1..738f96d571be 100644 --- a/frontend/common/types/requests.ts +++ b/frontend/common/types/requests.ts @@ -568,5 +568,10 @@ export type Req = { getAuditLogWebhooks: { organisationId: string } updateAuditLogWebhooks: { organisationId: string; data: Webhook } deleteAuditLogWebhook: { organisationId: string; id: number } + getIdentitySegments: PagedRequest<{ + q?: string + identity: string + projectId: string + }> // END OF TYPES } diff --git a/frontend/common/types/responses.ts b/frontend/common/types/responses.ts index 911784354b5a..851872f3684c 100644 --- a/frontend/common/types/responses.ts +++ b/frontend/common/types/responses.ts @@ -795,6 +795,7 @@ export type Res = { metadata_xml: string } samlAttributeMapping: PagedResponse + identitySegments: PagedResponse organisationWebhooks: PagedResponse // END OF TYPES } diff --git a/frontend/global.d.ts b/frontend/global.d.ts index ff244a743c6c..90d076c54381 100644 --- a/frontend/global.d.ts +++ b/frontend/global.d.ts @@ -27,7 +27,12 @@ declare global { ) => void const openConfirm: (data: OpenConfirm) => void const Row: typeof Component - const toast: (value: ReactNode, theme?: string, expiry?: number, action?: { buttonText: string; onClick: () => void }) => void + const toast: ( + value: ReactNode, + theme?: string, + expiry?: number, + action?: { buttonText: string; onClick: () => void }, + ) => void const Flex: typeof Component const isMobile: boolean const FormGroup: typeof Component diff --git a/frontend/web/components/EnvironmentSelect.tsx b/frontend/web/components/EnvironmentSelect.tsx index 38cd03ec6b06..4075b8617bcc 100644 --- a/frontend/web/components/EnvironmentSelect.tsx +++ b/frontend/web/components/EnvironmentSelect.tsx @@ -2,7 +2,7 @@ import React, { FC, useMemo } from 'react' import { useGetEnvironmentsQuery } from 'common/services/useEnvironment' import { Props } from 'react-select/lib/Select' -export type EnvironmentSelectType = Partial & { +export type EnvironmentSelectType = Partial> & { projectId: string value?: string label?: string diff --git a/frontend/web/components/modals/AuditLogWebhooks.tsx b/frontend/web/components/modals/AuditLogWebhooks.tsx index 90e27f02244f..13ef9f452e65 100644 --- a/frontend/web/components/modals/AuditLogWebhooks.tsx +++ b/frontend/web/components/modals/AuditLogWebhooks.tsx @@ -69,7 +69,8 @@ const AuditLogWebhooks: FC = ({ organisationId }) => {
Audit Webhooks

Audit webhooks let you know when audit logs occur. You can configure - 1 or more audit webhooks per organisation.
+ 1 or more audit webhooks per organisation. +