Merge branch 'dev' of github.com:dikstub-rssa/simrs-fe into feat/education-assessment-79
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import type { ClassValue } from 'clsx'
|
||||
import { clsx } from 'clsx'
|
||||
import { format } from 'date-fns'
|
||||
import { twMerge } from 'tailwind-merge'
|
||||
import { toast } from '~/components/pub/ui/toast'
|
||||
|
||||
export interface SelectOptionType<_T = string> {
|
||||
value: string
|
||||
@@ -116,3 +118,64 @@ export function calculateAge(birthDate: Date | string | null | undefined): strin
|
||||
return `${years} tahun ${months} bulan`
|
||||
}
|
||||
}
|
||||
|
||||
export function formatDateToDatetimeLocal(inputDate: Date) {
|
||||
const formattedString = format(inputDate, "yyyy-MM-dd'T'HH:mm")
|
||||
return formattedString
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts a plain JavaScript object (including File objects) into a FormData instance.
|
||||
* @param {object} data - The object to convert (e.g., form values).
|
||||
* @returns {FormData} The new FormData object suitable for API submission.
|
||||
*/
|
||||
export function toFormData(data: Record<string, any>): FormData {
|
||||
const formData = new FormData();
|
||||
|
||||
for (const key in data) {
|
||||
if (Object.prototype.hasOwnProperty.call(data, key)) {
|
||||
const value = data[key];
|
||||
|
||||
// Handle File objects, Blobs, or standard JSON values
|
||||
if (value !== null && value !== undefined) {
|
||||
// Check if the value is a File/Blob instance
|
||||
if (value instanceof File || value instanceof Blob) {
|
||||
// Append the file directly
|
||||
formData.append(key, value);
|
||||
} else if (typeof value === 'object') {
|
||||
// Handle nested objects/arrays by stringifying them (optional, depends on API)
|
||||
// Note: Most APIs expect nested data to be handled separately or passed as JSON string
|
||||
// For simplicity, we stringify non-File objects.
|
||||
formData.append(key, JSON.stringify(value));
|
||||
} else {
|
||||
// Append standard string, number, or boolean values
|
||||
formData.append(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return formData;
|
||||
}
|
||||
|
||||
export function printFormData(formData: FormData) {
|
||||
console.log("--- FormData Contents ---");
|
||||
// Use the entries() iterator to loop through key/value pairs
|
||||
for (const [key, value] of formData.entries()) {
|
||||
if (value instanceof File) {
|
||||
console.log(`Key: ${key}, Value: [File: ${value.name}, Type: ${value.type}, Size: ${value.size} bytes]`);
|
||||
} else {
|
||||
console.log(`Key: ${key}, Value: "${value}"`);
|
||||
}
|
||||
}
|
||||
console.log("-------------------------");
|
||||
}
|
||||
|
||||
export function unauthorizedToast() {
|
||||
toast({
|
||||
title: 'Unauthorized',
|
||||
description: 'You are not authorized to perform this action.',
|
||||
variant: 'destructive',
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user