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

refactor #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
167 changes: 88 additions & 79 deletions src/components/ColumnActionsForPerson.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,77 +6,82 @@ import { deletePersonData, updatePersonData } from "../api/person";
import { IWindowedTableColumns } from "../interfaces/component/ITableConfig";
import formElStyle from "../sass/settings.module.sass";

const PersonUpdateModal = (props: {
const PersonUpdateModal = ({
updateRow,
data,
columnConfigs,
setShowModal,
}: {
updateRow: (rowData: { [key: string]: any }) => void;
data: { [key: string]: any };
columnConfigs: IWindowedTableColumns[];
setShowModal: (flag: boolean) => void;
}) => {
const [loading, setLoading] = useState(false);
const [updatedDate, setUpdatedDate] = useState(props.data);
const [updatedData, setUpdatedData] = useState(data);

const handleChange = (fieldName: string, value: string) => {
setUpdatedData((prev) => ({ ...prev, [fieldName]: value }));
};

const handleUpdate = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
try {
const response = await updatePersonData(updatedData as IPersonData);
if (response.success) {
updateRow(updatedData);
setShowModal(false);
} else {
// Handle error appropriately (e.g., show error message)
console.error("Update failed", response.message);
}
} catch (error) {
console.error("Update error", error);
} finally {
setLoading(false);
}
};

return (
<Modal loading={loading} title={"Update Selected Rpw"} onClose={() => props.setShowModal(false)}>
<form className={formElStyle.settingsForm}>
<Modal loading={loading} title={"Update Selected Row"} onClose={() => setShowModal(false)}>
<form className={formElStyle.settingsForm} onSubmit={handleUpdate}>
<div style={{ height: "300px", overflowY: "scroll" }}>
{props.columnConfigs
{columnConfigs
.filter((x) => x.columnType === "data")
.map((column, i) => {
return (
<div key={i} className={formElStyle.element} style={{ marginBottom: "10px" }}>
<label>{column.title}</label>
{column.filterType === "select" && (
<select
name="sortOrder"
defaultValue={props.data[column.fieldName]}
onChange={(e) => {
e.preventDefault();
setUpdatedDate({ ...updatedDate, [column.fieldName]: e.target.value });
}}
>
{column.filterOptions &&
column.filterOptions.map((x, i) => (
<option value={x.value} key={i}>
{x.label}
</option>
))}
</select>
)}
{column.filterType === "input" && (
<input
type={column.filterType}
name={column.fieldName}
defaultValue={props.data[column.fieldName]}
onChange={(e) => {
e.preventDefault();
setUpdatedDate({ ...updatedDate, [column.fieldName]: e.target.value });
}}
/>
)}
</div>
);
})}
.map((column, i) => (
<div key={i} className={formElStyle.element} style={{ marginBottom: "10px" }}>
<label>{column.title}</label>
{column.filterType === "select" ? (
<select
name={column.fieldName}
defaultValue={data[column.fieldName]}
onChange={(e) => handleChange(column.fieldName, e.target.value)}
>
{column.filterOptions?.map((option) => (
<option value={option.value} key={option.value}>
{option.label}
</option>
))}
</select>
) : (
<input
type={column.filterType}
name={column.fieldName}
defaultValue={data[column.fieldName]}
onChange={(e) => handleChange(column.fieldName, e.target.value)}
/>
)}
</div>
))}
</div>
<div className={formElStyle.action}>
<button
onClick={(e) => {
e.preventDefault();
setLoading(true);
updatePersonData(updatedDate as IPersonData).then((response) => {
if (response.success) {
setLoading(false);
props.updateRow(updatedDate);
props.setShowModal(false);
}
});
}}
>
Update
</button>
<button type="submit">Update</button>
<button
className={formElStyle.cancel}
onClick={(e) => {
e.preventDefault();
props.setShowModal(false);
setShowModal(false);
}}
>
Cancel
Expand All @@ -87,44 +92,48 @@ const PersonUpdateModal = (props: {
);
};

export const ColumnActionsForPerson = (props: {
export const ColumnActionsForPerson = ({
rowData,
columnConfigs,
updateRow,
deleteRowData,
}: {
rowData: { [key: string]: any };
columnConfigs: IWindowedTableColumns[];
updateRow: (rowData: { [key: string]: any }) => void;
deleteRowData: (id: string) => void;
}) => {
const [showModal, setShowModal] = useState(false);

const handleDelete = async () => {
try {
const response = await deletePersonData(rowData.id);
if (response.success) {
deleteRowData(rowData.id);
setShowModal(false);
} else {
// Handle error appropriately (e.g., show error message)
console.error("Delete failed", response.message);
}
} catch (error) {
console.error("Delete error", error);
}
};

return (
<div className={style.container}>
{showModal && (
<PersonUpdateModal
columnConfigs={props.columnConfigs}
updateRow={props.updateRow}
data={props.rowData as IPersonData}
columnConfigs={columnConfigs}
updateRow={updateRow}
data={rowData as IPersonData}
setShowModal={setShowModal}
/>
)}
<button
className={style.update}
onClick={() => {
setShowModal(true);
}}
>
<button className={style.update} onClick={() => setShowModal(true)}>
Update
</button>
<button
className={style.cancel}
onClick={() => {
console.log(props.rowData.id);
deletePersonData(props.rowData.id).then((response) => {
if (response.success) {
props.deleteRowData((props.rowData as IPersonData).id);
setShowModal(false);
}
// add logic to refresh table data
});
}}
>
<button className={style.cancel} onClick={handleDelete}>
Delete
</button>
</div>
Expand Down
Loading
Loading