{type}
- {link}
-
+ {link}
+
+
+
diff --git a/src/components/Advertisements/core/AdvertisementRegister/AdvertisementRegister.module.css b/src/components/Advertisements/core/AdvertisementRegister/AdvertisementRegister.module.css
index c122d386fa..646311041a 100644
--- a/src/components/Advertisements/core/AdvertisementRegister/AdvertisementRegister.module.css
+++ b/src/components/Advertisements/core/AdvertisementRegister/AdvertisementRegister.module.css
@@ -4,6 +4,16 @@
align-items: center;
}
-.modalbtn i {
+.modalbtn i,
+.button i {
margin-right: 8px;
}
+
+.button {
+ min-width: 102px;
+}
+
+.editHeader {
+ background-color: #31bb6b;
+ color: white;
+}
diff --git a/src/components/Advertisements/core/AdvertisementRegister/AdvertisementRegister.test.tsx b/src/components/Advertisements/core/AdvertisementRegister/AdvertisementRegister.test.tsx
index 0c14996307..b0249cc48f 100644
--- a/src/components/Advertisements/core/AdvertisementRegister/AdvertisementRegister.test.tsx
+++ b/src/components/Advertisements/core/AdvertisementRegister/AdvertisementRegister.test.tsx
@@ -1,5 +1,5 @@
import React from 'react';
-import { render, fireEvent, waitFor } from '@testing-library/react';
+import { render, fireEvent, waitFor, screen } from '@testing-library/react';
import {
ApolloClient,
@@ -48,6 +48,7 @@ const MOCKS = [
data: {
createAdvertisement: {
_id: '1',
+ __typename: 'Advertisement',
},
},
},
@@ -93,7 +94,7 @@ describe('Testing Advertisement Register Component', () => {
});
});
- test('AdvertismentRegister component loads correctly', async () => {
+ test('AdvertismentRegister component loads correctly in register mode', async () => {
const { getByText } = render(
@@ -118,6 +119,34 @@ describe('Testing Advertisement Register Component', () => {
expect(getByText(translations.addNew)).toBeInTheDocument();
});
});
+
+ test('AdvertismentRegister component loads correctly in edit mode', async () => {
+ render(
+
+
+
+
+ {
+
+ }
+
+
+
+
+ );
+ await waitFor(() => {
+ expect(screen.getByTestId('editBtn')).toBeInTheDocument();
+ });
+ });
+
test('Opens and closes modals on button click', async () => {
const { getByText, queryByText } = render(
@@ -147,6 +176,7 @@ describe('Testing Advertisement Register Component', () => {
expect(queryByText(translations.close)).not.toBeInTheDocument();
});
});
+
test('Submits the form and shows success toast on successful advertisement creation', async () => {
const setTimeoutSpy = jest.spyOn(global, 'setTimeout');
@@ -171,40 +201,41 @@ describe('Testing Advertisement Register Component', () => {
);
- await waitFor(() => {
- fireEvent.click(getByText(translations.addNew));
- expect(queryByText(translations.RClose)).toBeInTheDocument();
+ fireEvent.click(getByText(translations.addNew));
+ expect(queryByText(translations.RClose)).toBeInTheDocument();
- fireEvent.change(getByLabelText(translations.Rname), {
- target: { value: 'Test Advertisement' },
- });
- expect(getByLabelText(translations.Rname)).toHaveValue(
- 'Test Advertisement'
- );
+ fireEvent.change(getByLabelText(translations.Rname), {
+ target: { value: 'Test Advertisement' },
+ });
+ expect(getByLabelText(translations.Rname)).toHaveValue(
+ 'Test Advertisement'
+ );
- fireEvent.change(getByLabelText(translations.Rlink), {
- target: { value: 'http://example.com' },
- });
- expect(getByLabelText(translations.Rlink)).toHaveValue(
- 'http://example.com'
- );
+ fireEvent.change(getByLabelText(translations.Rlink), {
+ target: { value: 'http://example.com' },
+ });
+ expect(getByLabelText(translations.Rlink)).toHaveValue(
+ 'http://example.com'
+ );
- fireEvent.change(getByLabelText(translations.Rtype), {
- target: { value: 'BANNER' },
- });
- expect(getByLabelText(translations.Rtype)).toHaveValue('BANNER');
+ fireEvent.change(getByLabelText(translations.Rtype), {
+ target: { value: 'BANNER' },
+ });
+ expect(getByLabelText(translations.Rtype)).toHaveValue('BANNER');
- fireEvent.change(getByLabelText(translations.RstartDate), {
- target: { value: '2023-01-01' },
- });
- expect(getByLabelText(translations.RstartDate)).toHaveValue('2023-01-01');
+ fireEvent.change(getByLabelText(translations.RstartDate), {
+ target: { value: '2023-01-01' },
+ });
+ expect(getByLabelText(translations.RstartDate)).toHaveValue('2023-01-01');
- fireEvent.change(getByLabelText(translations.RendDate), {
- target: { value: '2023-02-01' },
- });
- expect(getByLabelText(translations.RendDate)).toHaveValue('2023-02-01');
+ fireEvent.change(getByLabelText(translations.RendDate), {
+ target: { value: '2023-02-01' },
+ });
+ expect(getByLabelText(translations.RendDate)).toHaveValue('2023-02-01');
- fireEvent.click(getByText(translations.register));
+ fireEvent.click(getByText(translations.register));
+ await waitFor(() => {
+ // Assert the success toast and setTimeout
expect(toast.success).toBeCalledWith(
'Advertisement created successfully'
);
@@ -213,6 +244,7 @@ describe('Testing Advertisement Register Component', () => {
expect(queryByText(translations.close)).not.toBeInTheDocument();
});
+
test('Logs error to the console and shows error toast when advertisement creation fails', async () => {
const { getByText, queryByText } = render(
@@ -221,12 +253,12 @@ describe('Testing Advertisement Register Component', () => {
{
}
@@ -235,11 +267,11 @@ describe('Testing Advertisement Register Component', () => {
);
- await waitFor(() => {
- fireEvent.click(getByText(translations.addNew));
- expect(queryByText(translations.RClose)).toBeInTheDocument();
+ fireEvent.click(getByText(translations.addNew));
+ expect(queryByText(translations.RClose)).toBeInTheDocument();
- fireEvent.click(getByText(translations.register));
+ fireEvent.click(getByText(translations.register));
+ await waitFor(() => {
expect(toast.error).toBeCalledWith(
'An error occured, could not create new advertisement'
);
diff --git a/src/components/Advertisements/core/AdvertisementRegister/AdvertisementRegister.tsx b/src/components/Advertisements/core/AdvertisementRegister/AdvertisementRegister.tsx
index ff9391fa7b..44b12b01be 100644
--- a/src/components/Advertisements/core/AdvertisementRegister/AdvertisementRegister.tsx
+++ b/src/components/Advertisements/core/AdvertisementRegister/AdvertisementRegister.tsx
@@ -1,9 +1,12 @@
-import React, { useState } from 'react';
+import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import styles from './AdvertisementRegister.module.css';
import { Button, Form, Modal } from 'react-bootstrap';
+import {
+ ADD_ADVERTISEMENT_MUTATION,
+ UPDATE_ADVERTISEMENT_MUTATION,
+} from 'GraphQl/Mutations/mutations';
import { useMutation, useQuery } from '@apollo/client';
-import { ADD_ADVERTISEMENT_MUTATION } from 'GraphQl/Mutations/mutations';
import { useTranslation } from 'react-i18next';
import { toast } from 'react-toastify';
import dayjs from 'dayjs';
@@ -12,6 +15,14 @@ import { ADVERTISEMENTS_GET } from 'GraphQl/Queries/Queries';
interface InterfaceAddOnRegisterProps {
id?: string; // OrgId
createdBy?: string; // User
+ formStatus?: string;
+ idEdit?: string;
+ nameEdit?: string;
+ typeEdit?: string;
+ orgIdEdit?: string;
+ linkEdit?: string;
+ endDateEdit?: Date;
+ startDateEdit?: Date;
}
interface InterfaceFormStateTypes {
name: string;
@@ -25,6 +36,16 @@ interface InterfaceFormStateTypes {
function advertisementRegister({
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
createdBy,
+ formStatus,
+ idEdit,
+ nameEdit,
+ typeEdit,
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
+ orgIdEdit,
+ linkEdit,
+ endDateEdit,
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
+ startDateEdit,
}: InterfaceAddOnRegisterProps): JSX.Element {
const { t } = useTranslation('translation', { keyPrefix: 'advertisement' });
@@ -33,6 +54,7 @@ function advertisementRegister({
const handleClose = (): void => setShow(false);
const handleShow = (): void => setShow(true);
const [create] = useMutation(ADD_ADVERTISEMENT_MUTATION);
+ const [updateAdvertisement] = useMutation(UPDATE_ADVERTISEMENT_MUTATION);
const { refetch } = useQuery(ADVERTISEMENTS_GET);
//getting orgId from URL
@@ -45,6 +67,30 @@ function advertisementRegister({
endDate: new Date(),
orgId: currentOrg,
});
+
+ //if set to edit set the formState by edit variables
+ useEffect(() => {
+ if (formStatus === 'edit') {
+ setFormState((prevState) => ({
+ ...prevState,
+ name: nameEdit || '',
+ link: linkEdit || '',
+ type: typeEdit || 'BANNER',
+ startDate: startDateEdit || new Date(),
+ endDate: endDateEdit || new Date(),
+ orgId: currentOrg,
+ }));
+ }
+ }, [
+ formStatus,
+ nameEdit,
+ linkEdit,
+ typeEdit,
+ startDateEdit,
+ endDateEdit,
+ currentOrg,
+ ]);
+
const handleRegister = async (): Promise => {
try {
console.log('At handle register', formState);
@@ -77,20 +123,89 @@ function advertisementRegister({
console.log('error occured', error);
}
};
+ const handleUpdate = async (): Promise => {
+ try {
+ const updatedFields: Partial = {};
+
+ // Only include the fields which are updated
+ if (formState.name !== nameEdit) {
+ updatedFields.name = formState.name;
+ }
+ if (formState.link !== linkEdit) {
+ updatedFields.link = formState.link;
+ }
+ if (formState.type !== typeEdit) {
+ updatedFields.type = formState.type;
+ }
+ const startDateFormattedString = dayjs(formState.startDate).format(
+ 'YYYY-MM-DD'
+ );
+ const endDateFormattedString = dayjs(formState.endDate).format(
+ 'YYYY-MM-DD'
+ );
+
+ const startDateDate = dayjs(
+ startDateFormattedString,
+ 'YYYY-MM-DD'
+ ).toDate();
+ const endDateDate = dayjs(endDateFormattedString, 'YYYY-MM-DD').toDate();
+
+ if (!dayjs(startDateDate).isSame(startDateEdit, 'day')) {
+ updatedFields.startDate = startDateDate;
+ }
+ if (!dayjs(endDateDate).isSame(endDateEdit, 'day')) {
+ updatedFields.endDate = endDateDate;
+ }
+
+ console.log('At handle update', updatedFields);
+ const { data } = await updateAdvertisement({
+ variables: {
+ id: idEdit,
+ ...(updatedFields.name && { name: updatedFields.name }),
+ ...(updatedFields.link && { link: updatedFields.link }),
+ ...(updatedFields.type && { type: updatedFields.type }),
+ ...(updatedFields.startDate && {
+ startDate: startDateFormattedString,
+ }),
+ ...(updatedFields.endDate && { endDate: endDateFormattedString }),
+ },
+ });
+
+ if (data) {
+ toast.success('Advertisement updated successfully');
+ refetch();
+ handleClose();
+ }
+ } catch (error: any) {
+ toast.error(error.message);
+ }
+ };
return (
+ //If register show register button else show edit button
<>
-
+ {formStatus === 'register' ? (
+
+ ) : (
+
+ {t('edit')}
+
+ )}
-
- {t('RClose')}
+
+ {formStatus === 'register' ? (
+ {t('RClose')}
+ ) : (
+ {t('editAdvertisement')}
+ )}
{
setFormState({
...formState,
@@ -164,7 +278,7 @@ function advertisementRegister({
{
setFormState({
...formState,
@@ -183,13 +297,23 @@ function advertisementRegister({
>
{t('close')}
-
+ {formStatus === 'register' ? (
+
+ ) : (
+
+ )}
>
@@ -203,6 +327,7 @@ advertisementRegister.defaultProps = {
startDate: new Date(),
endDate: new Date(),
orgId: '',
+ formStatus: 'register',
};
advertisementRegister.propTypes = {
@@ -212,6 +337,7 @@ advertisementRegister.propTypes = {
startDate: PropTypes.instanceOf(Date),
endDate: PropTypes.instanceOf(Date),
orgId: PropTypes.string,
+ formStatus: PropTypes.string,
};
export default advertisementRegister;