Skip to content

Commit

Permalink
(refactor): remove repeated code
Browse files Browse the repository at this point in the history
  • Loading branch information
NethmiRodrigo committed Jun 27, 2024
1 parent 17a7ceb commit a7cfdcc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 43 deletions.
38 changes: 19 additions & 19 deletions src/components/interactive-builder/add-question.modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ interface RequiredLabelProps {
t: TFunction;
}

export const updateDatePickerType = (concept: Concept): DatePickerType | null => {
const conceptDataType = concept.datatype.name;
switch (conceptDataType) {
case 'Datetime':
return 'both';
case 'Date':
return 'calendar';
case 'Time':
return 'timer';
default:
console.warn(`Unsupported datatype for date fields: ${conceptDataType}`);
return null;
}
};

const AddQuestionModal: React.FC<AddQuestionModalProps> = ({
closeModal,
onSchemaChange,
Expand Down Expand Up @@ -139,25 +154,9 @@ const AddQuestionModal: React.FC<AddQuestionModalProps> = ({

const handleConceptChange = (event: React.ChangeEvent<HTMLInputElement>) => setConceptToLookup(event.target.value);

const updateDatePickerType = (concept: Concept) => {
const conceptDataType = concept.datatype.name;
switch (conceptDataType) {
case 'Datetime':
setDatePickerType('both');
break;
case 'Date':
setDatePickerType('calendar');
break;
case 'Time':
setDatePickerType('timer');
break;
default:
break;
}
};

const handleConceptSelect = (concept: Concept) => {
updateDatePickerType(concept);
const updatedDatePickerType = updateDatePickerType(concept);
if (updatedDatePickerType) setDatePickerType(updatedDatePickerType);
setConceptToLookup('');
setSelectedConcept(concept);
setAnswers(
Expand Down Expand Up @@ -213,7 +212,8 @@ const AddQuestionModal: React.FC<AddQuestionModalProps> = ({
type: questionType,
required: isQuestionRequired,
id: questionId ?? computedQuestionId,
...((renderingType === 'date' || renderingType === 'datetime') && { datePickerFormat: datePickerType }),
...((renderingType === 'date' || renderingType === 'datetime') &&
datePickerType && { datePickerFormat: datePickerType }),
questionOptions: {
rendering: renderingType,
concept: selectedConcept?.uuid ? selectedConcept?.uuid : '',
Expand Down
33 changes: 9 additions & 24 deletions src/components/interactive-builder/edit-question.modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { usePersonAttributeName } from '../../hooks/usePersonAttributeName';
import { usePersonAttributeTypes } from '../../hooks/usePersonAttributeTypes';
import { usePrograms, useProgramWorkStates } from '../../hooks/useProgramStates';
import styles from './question-modal.scss';
import { updateDatePickerType } from './add-question.modal';

interface EditQuestionModalProps {
closeModal: () => void;
Expand Down Expand Up @@ -85,12 +86,6 @@ const EditQuestionModal: React.FC<EditQuestionModalProps> = ({
const { t } = useTranslation();
const { fieldTypes, questionTypes } = useConfig<ConfigObject>();

const datePickerTypeOptions: DatePickerTypeOptions = {
datetime: [{ value: 'both', label: t('calendarAndTimer', 'Calendar and timer'), defaultChecked: true }],
date: [{ value: 'calendar', label: t('calendarOnly', 'Calendar only'), defaultChecked: false }],
time: [{ value: 'timer', label: t('timerOnly', 'Timer only'), defaultChecked: false }],
};

const [answersChanged, setAnswersChanged] = useState(false);
const [answersFromConcept, setAnswersFromConcept] = useState<
Array<{
Expand Down Expand Up @@ -152,6 +147,12 @@ const EditQuestionModal: React.FC<EditQuestionModalProps> = ({
const hasConceptChanged = selectedConcept && questionToEdit?.questionOptions?.concept !== selectedConcept?.uuid;
const [addInlineDate, setAddInlineDate] = useState(false);

const datePickerTypeOptions: DatePickerTypeOptions = {
datetime: [{ value: 'both', label: t('calendarAndTimer', 'Calendar and timer'), defaultChecked: true }],
date: [{ value: 'calendar', label: t('calendarOnly', 'Calendar only'), defaultChecked: false }],
time: [{ value: 'timer', label: t('timerOnly', 'Timer only'), defaultChecked: false }],
};

const debouncedSearch = useMemo(() => {
return debounce((searchTerm: string) => setConceptToLookup(searchTerm), 500) as (searchTerm: string) => void;
}, []);
Expand All @@ -172,25 +173,9 @@ const EditQuestionModal: React.FC<EditQuestionModalProps> = ({
setSelectedPersonAttributeType(attributeType);
};

const updateDatePickerType = (concept: Concept) => {
const conceptDataType = concept.datatype.name;
switch (conceptDataType) {
case 'Datetime':
setDatePickerType('both');
break;
case 'Date':
setDatePickerType('calendar');
break;
case 'Time':
setDatePickerType('timer');
break;
default:
break;
}
};

const handleConceptSelect = (concept: Concept) => {
updateDatePickerType(concept);
const datePickerType = updateDatePickerType(concept);
if (datePickerType) setDatePickerType(datePickerType);
setConceptToLookup('');
setSelectedAnswers([]);
setSelectedConcept(concept);
Expand Down

0 comments on commit a7cfdcc

Please sign in to comment.