Skip to content

Commit

Permalink
Merge pull request #182 from rahulg1254/admin-master
Browse files Browse the repository at this point in the history
Task #225241 feat: UT issues resolved
  • Loading branch information
itsvick authored Sep 3, 2024
2 parents e603ce7 + 562f3a3 commit 98f1e03
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 114 deletions.
4 changes: 2 additions & 2 deletions public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"CANCEL": "Cancel",
"ARE_YOU_SURE_DELETE": "There are {{block}} Active blocks in this district you cannot delete it.",
"NO_ACTIVE_BLOCKS_DELETE": "There are no active blocks in this district. Do you want to delete it?",
"ARE_YOU_SURE_DELETE_BLOCK": "There are Active centers in this block you cannot delete it",
"ARE_YOU_SURE_DELETE_BLOCK": "There are {{centers}} Active centers in this block you cannot delete it",
"NO_ACTIVE_CENTERS_DELETE": "There are no active centers in this block. Do you want to delete it?",
"CONFIRM_DELETE": "Confirm Delete",
"ADD_STATE": "Add New State",
Expand Down Expand Up @@ -212,7 +212,7 @@

},
"MASTER": {
"STATE": "State",
"STATE": "States",
"STATE_NAMES": "State Name",
"SORT": "Sort",
"DISTRICT_NAMES": "District Name",
Expand Down
80 changes: 32 additions & 48 deletions src/components/AddBlockModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const AddBlockModal: React.FC<AddBlockModalProps> = ({

const [errors, setErrors] = useState<Record<string, string | null>>({});
const [districts, setDistricts] = useState<
{ value: string; label: string; cohortId: string | null}[]
{ value: string; label: string; cohortId: string | null }[]
>([]);

const [districtsOptionRead, setDistrictsOptionRead] = useState<any>([]);
Expand Down Expand Up @@ -185,9 +185,11 @@ export const AddBlockModal: React.FC<AddBlockModalProps> = ({
value: string,
requiredMessage: string
) => {
if (!value) return requiredMessage;
if (field !== "controllingField" && !/^[a-zA-Z\s]+$/.test(value))
if (!value) return null;

if (field !== "controllingField" && !/^[a-zA-Z\s]+$/.test(value)) {
return t("COMMON.INVALID_TEXT");
}

const isUnique = (fieldName: string, value: string) => {
return true;
Expand Down Expand Up @@ -215,35 +217,7 @@ export const AddBlockModal: React.FC<AddBlockModalProps> = ({

setFormData((prev) => ({ ...prev, [field]: value }));

let errorMessage: string | null = null;

if (field === "name") {
errorMessage = validateField(
field,
value,
t("COMMON.BLOCK_NAME_REQUIRED")
);
} else if (field === "value") {
errorMessage = validateField(
field,
value,
t("COMMON.BLOCK_CODE_REQUIRED")
);
} else if (field === "controllingField") {
errorMessage = validateField(
field,
value,
t("COMMON.DISTRICT_NAME_REQUIRED")
);

const selectedDistrict = districts.find(
(district) => district.value === value
);
setCohortIdAddNewDropdown(selectedDistrict?.cohortId || null);

console.log("Selected District:", selectedDistrict);
console.log("Cohort ID Set:", selectedDistrict?.cohortId);
}
let errorMessage: string | null = validateField(field, value, "");

setErrors((prev) => ({
...prev,
Expand All @@ -253,21 +227,24 @@ export const AddBlockModal: React.FC<AddBlockModalProps> = ({

const validateForm = () => {
const newErrors = {
name: validateField(
"name",
formData.name,
t("COMMON.BLOCK_NAME_REQUIRED")
),
value: validateField(
"value",
formData.value,
t("COMMON.BLOCK_CODE_REQUIRED")
),
controllingField: validateField(
"controllingField",
formData.controllingField,
t("COMMON.DISTRICT_NAME_REQUIRED")
),
name:
validateField("name", formData.name, t("COMMON.BLOCK_NAME_REQUIRED")) ||
(!formData.name ? t("COMMON.BLOCK_NAME_REQUIRED") : null),
value:
validateField(
"value",
formData.value,
t("COMMON.BLOCK_CODE_REQUIRED")
) || (!formData.value ? t("COMMON.BLOCK_CODE_REQUIRED") : null),
controllingField:
validateField(
"controllingField",
formData.controllingField,
t("COMMON.DISTRICT_NAME_REQUIRED")
) ||
(!formData.controllingField
? t("COMMON.DISTRICT_NAME_REQUIRED")
: null),
};

setErrors(newErrors);
Expand All @@ -293,7 +270,7 @@ export const AddBlockModal: React.FC<AddBlockModalProps> = ({
formData.controllingField,
currentCohortId,
fieldId,
districtId,
districtId
);

setFormData({
Expand Down Expand Up @@ -323,6 +300,13 @@ export const AddBlockModal: React.FC<AddBlockModalProps> = ({
e as React.ChangeEvent<HTMLInputElement>
)
}
MenuProps={{
PaperProps: {
sx: {
maxHeight: 400,
},
},
}}
fullWidth
displayEmpty
variant="outlined"
Expand Down
25 changes: 4 additions & 21 deletions src/components/AddDistrictModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ const AddDistrictModal: React.FC<AddDistrictBlockModalProps> = ({
value: initialValues.value ?? "",
controllingField: initialValues.controllingField ?? stateCode,
});
setErrors({});
setErrors({});
}, [initialValues, stateCode]);

const isValidName = (input: string) =>
/^[a-zA-Z]+(?:\s[a-zA-Z]+)*$/.test(input);

Expand All @@ -112,31 +112,14 @@ const AddDistrictModal: React.FC<AddDistrictBlockModalProps> = ({

setFormData((prev) => ({ ...prev, [field]: value }));

if (value === "") {
setErrors((prev) => ({
...prev,
[field]: t(
field === "name"
? "COMMON.DISTRICT_NAME_REQUIRED"
: field === "controllingField"
? "COMMON.STATE_NAME_REQUIRED"
: "COMMON.CODE_REQUIRED"
),
}));
} else if (field === "name" && !isValidName(value.trim())) {
setErrors((prev) => ({ ...prev, [field]: t("COMMON.INVALID_INPUT") }));
} else if (field === "value" && !isValidCode(value)) {
setErrors((prev) => ({ ...prev, [field]: t("COMMON.INVALID_TEXT") }));
} else {
setErrors((prev) => ({ ...prev, [field]: null }));
}
setErrors((prev) => ({ ...prev, [field]: null }));
};

const validateForm = () => {
const newErrors: { name?: string; value?: string } = {};

if (!formData.name) {
newErrors.name = t("COMMON.STATE_NAME_REQUIRED");
newErrors.name = t("COMMON.DISTRICT_NAME_REQUIRED");
} else if (!isValidName(formData.name.trim())) {
newErrors.name = t("COMMON.INVALID_TEXT");
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/layouts/sidebar/MenuItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ const Menuitems = [
// href: "/",
subOptions: [
{
title: "State",
title: "MASTER.STATE",
href: "/state",
},
{
title: "District",
title: "MASTER.DISTRICTS",
href: "/district",
},
{
title: "Block",
title: "MASTER.BLOCKS",
href: "/block",
},
],
Expand Down
Loading

0 comments on commit 98f1e03

Please sign in to comment.