diff --git a/apps/admin-x-settings/src/components/settings/advanced/integrations/FirstPromoterModal.tsx b/apps/admin-x-settings/src/components/settings/advanced/integrations/FirstPromoterModal.tsx index 1d3db742e3..44143d9b4b 100644 --- a/apps/admin-x-settings/src/components/settings/advanced/integrations/FirstPromoterModal.tsx +++ b/apps/admin-x-settings/src/components/settings/advanced/integrations/FirstPromoterModal.tsx @@ -4,20 +4,58 @@ import Modal from '../../../../admin-x-ds/global/modal/Modal'; import NiceModal from '@ebay/nice-modal-react'; import TextField from '../../../../admin-x-ds/global/form/TextField'; import Toggle from '../../../../admin-x-ds/global/form/Toggle'; +import useRouting from '../../../../hooks/useRouting'; import {ReactComponent as Icon} from '../../../../assets/icons/firstpromoter.svg'; -import {useState} from 'react'; +import {Setting, getSettingValues, useEditSettings} from '../../../../api/settings'; +import {useEffect, useState} from 'react'; +import {useGlobalData} from '../../../providers/GlobalDataProvider'; const FirstpromoterModal = NiceModal.create(() => { + const {updateRoute} = useRouting(); const modal = NiceModal.useModal(); + + const {settings} = useGlobalData(); + const {mutateAsync: editSettings} = useEditSettings(); + + const [accountId, setAccountId] = useState(''); const [enabled, setEnabled] = useState(false); + const [firstPromoterEnabled] = getSettingValues(settings, ['firstpromoter']); + const [firstPromoterId] = getSettingValues(settings, ['firstpromoter_id']); + + useEffect(() => { + setEnabled(firstPromoterEnabled || false); + setAccountId(firstPromoterId || ''); + }, [firstPromoterEnabled, firstPromoterId]); + + const handleSave = async () => { + const updates: Setting[] = [ + { + key: 'firstpromoter', + value: enabled + }, + { + key: 'firstpromoter_id', + value: accountId + } + ]; + + await editSettings(updates); + }; + return ( { + updateRoute('integrations'); + }} cancelLabel='' okColor='black' okLabel='Save' + testId='firstpromoter-modal' title='' - onOk={() => { + onOk={async () => { + await handleSave(); + updateRoute('integrations'); modal.remove(); }} > @@ -29,6 +67,7 @@ const FirstpromoterModal = NiceModal.create(() => {
Enable FirstPromoter for tracking referrals} label='Enable FirstPromoter' @@ -43,6 +82,10 @@ const FirstpromoterModal = NiceModal.create(() => { } placeholder='XXXXXXXX' title='FirstPromoter account ID' + value={accountId} + onChange={(e) => { + setAccountId(e.target.value); + }} /> )} @@ -51,4 +94,4 @@ const FirstpromoterModal = NiceModal.create(() => { ); }); -export default FirstpromoterModal; \ No newline at end of file +export default FirstpromoterModal; diff --git a/apps/admin-x-settings/test/e2e/advanced/integrations/firstPromoter.test.ts b/apps/admin-x-settings/test/e2e/advanced/integrations/firstPromoter.test.ts new file mode 100644 index 0000000000..c2c5d546b4 --- /dev/null +++ b/apps/admin-x-settings/test/e2e/advanced/integrations/firstPromoter.test.ts @@ -0,0 +1,35 @@ +import {expect, test} from '@playwright/test'; +import {globalDataRequests, mockApi, updatedSettingsResponse} from '../../../utils/e2e'; + +test.describe('First Promoter integration', async () => { + test('Supports toggling and filling in First Promoter integration', async ({page}) => { + const {lastApiRequests} = await mockApi({page, requests: { + ...globalDataRequests, + editSettings: {method: 'PUT', path: '/settings/', response: updatedSettingsResponse([ + {key: 'firstpromoter', value: true}, + {key: 'firstpromoter_id', value: '123456789'} + ])} + }}); + + await page.goto('/'); + const section = page.getByTestId('integrations'); + const fpElement = section.getByText('FirstPromoter').last(); + await fpElement.hover(); + await page.getByRole('button', {name: 'Configure'}).click(); + const fpModal = page.getByTestId('firstpromoter-modal'); + + const fpToggle = fpModal.getByRole('switch'); + await fpToggle.click(); + const input = fpModal.getByRole('textbox'); + await input.fill('123456789'); + + await fpModal.getByRole('button', {name: 'Save'}).click(); + + expect(lastApiRequests.editSettings?.body).toEqual({ + settings: [ + {key: 'firstpromoter', value: true}, + {key: 'firstpromoter_id', value: '123456789'} + ] + }); + }); +});