Skip to content

Commit

Permalink
react/kiezradar: add search profile fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sevfurneaux authored and hom3mad3 committed Jan 30, 2025
1 parent 03bbe43 commit 0fb8fbe
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 deletions.
7 changes: 6 additions & 1 deletion meinberlin/react/kiezradar/SearchProfile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const cancelText = django.gettext('Cancel')
const saveText = django.gettext('Save')
const savingText = django.gettext('Saving')
const viewProjectsText = django.gettext('View projects')
const plansText = django.gettext('Plans')
const errorText = django.gettext('Error')
const errorDeleteSearchProfilesText = django.gettext(
'Failed to delete search profile'
Expand Down Expand Up @@ -78,7 +79,11 @@ export default function SearchProfile ({ apiUrl, planListUrl, profile: profile_,
]
.map((filter) => filter.map(({ name }) => name))

const selection = [[profile.query_text], ...filters]
const selection = [
[profile.query_text],
...filters,
[profile.plans_only ? plansText : null]
]
.map((names) => names.join(', '))
.filter(Boolean)

Expand Down
40 changes: 39 additions & 1 deletion meinberlin/react/kiezradar/use-create-search-profile.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('useCreateSearchProfile', () => {
topics: ['ANT'],
participations: [0, 1, 2],
projectState: ['active', 'past', 'future'],
plansOnly: false,
search: ''
}

Expand All @@ -59,7 +60,9 @@ describe('useCreateSearchProfile', () => {
organisations: [1],
topics: [1],
project_types: [1, 2, 3],
status: [1, 2, 3]
status: [1, 2, 3],
plans_only: false,
notification: true
}

const { result } = renderHook(() =>
Expand All @@ -71,6 +74,7 @@ describe('useCreateSearchProfile', () => {
topicChoices,
participationChoices,
projectStatus,
searchProfilesCount: 0,
onSearchProfileCreate: () => {}
})
)
Expand All @@ -93,6 +97,7 @@ describe('useCreateSearchProfile', () => {
topics: [],
participations: [],
projectState: [],
plansOnly: false,
search: ''
}

Expand Down Expand Up @@ -128,6 +133,7 @@ describe('useCreateSearchProfile', () => {
topicChoices,
participationChoices,
projectStatus,
searchProfilesCount: 0,
onSearchProfileCreate: mockOnSearchProfileCreate
})
)
Expand All @@ -138,4 +144,36 @@ describe('useCreateSearchProfile', () => {

expect(mockOnSearchProfileCreate).toHaveBeenCalledWith(mockedSearchProfile)
})

it('sets limitExceeded to true when searchProfilesCount is 10', async () => {
const appliedFilters = {
districts: [],
organisation: [],
topics: [],
participations: [],
projectState: [],
plansOnly: false,
search: ''
}

const { result } = renderHook(() =>
useCreateSearchProfile({
searchProfilesApiUrl,
appliedFilters,
districts,
organisations,
topicChoices,
participationChoices,
projectStatus,
searchProfilesCount: 10,
onSearchProfileCreate: () => {}
})
)

await act(async () => {
await result.current.createSearchProfile()
})

expect(result.current.limitExceeded).toBe(true)
})
})
13 changes: 11 additions & 2 deletions meinberlin/react/kiezradar/use-create-search-profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ export function useCreateSearchProfile ({
topicChoices,
participationChoices,
projectStatus,
searchProfilesCount,
onSearchProfileCreate
}) {
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
const [limitExceeded, setLimitExceeded] = useState(false)

const createSearchProfile = async () => {
if (searchProfilesCount === 10) {
setLimitExceeded(true)
return
}

setLoading(true)
setError(null)

Expand All @@ -51,7 +58,9 @@ export function useCreateSearchProfile ({
organisations: organisationIds,
topics: topicIds,
project_types: participationIds,
status: projectStatusIds
status: projectStatusIds,
plans_only: appliedFilters.plansOnly,
notification: true
}

if (appliedFilters.search.length > 0) {
Expand All @@ -70,7 +79,7 @@ export function useCreateSearchProfile ({
}
}

return { loading, error, createSearchProfile }
return { loading, limitExceeded, error, createSearchProfile }
}

function getFilteredResults ({
Expand Down
14 changes: 8 additions & 6 deletions meinberlin/react/plans/SaveSearchProfile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ export default function SaveSearchProfile ({
)
}

if (searchProfilesCount > 10) {
return <span className="save-search-profile__action">{limitText}</span>
}

if (searchProfile) {
return (
<a className="save-search-profile__action save-search-profile__action--link" href="/account/search-profiles">
Expand All @@ -42,7 +38,7 @@ export default function SaveSearchProfile ({
)
}

return <CreateSearchProfileButton {...props} />
return <CreateSearchProfileButton {...props} searchProfilesCount={searchProfilesCount} />
}

function CreateSearchProfileButton ({
Expand All @@ -53,19 +49,25 @@ function CreateSearchProfileButton ({
projectStatus,
searchProfilesApiUrl,
appliedFilters,
searchProfilesCount,
onSearchProfileCreate
}) {
const { loading, error, createSearchProfile } = useCreateSearchProfile({
const { loading, limitExceeded, error, createSearchProfile } = useCreateSearchProfile({
searchProfilesApiUrl,
appliedFilters,
districts,
organisations,
topicChoices,
participationChoices,
projectStatus,
searchProfilesCount,
onSearchProfileCreate
})

if (limitExceeded) {
return <span className="save-search-profile__action">{limitText}</span>
}

if (error) {
return <span className="save-search-profile__error">{error}</span>
}
Expand Down
3 changes: 2 additions & 1 deletion meinberlin/react/projects/getDefaultState.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const getDefaultState = (searchProfile) => {
districts: searchProfile.districts.map(d => d.name),
organisation: searchProfile.organisations.map(o => o.name),
participations: searchProfile.project_types.map(p => p.id),
topics: searchProfile.topics.map((t) => t.code)
topics: searchProfile.topics.map((t) => t.code),
plansOnly: searchProfile.plans_only
}
}

Expand Down

0 comments on commit 0fb8fbe

Please sign in to comment.