Skip to content

Commit

Permalink
Identity traits
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-ssg committed Feb 19, 2025
1 parent 23e3be4 commit 65bad13
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 31 deletions.
34 changes: 33 additions & 1 deletion frontend/common/services/useIdentityTrait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { IdentityTrait, Res } from 'common/types/responses'
import { Req } from 'common/types/requests'
import { service } from 'common/service'
import Utils from 'common/utils/utils'
import { getStore } from 'common/store'
import { identitySegmentService } from './useIdentitySegment'
import q from 'refractor/lang/q'

function getTraitEndpoint(
use_edge_identities: boolean,
Expand Down Expand Up @@ -66,6 +69,12 @@ export const identityTraitService = service
),
}
},
transformResponse: (res) => {
getStore().dispatch(
identitySegmentService.util.invalidateTags(['IdentitySegment']),
)
return res
},
}),
deleteIdentityTrait: builder.mutation<
Res['identityTrait'],
Expand Down Expand Up @@ -94,9 +103,16 @@ export const identityTraitService = service
query.use_edge_identities,
query.environmentId,
query.identity,
query.data.id,
),
}
},
transformResponse: (res) => {
getStore().dispatch(
identitySegmentService.util.invalidateTags(['IdentitySegment']),
)
return res
},
}),
getIdentityTraits: builder.query<
Res['identityTraits'],
Expand All @@ -110,6 +126,16 @@ export const identityTraitService = service
query.identity,
),
}),
transformResponse: (res) => {
// Core API returns paginated were as edge doesn't
if (res?.results) {
return res?.results.map((v) => ({
...v,
trait_value: Utils.featureStateToValue(v),
}))
}
return res
},
}),
updateIdentityTrait: builder.mutation<
Res['identityTrait'],
Expand All @@ -126,7 +152,7 @@ export const identityTraitService = service
query.identity,
query.data,
),
method: query.use_edge_identities ? 'PUT' : 'POST',
method: 'PUT',
url: getUpdateTraitEndpoint(
query.use_edge_identities,
query.environmentId,
Expand All @@ -135,6 +161,12 @@ export const identityTraitService = service
),
}
},
transformResponse: (res) => {
getStore().dispatch(
identitySegmentService.util.invalidateTags(['IdentitySegment']),
)
return res
},
}),
// END OF ENDPOINTS
}),
Expand Down
3 changes: 0 additions & 3 deletions frontend/web/components/IdentityTraits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Panel from './base/grid/Panel'
import { useHasPermission } from 'common/providers/Permission'
import API from 'project/api'
import CreateTraitModal from './modals/CreateTrait'
import AppActions from 'common/dispatcher/app-actions'
import {
useDeleteIdentityTraitMutation,
useGetIdentityTraitsQuery,
Expand Down Expand Up @@ -46,10 +45,8 @@ const IdentityTraits: FC<IdentityTraitsType> = ({
const [deleteTrait, { isLoading: deletingTrait }] =
useDeleteIdentityTraitMutation({})

//todo : https://github.com/Flagsmith/flagsmith/pull/5121 migrates this
const onTraitSaved = () => {
closeModal?.()
AppActions.getIdentitySegments(projectId, identityId)
}

const createTrait = () => {
Expand Down
28 changes: 7 additions & 21 deletions frontend/web/components/modals/CreateSegment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const CreateSegment: FC<CreateSegmentType> = ({
project?: number
} = {
description: '',
metadata: [],
name: '',
rules: [
{
Expand Down Expand Up @@ -157,9 +158,7 @@ const CreateSegment: FC<CreateSegmentType> = ({
const isSaving = creating || updating
const [showDescriptions, setShowDescriptions] = useState(false)
const [tab, setTab] = useState(UserTabs.RULES)
const [metadata, setMetadata] = useState<CustomMetadataField[]>(
segment.metadata,
)
const [metadata, setMetadata] = useState<Metadata[]>(segment.metadata)
const metadataEnable = Utils.getPlansPermission('METADATA')
const error = createError || updateError
const totalSegments = ProjectStore.getTotalSegments() ?? 0
Expand Down Expand Up @@ -240,13 +239,6 @@ const CreateSegment: FC<CreateSegmentType> = ({
}
}

const fetchUserIdentityList = () => {
if (!environmentId) return
identities?.results.forEach((identity) =>
AppActions.getIdentitySegments(projectId, identity.id),
)
}

const [valueChanged, setValueChanged] = useState(false)
const [metadataValueChanged, setMetadataValueChanged] = useState(false)
const onClosing = useCallback(() => {
Expand Down Expand Up @@ -288,15 +280,13 @@ const CreateSegment: FC<CreateSegmentType> = ({
if (createSuccess && createSegmentData) {
setSegment(createSegmentData)
onComplete?.(createSegmentData)
fetchUserIdentityList()
}
//eslint-disable-next-line
}, [createSuccess])
useEffect(() => {
if (updateSuccess && updateSegmentData) {
setSegment(updateSegmentData)
onComplete?.(updateSegmentData)
fetchUserIdentityList()
}
//eslint-disable-next-line
}, [updateSuccess])
Expand Down Expand Up @@ -366,11 +356,7 @@ const CreateSegment: FC<CreateSegmentType> = ({
readOnly={readOnly}
data-test={`rule-${displayIndex}`}
rule={rule}
operators={operators}
onRemove={() => {
setValueChanged(true)
removeRule(0, i)
}}
operators={operators!}
onChange={(v: SegmentRule) => {
setValueChanged(true)
updateRule(0, i, v)
Expand Down Expand Up @@ -418,8 +404,8 @@ const CreateSegment: FC<CreateSegmentType> = ({
entityId={`${segment.id}` || ''}
entityContentType={segmentContentType?.id}
entity={segmentContentType?.model}
onChange={(m: CustomMetadataField[]) => {
setMetadata(m)
onChange={(m) => {
setMetadata(m as Metadata[])
if (isEdit) {
setMetadataValueChanged(true)
}
Expand Down Expand Up @@ -488,7 +474,7 @@ const CreateSegment: FC<CreateSegmentType> = ({
environmentId={environmentId}
setEnvironmentId={setEnvironmentId}
identitiesLoading={identitiesLoading}
identities={identities}
identities={identities!}
page={page}
setPage={setPage}
name={name}
Expand Down Expand Up @@ -638,7 +624,7 @@ const LoadingCreateSegment: FC<LoadingCreateSegmentType> = (props) => {
pageType: page.pageType,
page_size: 10,
pages: page.pages,
search,
q: search,
},
{
skip: !environmentId,
Expand Down
4 changes: 2 additions & 2 deletions frontend/web/components/modals/CreateTrait.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import { FlagsmithValue } from 'common/types/responses'

type CreateTraitProps = {
id?: number | string
id?: number
trait_key?: string
trait_value?: FlagsmithValue
identityName: string
Expand Down Expand Up @@ -83,7 +83,7 @@ const CreateTrait: FC<CreateTraitProps> = ({
const handleSave = () => {
if (isEdit) {
updateTrait({
data: { id: identity, trait_key: traitKey, trait_value: traitValue },
data: { id, trait_key: traitKey, trait_value: traitValue },
environmentId,
identity,
use_edge_identities,
Expand Down
6 changes: 2 additions & 4 deletions frontend/web/components/pages/UserPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ const UserPage: FC<UserPageType> = (props) => {
environmentId={environmentId}
projectId={projectId}
identityId={id}
identityName={identity}
identityName={identity?.identity?.identifier || id}
/>
)}{' '}
{!segments?.results ? (
Expand Down Expand Up @@ -1036,9 +1036,7 @@ const UserPage: FC<UserPageType> = (props) => {
<TryIt
title='Check to see what features and traits are coming back for this user'
environmentId={environmentId}
userId={
(identity && identity.identity.identifier) || id
}
userId={identity?.identity?.identifier || id}
/>
</FormGroup>
</div>
Expand Down

0 comments on commit 65bad13

Please sign in to comment.