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