From d5ef5f8a1a4d89748b8449059f8a9f07ff2a6e8e Mon Sep 17 00:00:00 2001 From: Vitaly Baev Date: Sun, 29 Dec 2024 16:25:46 +0100 Subject: [PATCH] feat(party): kazakhstan parties suggestions --- packages/example/src/App.tsx | 29 ++++++- packages/react-dadata/readme.md | 22 ++++- packages/react-dadata/src/index.tsx | 12 +++ .../party-kazakhstan-types.ts | 48 +++++++++++ .../party_kazakhstan/party-kazakhstan.tsx | 82 +++++++++++++++++++ readme.md | 22 ++++- 6 files changed, 209 insertions(+), 6 deletions(-) create mode 100644 packages/react-dadata/src/variants/party_kazakhstan/party-kazakhstan-types.ts create mode 100644 packages/react-dadata/src/variants/party_kazakhstan/party-kazakhstan.tsx diff --git a/packages/example/src/App.tsx b/packages/example/src/App.tsx index 48f9346..b762371 100644 --- a/packages/example/src/App.tsx +++ b/packages/example/src/App.tsx @@ -1,7 +1,12 @@ import React, { useState } from 'react'; import './App.css'; import 'react-dadata/src/react-dadata.css'; -import { AddressSuggestions, PartyBelarusSuggestions, PartySuggestions } from '../../react-dadata/src'; +import { + AddressSuggestions, + PartyBelarusSuggestions, + PartyKazakhstanSuggestions, + PartySuggestions, +} from '../../react-dadata/src'; const DADATA_TOKEN = '3c2e964517d7358776e07d7d699cc2b0626dac54'; @@ -10,7 +15,9 @@ function App() { return
Пожалуйста, установите ваш API токен для DaData в `example/src/App.tsx:5`
; } - const [suggestionsType, setSuggestionsType] = useState<'address' | 'party_russia' | 'party_belarus'>('address'); + const [suggestionsType, setSuggestionsType] = useState< + 'address' | 'party_russia' | 'party_belarus' | 'party_kazakhstan' + >('address'); return (
@@ -48,6 +55,17 @@ function App() { />
+
+ setSuggestionsType('party_kazakhstan')} + /> + +
{suggestionsType === 'address' && ( @@ -66,6 +84,13 @@ function App() { selectOnBlur /> )} + {suggestionsType === 'party_kazakhstan' && ( + + )} ); } diff --git a/packages/react-dadata/readme.md b/packages/react-dadata/readme.md index e02b896..d1074d6 100644 --- a/packages/react-dadata/readme.md +++ b/packages/react-dadata/readme.md @@ -177,7 +177,7 @@ const [value, setValue] = useState(); ; ``` -#### Дополнительные параметры для компонента организаций +#### Дополнительные параметры для компонента организаций в России | Свойство | Обязательный | Тип | Описание | |----------------------|--------------|-----------|----------------------------------------------------------------| @@ -198,13 +198,31 @@ const [value, setValue] = useState(); ; ``` -#### Дополнительные параметры для компонента организаций +#### Дополнительные параметры для компонента организаций в Беларуси | Свойство | Обязательный | Тип | Описание | |----------------------|--------------|-----------|----------------------------------------------------------------| | filterStatus | Нет | `DaDataPartyBelarusStatus[]` | Фильтр по статусу организации, параметр status в запросе | | filterType | Нет | `DaDataPartyType[]` | Фильтр по типу организации, параметр type в запросе | +### Организации в Казахстане 🇰🇿 + +```jsx +import { PartyKazakhstanSuggestions } from 'react-dadata'; +import 'react-dadata/dist/react-dadata.css'; + +const [value, setValue] = useState(); + +; +``` + +#### Дополнительные параметры для компонента организаций в Казахстане + +| Свойство | Обязательный | Тип | Описание | +|----------------------|--------------|-----------|----------------------------------------------------------------| +| filterType | Нет | `DaDataPartyKazakhstanType[]` | Фильтр по типу организации, параметр type в запросе | + + ### Банки ```jsx diff --git a/packages/react-dadata/src/index.tsx b/packages/react-dadata/src/index.tsx index a1abbb5..a4a8334 100644 --- a/packages/react-dadata/src/index.tsx +++ b/packages/react-dadata/src/index.tsx @@ -23,11 +23,18 @@ import type { DaDataSuggestion, } from './types'; import { PartyBelarusSuggestions } from './variants/party_belarus/party-belarus'; +import { PartyKazakhstanSuggestions } from './variants/party_kazakhstan/party-kazakhstan'; +import type { + DaDataPartyKazakhstan, + DaDataPartyKazakhstanStatus, + DaDataPartyKazakhstanType, +} from './variants/party_kazakhstan/party-kazakhstan-types'; type DaDataAddressSuggestion = DaDataSuggestion; type DaDataPartySuggestion = DaDataSuggestion; type DaDataPartyRussiaSuggestion = DaDataSuggestion; type DaDataPartyBelarusSuggestion = DaDataSuggestion; +type DaDataPartyKazakhstanSuggestion = DaDataSuggestion; type DaDataBankSuggestion = DaDataSuggestion; type DaDataFioSuggestion = DaDataSuggestion; type DaDataEmailSuggestion = DaDataSuggestion; @@ -37,6 +44,7 @@ export { AddressSuggestions, PartySuggestions, PartyBelarusSuggestions, + PartyKazakhstanSuggestions, BankSuggestions, FioSuggestions, EmailSuggestions, @@ -46,6 +54,7 @@ export { type DaDataPartySuggestion, type DaDataPartyRussiaSuggestion, type DaDataPartyBelarusSuggestion, + type DaDataPartyKazakhstanSuggestion, type DaDataAddressBounds, type DaDataPartyType, type DaDataPartyRussia, @@ -54,6 +63,9 @@ export { type DaDataPartyStatus, type DaDataPartyBelarus, type DaDataPartyBelarusStatus, + type DaDataPartyKazakhstan, + type DaDataPartyKazakhstanStatus, + type DaDataPartyKazakhstanType, type DaDataBank, type DaDataBankStatus, type DaDataBankType, diff --git a/packages/react-dadata/src/variants/party_kazakhstan/party-kazakhstan-types.ts b/packages/react-dadata/src/variants/party_kazakhstan/party-kazakhstan-types.ts new file mode 100644 index 0000000..de07ed8 --- /dev/null +++ b/packages/react-dadata/src/variants/party_kazakhstan/party-kazakhstan-types.ts @@ -0,0 +1,48 @@ +/** + * Типы для подсказок по компаниям в Казахстане 🇰🇿 + */ + +export type DaDataPartyKazakhstanStatus = 'ACTIVE' | 'INACTIVE' | 'LIQUIDATING' | 'LIQUIDATED' | 'SUSPENDED'; + +export type DaDataPartyKazakhstanType = + | 'LEGAL' + | 'BRANCH' + | 'INDIVIDUAL' + | 'INDIVIDUAL_JOINT_VENTURE' + | 'FOREIGN_BRANCH'; + +export interface DaDataPartyKazakhstan { + bin: string; + registration_date: string; + status: DaDataPartyKazakhstanStatus; + type: DaDataPartyKazakhstanType; + name_ru: string; + name_kz: string; + fio: string; + kato: string; + address_ru: string; + address_kz: string; + address_settlement_ru: string; + address_settlement_kz: string; + address_local: string; + oked: string; + oked_name_ru: string; + oked_name_kz: string; + krp: string; + krp_name_ru: string; + krp_name_kz: string; + kse: string; + kse_name_ru: string; + kse_name_kz: string; + kfs: string; + kfs_name_ru: string; + kfs_name_kz: string; +} + +type DaDataPartyKazakhstanRequestFilterItem = { type: DaDataPartyKazakhstanType }; + +export interface DaDataPartyKazakhstanRequestPayload extends Record { + query: string; + count: number; + filters?: DaDataPartyKazakhstanRequestFilterItem[]; +} diff --git a/packages/react-dadata/src/variants/party_kazakhstan/party-kazakhstan.tsx b/packages/react-dadata/src/variants/party_kazakhstan/party-kazakhstan.tsx new file mode 100644 index 0000000..b5a5f0c --- /dev/null +++ b/packages/react-dadata/src/variants/party_kazakhstan/party-kazakhstan.tsx @@ -0,0 +1,82 @@ +import React, { type ReactNode } from 'react'; +import { type BaseProps, BaseSuggestions } from '../../BaseSuggestions'; +import { HighlightWords } from '../../HighlightWords'; +import type { DaDataSuggestion } from '../../types'; +import type { + DaDataPartyKazakhstan, + DaDataPartyKazakhstanRequestPayload, + DaDataPartyKazakhstanType, +} from './party-kazakhstan-types'; + +interface Props extends BaseProps { + filterType?: DaDataPartyKazakhstanType[]; +} + +export class PartyKazakhstanSuggestions extends BaseSuggestions< + DaDataPartyKazakhstan, + Props, + DaDataPartyKazakhstanRequestPayload +> { + loadSuggestionsUrl = 'https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/party_kz'; + + /** + * Структура запроса для подсказок по организациям в Казахстане 🇰🇿 + * @see https://dadata.ru/api/suggest/party_kz/ + */ + getLoadSuggestionsData = () => { + const { count, filterType } = this.props; + const { query } = this.state; + + const requestPayload: DaDataPartyKazakhstanRequestPayload = { + query, + count: count || 10, + filters: [], + }; + + // Ограничение по типу организации + if (filterType) { + for (let i = 0; i < filterType.length; i++) { + requestPayload.filters?.push({ + type: filterType[i], + }); + } + } + + return requestPayload; + }; + + // В России ИНН допускает коллизии, и там существует свойство hid + // В Беларуси такого свойства нет, поэтому используем БИН + name_kz + registration_date + protected getSuggestionKey = (suggestion: DaDataSuggestion): string => + suggestion.data.bin + suggestion.data.name_kz + suggestion.data.registration_date; + + protected renderOption = (suggestion: DaDataSuggestion): ReactNode => { + const { renderOption, highlightClassName } = this.props; + const { query } = this.state; + + return renderOption ? ( + renderOption(suggestion, query) + ) : ( +
+
+ +
+
+ {suggestion.data.address_ru && ( +
+ +
+ )} +
+
+ ); + }; +} diff --git a/readme.md b/readme.md index e02b896..d1074d6 100644 --- a/readme.md +++ b/readme.md @@ -177,7 +177,7 @@ const [value, setValue] = useState(); ; ``` -#### Дополнительные параметры для компонента организаций +#### Дополнительные параметры для компонента организаций в России | Свойство | Обязательный | Тип | Описание | |----------------------|--------------|-----------|----------------------------------------------------------------| @@ -198,13 +198,31 @@ const [value, setValue] = useState(); ; ``` -#### Дополнительные параметры для компонента организаций +#### Дополнительные параметры для компонента организаций в Беларуси | Свойство | Обязательный | Тип | Описание | |----------------------|--------------|-----------|----------------------------------------------------------------| | filterStatus | Нет | `DaDataPartyBelarusStatus[]` | Фильтр по статусу организации, параметр status в запросе | | filterType | Нет | `DaDataPartyType[]` | Фильтр по типу организации, параметр type в запросе | +### Организации в Казахстане 🇰🇿 + +```jsx +import { PartyKazakhstanSuggestions } from 'react-dadata'; +import 'react-dadata/dist/react-dadata.css'; + +const [value, setValue] = useState(); + +; +``` + +#### Дополнительные параметры для компонента организаций в Казахстане + +| Свойство | Обязательный | Тип | Описание | +|----------------------|--------------|-----------|----------------------------------------------------------------| +| filterType | Нет | `DaDataPartyKazakhstanType[]` | Фильтр по типу организации, параметр type в запросе | + + ### Банки ```jsx