119 lines
3.5 KiB
TypeScript
119 lines
3.5 KiB
TypeScript
import ButtonBackHeader from "@/components/buttonBackHeader";
|
|
import ButtonSaveHeader from "@/components/buttonSaveHeader";
|
|
import { InputForm } from "@/components/inputForm";
|
|
import Styles from "@/constants/Styles";
|
|
import { apiEditProject, apiGetProjectOne } from "@/lib/api";
|
|
import { setUpdateProject } from "@/lib/projectUpdate";
|
|
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 EditProject() {
|
|
const { token, decryptToken } = useAuthSession();
|
|
const { id } = useLocalSearchParams<{ id: string }>();
|
|
const dispatch = useDispatch()
|
|
const update = useSelector((state: any) => state.projectUpdate)
|
|
const [judul, setJudul] = useState("");
|
|
const [error, setError] = useState(false);
|
|
const [disable, setDisable] = useState(false);
|
|
|
|
async function handleLoad() {
|
|
try {
|
|
const hasil = await decryptToken(String(token?.current));
|
|
const response = await apiGetProjectOne({
|
|
user: hasil,
|
|
cat: "data",
|
|
id: id,
|
|
});
|
|
setJudul(response.data.title);
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
handleLoad();
|
|
}, []);
|
|
|
|
function onValidation(val: string) {
|
|
setJudul(val)
|
|
if (val == "" || val == "null") {
|
|
setError(true)
|
|
}
|
|
}
|
|
|
|
function checkAll() {
|
|
if (judul == "" || judul == "null" || error) {
|
|
setDisable(true)
|
|
} else {
|
|
setDisable(false)
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
checkAll()
|
|
}, [judul, error]);
|
|
|
|
async function handleUpdate() {
|
|
try {
|
|
const hasil = await decryptToken(String(token?.current));
|
|
const response = await apiEditProject({
|
|
name: judul,
|
|
user: hasil,
|
|
}, id);
|
|
if (response.success) {
|
|
dispatch(setUpdateProject({ ...update, data: !update.data }))
|
|
Toast.show({ type: 'small', text1: 'Berhasil mengubah data', })
|
|
router.back();
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
<SafeAreaView>
|
|
<Stack.Screen
|
|
options={{
|
|
headerLeft: () => (
|
|
<ButtonBackHeader
|
|
onPress={() => {
|
|
router.back();
|
|
}}
|
|
/>
|
|
),
|
|
headerTitle: "Edit Judul Kegiatan",
|
|
headerTitleAlign: "center",
|
|
headerRight: () => (
|
|
<ButtonSaveHeader
|
|
disable={disable}
|
|
category="update"
|
|
onPress={() => { handleUpdate() }}
|
|
/>
|
|
),
|
|
}}
|
|
/>
|
|
<ScrollView>
|
|
<View style={[Styles.p15, Styles.mb100]}>
|
|
<InputForm
|
|
label="Judul Kegiatan"
|
|
type="default"
|
|
placeholder="Judul Kegiatan"
|
|
required
|
|
bg="white"
|
|
value={judul}
|
|
onChange={(val) => { onValidation(val) }}
|
|
error={error}
|
|
errorText="Judul Kegiatan harus diisi"
|
|
/>
|
|
</View>
|
|
</ScrollView>
|
|
</SafeAreaView>
|
|
);
|
|
}
|