feat: refactor date handling in ads and news creation/update
This commit is contained in:
parent
fc23f45854
commit
bfa46cdca4
@ -2,6 +2,7 @@ import { z } from 'zod'
|
||||
|
||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||
import type { TAdsSchema } from '~/pages/form-advertisements'
|
||||
import { datePayload } from '~/utils/formatter'
|
||||
|
||||
const advertisementsResponseSchema = z.object({
|
||||
data: z.object({
|
||||
@ -17,8 +18,8 @@ export const createAdsRequest = async (parameters: TParameters) => {
|
||||
const { payload, ...restParameters } = parameters
|
||||
const transformedPayload = {
|
||||
...payload,
|
||||
start_date: new Date(payload.start_date).toISOString(),
|
||||
end_date: new Date(payload.end_date).toISOString(),
|
||||
start_date: datePayload(payload.start_date),
|
||||
end_date: datePayload(payload.end_date),
|
||||
}
|
||||
try {
|
||||
const { data } = await HttpServer(restParameters).post(
|
||||
|
||||
@ -2,6 +2,7 @@ import { z } from 'zod'
|
||||
|
||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||
import type { TContentSchema } from '~/pages/form-contents'
|
||||
import { datePayload } from '~/utils/formatter'
|
||||
|
||||
const newsResponseSchema = z.object({
|
||||
data: z.object({
|
||||
@ -20,7 +21,7 @@ export const createNewsRequest = async (parameters: TParameter) => {
|
||||
...restPayload,
|
||||
categories: categories.map((category) => category?.id),
|
||||
tags: tags?.map((tag) => tag?.id) || [],
|
||||
live_at: new Date(live_at).toISOString(),
|
||||
live_at: datePayload(live_at),
|
||||
}
|
||||
try {
|
||||
const { data } = await HttpServer(restParameters).post(
|
||||
|
||||
@ -2,6 +2,7 @@ import { z } from 'zod'
|
||||
|
||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||
import type { TAdsSchema } from '~/pages/form-advertisements'
|
||||
import { datePayload } from '~/utils/formatter'
|
||||
|
||||
const advertisementsResponseSchema = z.object({
|
||||
data: z.object({
|
||||
@ -18,8 +19,8 @@ export const updateAdsRequest = async (parameters: TParameters) => {
|
||||
const { id, ...restPayload } = payload
|
||||
const transformedPayload = {
|
||||
...restPayload,
|
||||
start_date: new Date(payload.start_date).toISOString(),
|
||||
end_date: new Date(payload.end_date).toISOString(),
|
||||
start_date: datePayload(payload.start_date),
|
||||
end_date: datePayload(payload.end_date),
|
||||
}
|
||||
try {
|
||||
const { data } = await HttpServer(restParameters).put(
|
||||
|
||||
@ -2,6 +2,7 @@ import { z } from 'zod'
|
||||
|
||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||
import type { TContentSchema } from '~/pages/form-contents'
|
||||
import { datePayload } from '~/utils/formatter'
|
||||
|
||||
const newsResponseSchema = z.object({
|
||||
data: z.object({
|
||||
@ -20,7 +21,7 @@ export const updateNewsRequest = async (parameters: TParameter) => {
|
||||
...restPayload,
|
||||
categories: categories.map((category) => category?.id),
|
||||
tags: tags?.map((tag) => tag?.id) || [],
|
||||
live_at: new Date(live_at).toISOString(),
|
||||
live_at: datePayload(live_at),
|
||||
}
|
||||
try {
|
||||
const { data } = await HttpServer(restParameters).put(
|
||||
|
||||
@ -10,6 +10,7 @@ import { Button } from '~/components/ui/button'
|
||||
import { Input } from '~/components/ui/input'
|
||||
import { InputFile } from '~/components/ui/input-file'
|
||||
import { TitleDashboard } from '~/components/ui/title-dashboard'
|
||||
import { dateInput } from '~/utils/formatter'
|
||||
|
||||
export const adsSchema = z.object({
|
||||
id: z.string().optional(),
|
||||
@ -43,12 +44,8 @@ export const FormAdvertisementsPage = (properties: TProperties) => {
|
||||
id: adData?.id || undefined,
|
||||
image: adData?.image_url || '',
|
||||
url: adData?.url || '',
|
||||
start_date: adData?.start_date
|
||||
? new Date(adData.start_date).toISOString().split('T')[0]
|
||||
: '',
|
||||
end_date: adData?.end_date
|
||||
? new Date(adData.end_date).toISOString().split('T')[0]
|
||||
: '',
|
||||
start_date: adData?.start_date ? dateInput(adData.start_date) : '',
|
||||
end_date: adData?.end_date ? dateInput(adData.end_date) : '',
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@ -15,6 +15,7 @@ import { InputFile } from '~/components/ui/input-file'
|
||||
import { Switch } from '~/components/ui/switch'
|
||||
import { TitleDashboard } from '~/components/ui/title-dashboard'
|
||||
import type { loader } from '~/routes/_admin.lg-admin._dashboard'
|
||||
import { dateInput } from '~/utils/formatter'
|
||||
|
||||
export const contentSchema = z.object({
|
||||
id: z.string().optional(),
|
||||
@ -86,9 +87,7 @@ export const FormContentsPage = (properties: TProperties) => {
|
||||
content: newsData?.content || '',
|
||||
featured_image: newsData?.featured_image || '',
|
||||
is_premium: newsData?.is_premium || false,
|
||||
live_at: newsData?.live_at
|
||||
? new Date(newsData.live_at).toISOString().split('T')[0]
|
||||
: '',
|
||||
live_at: newsData?.live_at ? dateInput(newsData.live_at) : '',
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@ -11,6 +11,12 @@ export const formatDate = (isoDate: string): string => {
|
||||
}).format(date)
|
||||
}
|
||||
|
||||
export const dateInput = (isoDate: string): string =>
|
||||
new Date(isoDate).toISOString().split('T')[0]
|
||||
|
||||
export const datePayload = (date: string): string =>
|
||||
new Date(date).toISOString()
|
||||
|
||||
export const urlFriendlyCode = (input: string) => {
|
||||
return input
|
||||
.trim()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user