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]); }, [props.isOpen, props.initialValue, reset]);
useEffect(() => {
if (props.type === "create") {
reset(props.initialValue);
}
}, [props.isOpen]);
// Prepare options for selects // Prepare options for selects
const shipTypeOptions = useMemo<SelectOption[]>(() => { const shipTypeOptions = useMemo<SelectOption[]>(() => {
return (shipTypes || []).map((type) => ({ return (shipTypes || []).map((type) => ({

View File

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