Fix: - (application)/(user)/donation/[id]/(news)/[news]/edit-news - (application)/(user)/donation/[id]/(news)/[news]/index - (application)/(user)/donation/[id]/(news)/add-news - (application)/(user)/donation/[id]/(news)/list-of-news - (application)/(user)/donation/[id]/(news)/recap-of-news - (application)/(user)/donation/[id]/infromation-fundrising - service/api-client/api-donation ### No Issue
133 lines
3.2 KiB
TypeScript
133 lines
3.2 KiB
TypeScript
import {
|
|
ButtonCenteredOnly,
|
|
ButtonCustom,
|
|
InformationBox,
|
|
LandscapeFrameUploaded,
|
|
Spacing,
|
|
StackCustom,
|
|
TextAreaCustom,
|
|
TextInputCustom,
|
|
ViewWrapper,
|
|
} from "@/components";
|
|
import DIRECTORY_ID from "@/constants/directory-id";
|
|
import { apiDonationCreateNews } from "@/service/api-client/api-donation";
|
|
import { uploadFileService } from "@/service/upload-service";
|
|
import pickFile, { IFileData } from "@/utils/pickFile";
|
|
import { router, useLocalSearchParams } from "expo-router";
|
|
import { useState } from "react";
|
|
import Toast from "react-native-toast-message";
|
|
|
|
export default function DonationAddNews() {
|
|
const { id } = useLocalSearchParams();
|
|
const [data, setData] = useState({
|
|
title: "",
|
|
deskripsi: "",
|
|
});
|
|
const [image, setImage] = useState<IFileData | null>(null);
|
|
const [isLoading, setLoading] = useState(false);
|
|
|
|
const handlerSubmit = async () => {
|
|
let newData: any = { ...data };
|
|
try {
|
|
setLoading(true);
|
|
if (image) {
|
|
const responseUploadImage = await uploadFileService({
|
|
dirId: DIRECTORY_ID.donasi_kabar,
|
|
imageUri: image?.uri,
|
|
});
|
|
|
|
newData = {
|
|
...newData,
|
|
imageId: responseUploadImage.data.id,
|
|
};
|
|
}
|
|
|
|
const response = await apiDonationCreateNews({
|
|
id: id as string,
|
|
data: newData,
|
|
});
|
|
|
|
if (!response.success) {
|
|
Toast.show({
|
|
type: "error",
|
|
text1: "Gagal menambah berita",
|
|
});
|
|
|
|
return
|
|
}
|
|
|
|
Toast.show({
|
|
type: "success",
|
|
text1: "Berita berhasil ditambahkan",
|
|
});
|
|
|
|
router.back();
|
|
} catch (error) {
|
|
console.log("[ERROR]", error);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<ViewWrapper>
|
|
<StackCustom gap={"xs"}>
|
|
<InformationBox text="Upload gambar bersifat opsional untuk melengkapi kabar terkait donasi Anda." />
|
|
<LandscapeFrameUploaded image={image?.uri} />
|
|
<ButtonCenteredOnly
|
|
onPress={() => {
|
|
pickFile({
|
|
allowedType: "image",
|
|
setImageUri(file) {
|
|
setImage(file);
|
|
},
|
|
});
|
|
}}
|
|
icon="upload"
|
|
>
|
|
Upload
|
|
</ButtonCenteredOnly>
|
|
<Spacing />
|
|
<TextInputCustom
|
|
label="Judul Berita"
|
|
placeholder="Masukan judul berita"
|
|
required
|
|
value={data.title}
|
|
onChangeText={(value) => {
|
|
setData({
|
|
...data,
|
|
title: value,
|
|
});
|
|
}}
|
|
/>
|
|
<TextAreaCustom
|
|
label="Deskripsi Berita"
|
|
placeholder="Masukan deskripsi berita"
|
|
required
|
|
showCount
|
|
maxLength={1000}
|
|
value={data.deskripsi}
|
|
onChangeText={(value) => {
|
|
setData({
|
|
...data,
|
|
deskripsi: value,
|
|
});
|
|
}}
|
|
/>
|
|
|
|
<Spacing />
|
|
<ButtonCustom
|
|
disabled={!data.title || !data.deskripsi}
|
|
isLoading={isLoading}
|
|
onPress={() => {
|
|
handlerSubmit();
|
|
}}
|
|
>
|
|
Simpan
|
|
</ButtonCustom>
|
|
</StackCustom>
|
|
<Spacing />
|
|
</ViewWrapper>
|
|
);
|
|
}
|