Skip to content

Commit

Permalink
Merge pull request #84 from rahulg1254/admin-master
Browse files Browse the repository at this point in the history
Task #223691 feat: Integration Create, Update, Delete API's in District - Custom Modal for Block & District
  • Loading branch information
itsvick authored Aug 2, 2024
2 parents 61b27c6 + 3599f16 commit e639c79
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 77 deletions.
11 changes: 9 additions & 2 deletions public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@
"STATE_NAME": "State Name",
"INVALID_TEXT": "Invalid Text",
"INVALID_CODE": "Invalid Code",
"FILTER_BY_STATUS": "Filter By Status :"
"FILTER_BY_STATUS": "Filter By Status :",
"DISTRICT_NAME": "District Name",
"DISTRICT_CODE": "District Code",
"CONTROLLING_FIELD": "Controlling Field",
"ADD_DISTRICT": "Add District"
},
"LOGIN_PAGE": {
"USERNAME": "Username",
Expand Down Expand Up @@ -129,7 +133,10 @@
"SEARCHBAR_PLACEHOLDER_BLOCK": "Search Blocks",
"BLOCKS": "Blocks",
"UPDATED_AT": "Updated At",
"CREATED_AT": "Created At"
"CREATED_AT": "Created At",
"STATE_CODE": "State Code",
"DISTRICT_CODE": "District Code",
"BLOCK_CODE": "Block Code"
},
"CENTERS": {
"CENTERS": "Centers",
Expand Down
138 changes: 138 additions & 0 deletions src/components/AddDistrictBlockModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import React, { useState, useEffect } from "react";
import {
Dialog,
DialogTitle,
DialogContent,
DialogActions,
TextField,
Button,
Typography,
Box,
} from "@mui/material";
import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined";
import { useTranslation } from "next-i18next";

interface AddDistrictBlockModalProps {
open: boolean;
onClose: () => void;
onSubmit: (
name: string,
value: string,
controllingField: string,
fieldId: string,
districtId?: string
) => void;
fieldId: string;
initialValues?: {
name?: string;
value?: string;
controllingField?: string;
};
districtId?: string;
}

export const AddDistrictBlockModal: React.FC<AddDistrictBlockModalProps> = ({
open,
onClose,
onSubmit,
fieldId,
initialValues = {},
districtId,
}) => {
const [formData, setFormData] = useState({
name: initialValues.name || "",
value: initialValues.value || "",
controllingField: initialValues.controllingField || "",
});

const { t } = useTranslation();

useEffect(() => {
setFormData({
name: initialValues.name || "",
value: initialValues.value || "",
controllingField: initialValues.controllingField || "",
});
}, [initialValues]);

const handleChange =
(field: string) => (event: React.ChangeEvent<HTMLInputElement>) => {
setFormData((prev) => ({ ...prev, [field]: event.target.value }));
};

const handleSubmit = () => {
const { name, value, controllingField } = formData;
onSubmit(name, value, controllingField, fieldId, districtId);
console.log(name, value, controllingField, fieldId, districtId);
onClose();
};

const buttonStyles = {
fontSize: "14px",
fontWeight: "500",
};

return (
<Dialog open={open} onClose={onClose}>
<DialogTitle>{t("COMMON.ADD_DISTRICT")}</DialogTitle>
<DialogContent>
<TextField
margin="dense"
label="Controlling Field"
type="text"
fullWidth
variant="outlined"
value={formData.controllingField}
onChange={handleChange("controllingField")}
/>
<TextField
margin="dense"
label="Name"
type="text"
fullWidth
variant="outlined"
value={formData.name}
onChange={handleChange("name")}
/>
<TextField
margin="dense"
label="Value"
type="text"
fullWidth
variant="outlined"
value={formData.value}
onChange={handleChange("value")}
/>
<Box display="flex" alignItems="center" mt={2}>
<InfoOutlinedIcon color="primary" sx={{ mr: 1 }} />
<Typography variant="caption" color="textSecondary">
{t("COMMON.CODE_NOTIFICATION")}
</Typography>
</Box>
</DialogContent>
<DialogActions>
<Button
onClick={onClose}
sx={{
...buttonStyles,
color: "secondary",
"&:hover": {
backgroundColor: "transparent",
},
}}
variant="outlined"
>
{t("COMMON.CANCEL")}
</Button>
<Button
onClick={handleSubmit}
sx={{ ...buttonStyles, width: "auto", height: "40px" }}
variant="contained"
color="primary"
>
{t("COMMON.SUBMIT")}
</Button>
</DialogActions>
</Dialog>
);
};
12 changes: 9 additions & 3 deletions src/pages/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const Block: React.FC = () => {
setLoading(true);
try {
const data = await getStateBlockDistrictList({ fieldName: "states" });
const states = data?.result || [];
const states = data?.result?.values || [];
setStateData(states);
if (states.length > 0) {
setSelectedState(states[0].value); // Set the first state as selected by default
Expand All @@ -83,7 +83,7 @@ const Block: React.FC = () => {
controllingfieldfk: selectedState,
fieldName: "districts",
});
const districts = data?.result || [];
const districts = data?.result?.values || [];
setDistrictData(districts);
if (districts.length > 0) {
setSelectedDistrict(districts[0].value); // Set the first district as selected by default
Expand All @@ -108,7 +108,7 @@ const Block: React.FC = () => {
controllingfieldfk: selectedDistrict,
fieldName: "blocks",
});
setBlockData(data?.result || []);
setBlockData(data?.result?.values || []);
} catch (error) {
console.error("Error fetching blocks", error);
} finally {
Expand All @@ -127,6 +127,11 @@ const Block: React.FC = () => {
title: t("MASTER.BLOCK_NAMES"),
dataType: DataType.String,
},
{
key: "value",
title: t("MASTER.BLOCK_CODE"),
dataType: DataType.String,
},
{
key: "createdAt",
title: t("MASTER.CREATED_AT"),
Expand Down Expand Up @@ -290,6 +295,7 @@ const Block: React.FC = () => {
block: transformLabel(block.label),
createdAt: block.createdAt,
updatedAt: block.updatedAt,
value: block.value,
}))}
limit={pageLimit}
offset={pageOffset}
Expand Down
Loading

0 comments on commit e639c79

Please sign in to comment.