Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 1797 #1458

Open
wants to merge 3 commits into
base: release/1.1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { FactCreate, FactUpdate } from '@/models/streetcode/text-contents.model'
import PreviewFileModal from '../../MainBlock/PreviewFileModal/PreviewFileModal.component';
import { UploadChangeParam } from 'antd/es/upload';
import POPOVER_CONTENT from '@/features/AdminPage/JobsPage/JobsModal/constants/popoverContent';
import uniquenessValidator from '@/app/common/utils/uniquenessValidator';

interface Props {
fact?: FactCreate,
Expand Down Expand Up @@ -62,6 +63,12 @@ const InterestingFactsAdminModal = ({ fact, open, setModalOpen, onChange }: Prop
setFileList([]);
};

const validateFact = uniquenessValidator(
() => (factsStore.getFactArray.map( (fact) => fact.title)),
() => (fact?.title),
Copy link
Contributor

@Michael-Kolpakov Michael-Kolpakov Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really necessary to wrap the right side of the expression in parentheses?

'Факт з такою назвою вже існує',
);

useEffect(() => {
if (fact && open) {
setHasUploadedPhoto(true);
Expand Down Expand Up @@ -110,17 +117,7 @@ const InterestingFactsAdminModal = ({ fact, open, setModalOpen, onChange }: Prop
return fact;
};

const setFactUpdateIfItExist = (formValues: any) => {
factsStore.getFactArray.map((t) => t).forEach((t) => {
if (formValues.title === t.title
|| formValues.factContent === t.factContent
|| imageId.current === t.imageId) fact = t;
});
}

const onSuccesfulSubmit = (formValues: any) => {
setFactUpdateIfItExist(formValues);

if (fact) {
let item = factsStore.factMap.get(fact.id) as FactUpdate;

Expand All @@ -143,6 +140,7 @@ const InterestingFactsAdminModal = ({ fact, open, setModalOpen, onChange }: Prop

setHasUploadedPhoto(false);
onChange('fact', formValues);
clearModal();
};

const handleOk = async () => {
Expand Down Expand Up @@ -185,6 +183,7 @@ const InterestingFactsAdminModal = ({ fact, open, setModalOpen, onChange }: Prop
name="title"
label="Заголовок: "
rules={[{ required: true, message: 'Введіть заголовок, будь ласка' },
{ validator: validateFact}
]}
>
<Input maxLength={68} showCount onChange={(e) => onChange('title', e.target.value)} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import TimelineItem, {
HistoricalContext, HistoricalContextUpdate, selectDateOptionsforTimeline,
} from '@/models/timeline/chronology.model';
import POPOVER_CONTENT from '@/features/AdminPage/JobsPage/JobsModal/constants/popoverContent';
import uniquenessValidator from '@/app/common/utils/uniquenessValidator';

interface NewTimelineModalProps {
timelineItem?: TimelineItem;
Expand Down Expand Up @@ -116,12 +117,13 @@ const NewTimelineModal: React.FC<NewTimelineModalProps> = observer(({ timelineIt
}
};

const validateTimelineItem = uniquenessValidator(
() => (timelineItemStore.getTimelineItemArray.map((item) => item.title)),
() => (timelineItem?.title),
'Хронологія з такою назвою вже існує',
);

const onSuccesfulSubmit = (formValues: any) => {
timelineItemStore.getTimelineItemArray.map((t) => t).forEach((t) => {
if (formValues.title == t.title || formValues.description == t.description) {
timelineItem = t;
}
});
if (timelineItem) {
const item = timelineItemStore.timelineItemMap.get(timelineItem.id);
if (item) {
Expand All @@ -144,6 +146,7 @@ const NewTimelineModal: React.FC<NewTimelineModalProps> = observer(({ timelineIt
}

onChange('timeline', formValues);
clearModal();
};

const onContextSelect = useCallback((value: string) => {
Expand Down Expand Up @@ -204,7 +207,7 @@ const NewTimelineModal: React.FC<NewTimelineModalProps> = observer(({ timelineIt
await form.validateFields();
form.submit();
message.success('Хронологію успішно додано!', 2);
setIsSaveButtonDisabled(true);
setIsSaveButtonDisabled(true);
} catch (error) {
message.config({
top: 100,
Expand All @@ -216,9 +219,9 @@ const NewTimelineModal: React.FC<NewTimelineModalProps> = observer(({ timelineIt
}
};

const handleInputChange = () => {
setIsSaveButtonDisabled(false);
}
const handleInputChange = () => {
setIsSaveButtonDisabled(false);
}

return (
<Modal
Expand All @@ -227,7 +230,7 @@ const NewTimelineModal: React.FC<NewTimelineModalProps> = observer(({ timelineIt
onCancel={() => {
setIsModalOpen(false);
setDateTimePickerType('date');
setIsSaveButtonDisabled(true);
setIsSaveButtonDisabled(true);
}}
footer={null}
maskClosable
Expand All @@ -251,7 +254,10 @@ const NewTimelineModal: React.FC<NewTimelineModalProps> = observer(({ timelineIt
<Form.Item
name="title"
label="Назва: "
rules={[{ required: true, message: 'Введіть назву', max: MAX_LENGTH.title }]}
rules={[
{ required: true, message: 'Введіть назву', max: MAX_LENGTH.title },
{ validator: validateTimelineItem },
]}
>
<Input
maxLength={MAX_LENGTH.title}
Expand Down