sửa lỗi form không reset, cập nhật thêm hàm gọi api khi thêm mới tàu

This commit is contained in:
Tran Anh Tuan
2025-12-10 20:05:34 +07:00
parent 35027a7e23
commit 12fb7c48ed
3 changed files with 19 additions and 5 deletions

View File

@@ -96,6 +96,12 @@ const CreateOrUpdateShip = (props: CreateOrUpdateShipProps) => {
}
}, [props.isOpen, props.initialValue, reset]);
useEffect(() => {
if (props.type === "create") {
reset(props.initialValue);
}
}, [props.isOpen]);
// Prepare options for selects
const shipTypeOptions = useMemo<SelectOption[]>(() => {
return (shipTypes || []).map((type) => ({

View File

@@ -1,6 +1,9 @@
import { ThemedText } from "@/components/themed-text";
import { ThemedView } from "@/components/themed-view";
import { queryUpdateShip } from "@/controller/DeviceController";
import {
queryCreateShip,
queryUpdateShip,
} from "@/controller/DeviceController";
import { useTheme } from "@/hooks/use-theme-context";
import { showSuccessToast } from "@/services/toast_service";
import { useShip } from "@/state/use-ship";
@@ -45,12 +48,14 @@ export default function ShipsScreen() {
setIsCreateShip(false); // Đảm bảo là mode update khi edit
setShowUpdateShip(true);
};
const handleUpdateShip = async (body: Model.ShipBodyRequest) => {
const handleSubmitForm = async (body: Model.ShipBodyRequest) => {
try {
let resp;
if (isCreateShip) {
// TODO: Thêm API create ship khi có
showSuccessToast("Thêm tàu mới thành công");
resp = await queryCreateShip(body);
if (resp.status === 201) {
showSuccessToast("Thêm tàu mới thành công");
}
} else {
resp = await queryUpdateShip(ship!.ship_id, body);
if (resp.status === 200) {
@@ -99,7 +104,7 @@ export default function ShipsScreen() {
initialValue={ship?.body || undefined}
type={isCreateShip ? "create" : "update"}
onClose={() => setShowUpdateShip(false)}
onSubmit={handleUpdateShip}
onSubmit={handleSubmitForm}
/>
</>
);

View File

@@ -31,6 +31,9 @@ export async function queryShipsImage(ship_id: string) {
});
}
export async function queryCreateShip(body: Model.ShipBodyRequest) {
return await api.post(`${API_GET_ALL_SHIP}`, body);
}
export async function queryUpdateShip(
shipId: string,
body: Model.ShipBodyRequest