diff --git a/src/app_modules/katalog/component/regular_expressions.ts b/src/app_modules/katalog/component/regular_expressions.ts index bb809be0..e4e9d38b 100644 --- a/src/app_modules/katalog/component/regular_expressions.ts +++ b/src/app_modules/katalog/component/regular_expressions.ts @@ -1,3 +1,3 @@ export var validRegex = /^([a-zA-Z0-9\.!#$%&'*+/=?^_`{|}~-]+)@([a-zA-Z0-9])+.([a-z]+)(.[a-z]+)?$/; -export const gmailRegex = /^[a-zA-Z0-9._%+-]+@gmail\.com$/; \ No newline at end of file +export const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; \ No newline at end of file diff --git a/src/app_modules/katalog/profile/_component/button/comp_button_update_background_profile.tsx b/src/app_modules/katalog/profile/_component/button/comp_button_update_background_profile.tsx index bb947e84..59d5f371 100644 --- a/src/app_modules/katalog/profile/_component/button/comp_button_update_background_profile.tsx +++ b/src/app_modules/katalog/profile/_component/button/comp_button_update_background_profile.tsx @@ -29,16 +29,8 @@ export function Profile_ComponentButtonUpdateBackgroundProfile({ async function onUpdate() { try { setLoading(true); - const deletePhoto = await funGlobal_DeleteFileById({ - fileId: fileId, - dirId: DIRECTORY_ID.profile_background, - }); - - if (!deletePhoto.success) { - ComponentGlobal_NotifikasiPeringatan("Gagal update background"); - return; - } + // Upload gambar baru const uploadFile = await funGlobal_UploadToStorage({ file: file, dirId: DIRECTORY_ID.profile_background, @@ -46,9 +38,19 @@ export function Profile_ComponentButtonUpdateBackgroundProfile({ if (!uploadFile.success) { setLoading(false); - return ComponentGlobal_NotifikasiPeringatan( - "Gagal upload background" - ); + ComponentGlobal_NotifikasiPeringatan("Gagal upload background"); + return; + } + + // Hapus gambar lama + const deletePhoto = await funGlobal_DeleteFileById({ + fileId: fileId, + dirId: DIRECTORY_ID.profile_background, + }); + + if (!deletePhoto.success) { + setLoading(false); + clientLogger.error("Error delete background", deletePhoto.message); } const res = await profile_funUpdateBackground({ @@ -60,12 +62,12 @@ export function Profile_ComponentButtonUpdateBackgroundProfile({ ComponentGlobal_NotifikasiBerhasil(res.message); router.back(); } else { + setLoading(false); ComponentGlobal_NotifikasiGagal(res.message); } } catch (error) { - clientLogger.error("Error upload background", error); - } finally { setLoading(false); + clientLogger.error("Error upload background", error); } } diff --git a/src/app_modules/katalog/profile/_component/button/comp_button_update_photo_profile.tsx b/src/app_modules/katalog/profile/_component/button/comp_button_update_photo_profile.tsx index 0b01993b..a36365f2 100644 --- a/src/app_modules/katalog/profile/_component/button/comp_button_update_photo_profile.tsx +++ b/src/app_modules/katalog/profile/_component/button/comp_button_update_photo_profile.tsx @@ -1,21 +1,21 @@ "use client"; +import { DIRECTORY_ID } from "@/app/lib"; import { MainColor } from "@/app_modules/_global/color"; +import { + funGlobal_DeleteFileById, + funGlobal_UploadToStorage, +} from "@/app_modules/_global/fun"; import { ComponentGlobal_NotifikasiBerhasil, ComponentGlobal_NotifikasiGagal, ComponentGlobal_NotifikasiPeringatan, } from "@/app_modules/_global/notif_global"; -import { Box, Button, Center } from "@mantine/core"; +import { clientLogger } from "@/util/clientLogger"; +import { Box, Button } from "@mantine/core"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { profile_funUpdatePhoto } from "../../fun"; -import { - funGlobal_DeleteFileById, - funGlobal_UploadToStorage, -} from "@/app_modules/_global/fun"; -import { DIRECTORY_ID } from "@/app/lib"; -import { clientLogger } from "@/util/clientLogger"; export function Profile_ComponentButtonUpdatePhotoProfile({ file, @@ -31,16 +31,8 @@ export function Profile_ComponentButtonUpdatePhotoProfile({ async function onUpdate() { try { setLoading(true); - const deletePhoto = await funGlobal_DeleteFileById({ - fileId: fileId, - dirId: DIRECTORY_ID.profile_foto, - }); - - if (!deletePhoto.success) { - setLoading(false); - clientLogger.error("Error delete logo", deletePhoto.message); - } + // Upload foto baru const uploadPhoto = await funGlobal_UploadToStorage({ file: file, dirId: DIRECTORY_ID.profile_foto, @@ -52,6 +44,17 @@ export function Profile_ComponentButtonUpdatePhotoProfile({ return; } + // Hapus gambar lama + const deletePhoto = await funGlobal_DeleteFileById({ + fileId: fileId, + dirId: DIRECTORY_ID.profile_foto, + }); + + if (!deletePhoto.success) { + setLoading(false); + clientLogger.error("Error delete logo", deletePhoto.message); + } + const res = await profile_funUpdatePhoto({ fileId: uploadPhoto.data.id, profileId: profileId, diff --git a/src/app_modules/katalog/profile/_component/button/comp_create_new_profile.tsx b/src/app_modules/katalog/profile/_component/button/comp_create_new_profile.tsx index ccb8fc6a..08f2fcc9 100644 --- a/src/app_modules/katalog/profile/_component/button/comp_create_new_profile.tsx +++ b/src/app_modules/katalog/profile/_component/button/comp_create_new_profile.tsx @@ -7,7 +7,7 @@ import { ComponentGlobal_NotifikasiGagal, ComponentGlobal_NotifikasiPeringatan, } from "@/app_modules/_global/notif_global"; -import { gmailRegex } from "@/app_modules/katalog/component/regular_expressions"; +import { emailRegex } from "@/app_modules/katalog/component/regular_expressions"; import { Button } from "@mantine/core"; import _ from "lodash"; import { useRouter } from "next/navigation"; @@ -36,7 +36,7 @@ export function Profile_ComponentCreateNewProfile({ }; if (_.values(newData).includes("")) return ComponentGlobal_NotifikasiPeringatan("Lengkapi Data"); - if (!newData.email.match(gmailRegex)) + if (!newData.email.match(emailRegex)) return ComponentGlobal_NotifikasiPeringatan("Format email salah"); if (fotoProfileId == "") diff --git a/src/app_modules/katalog/profile/create/view.tsx b/src/app_modules/katalog/profile/create/view.tsx index 77feb0b2..7751a451 100644 --- a/src/app_modules/katalog/profile/create/view.tsx +++ b/src/app_modules/katalog/profile/create/view.tsx @@ -4,7 +4,7 @@ import { ComponentGlobal_ErrorInput } from "@/app_modules/_global/component"; import { Select, Stack, TextInput } from "@mantine/core"; import { IconAt } from "@tabler/icons-react"; import { useState } from "react"; -import { gmailRegex } from "../../component/regular_expressions"; +import { emailRegex } from "../../component/regular_expressions"; import { Profile_ComponentCreateNewProfile } from "../_component"; import Profile_ViewUploadBackground from "./view_upload_background"; import Profile_ViewUploadFoto from "./view_upload_foto"; @@ -70,7 +70,7 @@ export default function CreateProfile() { maxLength={100} placeholder="Contoh: User@gmail.com" error={ - value.email.length > 0 && !value.email.match(gmailRegex) ? ( + value.email.length > 0 && !value.email.match(emailRegex) ? ( ) : ( "" diff --git a/src/app_modules/katalog/profile/edit/view.tsx b/src/app_modules/katalog/profile/edit/view.tsx index 471499c7..8ac1e38e 100644 --- a/src/app_modules/katalog/profile/edit/view.tsx +++ b/src/app_modules/katalog/profile/edit/view.tsx @@ -10,7 +10,7 @@ import { Button, Loader, Select, Stack, TextInput } from "@mantine/core"; import _ from "lodash"; import { useRouter } from "next/navigation"; import { useState } from "react"; -import { gmailRegex } from "../../component/regular_expressions"; +import { emailRegex } from "../../component/regular_expressions"; import { Profile_funEditById } from "../fun/update/fun_edit_profile_by_id"; import { MODEL_PROFILE } from "../model/interface"; @@ -27,7 +27,7 @@ export default function EditProfile({ data }: { data: MODEL_PROFILE }) { // console.log(body) if (_.values(body).includes("")) return ComponentGlobal_NotifikasiPeringatan("Lengkapi data"); - if (!body.email.match(gmailRegex)) + if (!body.email.match(emailRegex)) return ComponentGlobal_NotifikasiPeringatan("Format email salah"); try { @@ -139,7 +139,7 @@ export default function EditProfile({ data }: { data: MODEL_PROFILE }) { dataProfile?.email === "" ? ( ) : dataProfile?.email?.length > 0 && - !dataProfile?.email.match(gmailRegex) ? ( + !dataProfile?.email.match(emailRegex) ? ( ) : ( ""