-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add search for OpenGov * chore: typo * fix: review
- Loading branch information
1 parent
764c0e0
commit f600eeb
Showing
7 changed files
with
130 additions
and
14 deletions.
There are no files selected for viewing
11 changes: 8 additions & 3 deletions
11
src/renderer/features/governance/ReferendumFilter/model/referendum-filter-model.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import { createStore } from 'effector'; | ||
import { createEvent, restore } from 'effector'; | ||
|
||
const $referendums = createStore<any[]>([]); | ||
const queryChanged = createEvent<string>(); | ||
|
||
const $query = restore<string>(queryChanged, ''); | ||
|
||
export const referendumFilterModel = { | ||
referendums: $referendums, | ||
$query, | ||
events: { | ||
queryChanged, | ||
}, | ||
}; |
17 changes: 15 additions & 2 deletions
17
src/renderer/features/governance/ReferendumFilter/ui/ReferendumFilter.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
import { useUnit } from 'effector-react'; | ||
|
||
import { useI18n } from '@app/providers'; | ||
import { SearchInput } from '@shared/ui'; | ||
import { referendumFilterModel } from '../model/referendum-filter-model'; | ||
|
||
export const ReferendumFilter = () => { | ||
const { t } = useI18n(); | ||
const query = useUnit(referendumFilterModel.$query); | ||
|
||
return ( | ||
// eslint-disable-next-line i18next/no-literal-string | ||
<div>Referendum filter</div> | ||
<SearchInput | ||
value={query} | ||
placeholder={t('governance.searchPlaceholder')} | ||
wrapperClass="w-[230px]" | ||
onChange={referendumFilterModel.events.queryChanged} | ||
/> | ||
); | ||
}; |
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
42 changes: 42 additions & 0 deletions
42
src/renderer/pages/Governance/lib/__tests__/governance-page-utils.test.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ChainId, ReferendumId } from '@shared/core'; | ||
import { filterReferendums } from '../utils'; | ||
|
||
describe('page/governance/lib/governance-page-utils', () => { | ||
const chainId = '0x1'; | ||
const referendums = new Map([ | ||
['111', {}], | ||
['222', {}], | ||
]); | ||
const details: Record<ChainId, Record<ReferendumId, string>> = { | ||
'0x1': { | ||
111: 'Referendum Title 1', | ||
222: 'Referendum Title 2', | ||
}, | ||
}; | ||
|
||
test('should return all referendums if query is empty', () => { | ||
const query = ''; | ||
const result = filterReferendums({ referendums, query, details, chainId }); | ||
expect(result).toEqual(referendums); | ||
}); | ||
|
||
test('should return referendums that match the query in the ID', () => { | ||
const query = '111'; | ||
const result = filterReferendums({ referendums, query, details, chainId }); | ||
expect(result.size).toEqual(1); | ||
expect(result.has('111')).toEqual(true); | ||
}); | ||
|
||
test('should return referendums that match the query in the title', () => { | ||
const query = 'Title 2'; | ||
const result = filterReferendums({ referendums, query, details, chainId }); | ||
expect(result.size).toEqual(1); | ||
expect(result.has('222')).toEqual(true); | ||
}); | ||
|
||
test('should return an empty map if no referendums match the query', () => { | ||
const query = 'none'; | ||
const result = filterReferendums({ referendums, query, details, chainId }); | ||
expect(result.size).toEqual(0); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ChainId, ReferendumId } from '@shared/core'; | ||
import { includes } from '@shared/lib/utils'; | ||
|
||
type ReferendumMap<T> = Map<ReferendumId, T>; | ||
type Props<T> = { | ||
referendums: ReferendumMap<T>; | ||
query: string; | ||
details: Record<ChainId, Record<ReferendumId, string>>; | ||
chainId: ChainId; | ||
}; | ||
|
||
export function filterReferendums<T>({ referendums, query, details, chainId }: Props<T>): ReferendumMap<T> { | ||
if (!query || referendums.size === 0) return referendums; | ||
|
||
const filteredReferendums = Array.from(referendums.entries()).filter(([key]) => { | ||
const title = details[chainId]?.[key]; | ||
const hasIndex = includes(key, query); | ||
const hasTitle = includes(title, query); | ||
|
||
return hasIndex || hasTitle; | ||
}); | ||
|
||
return new Map(filteredReferendums); | ||
} |
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
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