Files
mobile-darmasaba/app/(application)/division/[id]/(fitur-division)/task/update/[detail].tsx
amaliadwiy 6cc5d07017 fix : button header
Deskripsi:
- semua udh custom button header untuk ios 26

NO Issues
2026-01-28 17:42:37 +08:00

317 lines
11 KiB
TypeScript

import AppHeader from "@/components/AppHeader";
import ButtonSaveHeader from "@/components/buttonSaveHeader";
import { InputForm } from "@/components/inputForm";
import ModalAddDetailTugasTask from "@/components/task/modalAddDetailTugasTask";
import Text from "@/components/Text";
import Styles from "@/constants/Styles";
import { apiEditTaskTugas, apiGetTaskTugas } from "@/lib/api";
import { formatDateOnly } from "@/lib/fun_formatDateOnly";
import { getDatesInRange } from "@/lib/fun_getDatesInRange";
import { setUpdateTask } from "@/lib/taskUpdate";
import { useAuthSession } from "@/providers/AuthProvider";
import { useHeaderHeight } from '@react-navigation/elements';
import { router, Stack, useLocalSearchParams } from "expo-router";
import 'intl';
import 'intl/locale-data/jsonp/id';
import moment from "moment";
import { useEffect, useState } from "react";
import {
KeyboardAvoidingView,
Platform,
Pressable,
SafeAreaView,
ScrollView,
View
} from "react-native";
import Toast from "react-native-toast-message";
import DateTimePicker, { DateType } from "react-native-ui-datepicker";
import { useDispatch, useSelector } from "react-redux";
export default function UpdateProjectTaskDivision() {
const headerHeight = useHeaderHeight();
const { detail } = useLocalSearchParams<{ detail: string }>();
const dispatch = useDispatch();
const update = useSelector((state: any) => state.taskUpdate);
const { token, decryptToken } = useAuthSession();
const [range, setRange] = useState<{
startDate: DateType;
endDate: DateType;
}>({ startDate: undefined, endDate: undefined });
const [loadingSubmit, setLoadingSubmit] = useState(false)
const [month, setMonth] = useState<any>();
const [year, setYear] = useState<any>();
const [loading, setLoading] = useState(true);
const [disableBtn, setDisableBtn] = useState(false);
const [dataDetail, setDataDetail] = useState<any>([])
const [modalDetail, setModalDetail] = useState(false)
const [dsbButton, setDsbButton] = useState(true)
const [title, setTitle] = useState("");
const [error, setError] = useState({
startDate: false,
endDate: false,
title: false,
});
const from = formatDateOnly(range.startDate);
const to = formatDateOnly(range.endDate);
async function handleLoad() {
try {
setLoading(true);
const hasil = await decryptToken(String(token?.current));
const response = await apiGetTaskTugas({
user: hasil,
id: detail,
});
setTitle(response.data.title);
setRange({
startDate: new Date(response.data.dateStart),
endDate: new Date(response.data.dateEnd),
});
setMonth(new Date(response.data.dateStart).getMonth());
setYear(new Date(response.data.dateStart).getFullYear());
const response2 = await apiGetTaskTugas({
user: hasil,
id: detail,
cat: "detailTask"
});
if (response2.data.length == 0) {
const datanya = getDatesInRange(response.data.dateStart, response.data.dateEnd)
setDataDetail(datanya.map((item: any) => ({
date: item,
timeStart: null,
timeEnd: null,
})))
} else {
setDataDetail(response2.data)
}
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
}
useEffect(() => {
handleLoad();
}, []);
async function handleEdit() {
try {
setLoadingSubmit(true)
const dataDetailFix = dataDetail.map((item: any) => ({
date: moment(item.date, "DD-MM-YYYY").format("YYYY-MM-DD"),
timeStart: item.timeStart,
timeEnd: item.timeEnd,
}))
const hasil = await decryptToken(String(token?.current));
const response = await apiEditTaskTugas({
data: {
title,
dateStart: formatDateOnly(range.startDate, "YYYY-MM-DD"),
dateEnd: formatDateOnly(range.endDate, "YYYY-MM-DD"),
user: hasil,
dataDetail: dataDetailFix
},
id: detail
});
if (response.success) {
dispatch(setUpdateTask({ ...update, task: !update.task }))
Toast.show({ type: 'small', text1: 'Berhasil mengubah data', })
router.back();
} else {
Toast.show({ type: 'small', text1: response.message, })
}
} catch (error) {
console.error(error);
Toast.show({ type: 'small', text1: 'Terjadi kesalahan', })
} finally {
setLoadingSubmit(false)
}
}
function checkAll() {
if (
from == "" ||
to == "" ||
title == "" ||
title == "null" ||
error.startDate ||
error.endDate ||
error.title
) {
setDisableBtn(true);
} else {
setDisableBtn(false);
}
}
function onValidation(cat: string, val: string) {
if (cat == "title") {
setTitle(val);
if (val == "" || val == "null") {
setError((error) => ({ ...error, title: true }));
} else {
setError((error) => ({ ...error, title: false }));
}
}
}
function checkButton() {
if (range.startDate == null || range.endDate == null || range.startDate == undefined || range.endDate == undefined) {
setDsbButton(true)
setDataDetail([])
} else {
setDsbButton(false)
const awal = formatDateOnly(range.startDate, "YYYY-MM-DD")
const akhir = formatDateOnly(range.endDate, "YYYY-MM-DD")
const datanya = getDatesInRange(awal, akhir)
setDataDetail(datanya.map((item: any) => ({
date: item,
timeStart: null,
timeEnd: null,
})))
}
}
useEffect(() => {
checkAll();
}, [from, to, title, error]);
useEffect(() => {
checkButton()
}, [range])
return (
<SafeAreaView>
<Stack.Screen
options={{
// headerLeft: () => (
// <ButtonBackHeader
// onPress={() => {
// router.back();
// }}
// />
// ),
headerTitle: "Edit Tanggal dan Tugas",
headerTitleAlign: "center",
// headerRight: () => (
// <ButtonSaveHeader
// disable={disableBtn || loadingSubmit}
// category="update"
// onPress={() => {
// handleEdit()
// }}
// />
// ),
header: () => (
<AppHeader
title="Edit Tanggal dan Tugas"
showBack={true}
onPressLeft={() => router.back()}
right={
<ButtonSaveHeader
disable={disableBtn || loadingSubmit}
category="update"
onPress={() => {
handleEdit()
}}
/>
}
/>
)
}}
/>
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : undefined}
keyboardVerticalOffset={headerHeight}
>
<ScrollView>
<View style={[Styles.p15, Styles.mb100]}>
<View style={[Styles.wrapPaper, Styles.p10]}>
{!loading && (
<DateTimePicker
mode="range"
startDate={range.startDate}
endDate={range.endDate}
onChange={(param) => setRange(param)}
month={month}
year={year}
styles={{
selected: Styles.selectedDate,
selected_label: Styles.cWhite,
range_fill: Styles.selectRangeDate,
month_label: Styles.cBlack,
month_selector_label: Styles.cBlack,
year_label: Styles.cBlack,
year_selector_label: Styles.cBlack,
day_label: Styles.cBlack,
time_label: Styles.cBlack,
weekday_label: Styles.cBlack,
}}
/>
)}
</View>
<View style={[Styles.mv10]}>
<View style={[Styles.rowSpaceBetween]}>
<View style={[{ width: "48%" }]}>
<Text style={[Styles.mb05]}>
Tanggal Mulai <Text style={Styles.cError}>*</Text>
</Text>
<View style={[Styles.wrapPaper, Styles.p10]}>
<Text style={{ textAlign: "center" }}>{from}</Text>
</View>
</View>
<View style={[{ width: "48%" }]}>
<Text style={[Styles.mb05]}>
Tanggal Berakhir <Text style={Styles.cError}>*</Text>
</Text>
<View style={[Styles.wrapPaper, Styles.p10]}>
<Text style={{ textAlign: "center" }}>{to}</Text>
</View>
</View>
</View>
{(error.endDate || error.startDate) && (
<Text style={[Styles.textInformation, Styles.cError, Styles.mt05]} >
Tanggal tidak boleh kosong
</Text>
)}
<Pressable
style={[Styles.btnTab, Styles.btnLainnya, dsbButton && Styles.btnDisabled]}
disabled={dsbButton}
onPress={() => { setModalDetail(true) }}
>
<Text style={[dsbButton ? Styles.cGray : Styles.cWhite]}>Detail</Text>
</Pressable>
</View>
<InputForm
label="Judul Tugas"
type="default"
placeholder="Judul Tugas"
required
bg="white"
value={title}
error={error.title}
errorText="Judul tidak boleh kosong"
onChange={(e) => {
onValidation("title", e)
}}
/>
</View>
</ScrollView>
</KeyboardAvoidingView>
<ModalAddDetailTugasTask
isVisible={modalDetail}
setVisible={setModalDetail}
dataTanggal={dataDetail}
onSubmit={(data) => {
setDataDetail(data)
}}
/>
</SafeAreaView>
);
}