108 lines
3.4 KiB
TypeScript
108 lines
3.4 KiB
TypeScript
import ButtonBackHeader from "@/components/buttonBackHeader";
|
|
import ButtonSaveHeader from "@/components/buttonSaveHeader";
|
|
import { InputForm } from "@/components/inputForm";
|
|
import Styles from "@/constants/Styles";
|
|
import { apiCancelTask } from "@/lib/api";
|
|
import { setUpdateTask } from "@/lib/taskUpdate";
|
|
import { useAuthSession } from "@/providers/AuthProvider";
|
|
import { router, Stack, useLocalSearchParams } from "expo-router";
|
|
import { useEffect, useState } from "react";
|
|
import { SafeAreaView, ScrollView, View } from "react-native";
|
|
import Toast from "react-native-toast-message";
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
|
|
export default function TaskDivisionCancel() {
|
|
const { id, detail } = useLocalSearchParams<{ id: string; detail: string }>();
|
|
const { token, decryptToken } = useAuthSession();
|
|
const dispatch = useDispatch();
|
|
const update = useSelector((state: any) => state.taskUpdate);
|
|
const [reason, setReason] = useState("");
|
|
const [error, setError] = useState(false);
|
|
const [disable, setDisable] = useState(false);
|
|
|
|
function onValidation(val: string) {
|
|
setReason(val);
|
|
if (val == "" || val == "null") {
|
|
setError(true);
|
|
} else {
|
|
setError(false);
|
|
}
|
|
}
|
|
|
|
function checkAll() {
|
|
if (reason == "" || reason == "null" || error) {
|
|
setDisable(true);
|
|
} else {
|
|
setDisable(false);
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
checkAll();
|
|
}, [reason, error]);
|
|
|
|
async function handleCancel() {
|
|
try {
|
|
const hasil = await decryptToken(String(token?.current));
|
|
const response = await apiCancelTask(
|
|
{
|
|
reason: reason,
|
|
user: hasil,
|
|
},
|
|
detail
|
|
);
|
|
if (response.success) {
|
|
dispatch(setUpdateTask({ ...update, data: !update.data }));
|
|
Toast.show({ type: 'small', text1: 'Berhasil membatalkan kegiatan', })
|
|
router.back();
|
|
} else {
|
|
Toast.show({ type: 'small', text1: response.message, })
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
Toast.show({ type: 'small', text1: 'Gagal membatalkan kegiatan', })
|
|
}
|
|
}
|
|
|
|
return (
|
|
<SafeAreaView>
|
|
<Stack.Screen
|
|
options={{
|
|
headerLeft: () => (
|
|
<ButtonBackHeader
|
|
onPress={() => {
|
|
router.back();
|
|
}}
|
|
/>
|
|
),
|
|
headerTitle: "Pembatalan Tugas",
|
|
headerTitleAlign: "center",
|
|
headerRight: () => (
|
|
<ButtonSaveHeader
|
|
disable={disable}
|
|
category="cancel"
|
|
onPress={() => {
|
|
handleCancel();
|
|
}}
|
|
/>
|
|
),
|
|
}}
|
|
/>
|
|
<ScrollView>
|
|
<View style={[Styles.p15, Styles.mb100]}>
|
|
<InputForm
|
|
label="Alasan Pembatalan"
|
|
type="default"
|
|
placeholder="Alasan Pembatalan"
|
|
required
|
|
bg="white"
|
|
error={error}
|
|
errorText="Alasan pembatalan harus diisi"
|
|
onChange={(val) => onValidation(val)}
|
|
/>
|
|
</View>
|
|
</ScrollView>
|
|
</SafeAreaView>
|
|
);
|
|
}
|