Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/api/v2/barometers/[slug]/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const getBarometer = withPrisma(async (prisma, slug: string) => {
slug: true,
},
},
images: true,
},
},
images: {
Expand Down
9 changes: 9 additions & 0 deletions app/api/v2/manufacturers/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ export const getManufacturers = withPrisma(async (prisma, page: number, pageSize
take: pageSize || undefined,
include: {
countries: true,
images: {
select: {
url: true,
id: true,
blurData: true,
order: true,
name: true,
},
},
predecessors: {
select: {
id: true,
Expand Down
80 changes: 52 additions & 28 deletions app/api/v2/manufacturers/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { FrontRoutes } from '@/utils/routes-front'
interface ManufacturerDTO extends Manufacturer {
successors?: { id: string }[]
countries?: { id: number }[]
images?: { id: string; url: string; blurData: string }[]
}
/**
* Retrieve a list of all Manufacturers
Expand All @@ -35,7 +36,7 @@ export async function GET(req: NextRequest) {
*/
export const POST = withPrisma(async (prisma, req: NextRequest) => {
try {
const { successors, countries, ...manufData }: ManufacturerDTO = await req
const { successors, countries, images, ...manufData }: ManufacturerDTO = await req
.json()
.then(cleanObject)

Expand All @@ -57,6 +58,13 @@ export const POST = withPrisma(async (prisma, req: NextRequest) => {
},
}
: {}),
...(images
? {
images: {
create: images,
},
}
: {}),
},
})

Expand All @@ -77,12 +85,14 @@ export const POST = withPrisma(async (prisma, req: NextRequest) => {
*/
export const PUT = withPrisma(async (prisma, req: NextRequest) => {
try {
const { successors, countries, ...manufData }: ManufacturerDTO = await req.json().then(data =>
// replace empty strings with NULLs
traverse.map(data, function map(node) {
if (node === '') this.update(null)
}),
)
const { successors, countries, images, ...manufData }: ManufacturerDTO = await req
.json()
.then(data =>
// replace empty strings with NULLs
traverse.map(data, function map(node) {
if (node === '') this.update(null)
}),
)
const manufacturer = await prisma.manufacturer.findUnique({ where: { id: manufData.id } })
if (!manufacturer) {
return NextResponse.json({ message: 'Manufacturer not found' }, { status: 404 })
Expand All @@ -91,32 +101,46 @@ export const PUT = withPrisma(async (prisma, req: NextRequest) => {
const slug = manufData.name
? getBrandSlug(manufData.name, manufData.firstName)
: manufacturer.slug
const updatedManufacturer = await prisma.manufacturer.update({
where: { id: manufacturer.id },
data: {
...manufData,
...(successors
? {
successors: {
set: successors,
},
}
: {}),
...(countries
? {
countries: {
set: countries,
},
}
: {}),
slug,
},
await prisma.$transaction(async tx => {
await Promise.all([
// delete old images if the new ones are provided
images
? tx.image.deleteMany({ where: { barometers: { some: { id: manufacturer.id } } } })
: Promise.resolve(),
await tx.manufacturer.update({
where: { id: manufacturer.id },
data: {
...manufData,
...(successors
? {
successors: {
set: successors,
},
}
: {}),
...(countries
? {
countries: {
set: countries,
},
}
: {}),
images: images
? {
deleteMany: {},
create: images,
}
: {},
slug,
},
}),
])
})

revalidatePath(trimTrailingSlash(FrontRoutes.Brands))
revalidatePath(FrontRoutes.Brands + slug)
await revalidateSuccessors(successors)
return NextResponse.json(updatedManufacturer, { status: 200 })
return NextResponse.json({ slug }, { status: 200 })
} catch (error) {
console.error(error)
return NextResponse.json(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
import { useEffect, useMemo, useState } from 'react'
import { useDisclosure } from '@mantine/hooks'
import { BarometerDTO } from '@/app/types'
import sx from './styles.module.scss'
import { googleStorageImagesFolder } from '@/utils/constants'
import { FrontRoutes } from '@/utils/routes-front'
import { showError, showInfo } from '@/utils/notification'
Expand Down Expand Up @@ -76,7 +75,7 @@ function SortableImage({
/>
<Box {...listeners}>
<NextImage
className={sx.thumbnail}
className="h-auto w-auto"
alt="Barometer"
key={image}
src={googleStorageImagesFolder + image}
Expand Down Expand Up @@ -113,7 +112,7 @@ export function ImagesEdit({ barometer, size, ...props }: ImagesEditProps) {
form.setFieldValue('images', newOrder)
}
}
const updateBarometerWithImages = async (values: FormProps) => {
const update = async (values: FormProps) => {
// exit if no image was changed
if (isEqual(values.images, barometer.images)) {
close()
Expand Down Expand Up @@ -227,16 +226,16 @@ export function ImagesEdit({ barometer, size, ...props }: ImagesEditProps) {
centered
opened={opened}
onClose={onClose}
classNames={{ title: sx.imageEditModalTitle }}
styles={{ title: { fontWeight: 500, fontSize: '22px' } }}
>
<Box pos="relative" component="form" onSubmit={form.onSubmit(updateBarometerWithImages)}>
<Box pos="relative" component="form" onSubmit={form.onSubmit(update)}>
<LoadingOverlay visible={isUploading} zIndex={100} />
<Stack>
<FileButton multiple onChange={uploadImages} accept="image/**">
{fbProps => (
<Button
color="dark.4"
className={sx.addImageBtn}
className="self-start"
leftSection={<IconPhotoPlus />}
{...fbProps}
>
Expand All @@ -262,7 +261,7 @@ export function ImagesEdit({ barometer, size, ...props }: ImagesEditProps) {
</Box>
</Modal>
<Tooltip label="Edit images">
<UnstyledButton className={sx.imageEdit} {...props} onClick={open}>
<UnstyledButton className="absolute right-20 top-0 z-10" {...props} onClick={open}>
<IconEdit color="brown" size={size} />
</UnstyledButton>
</Tooltip>
Expand Down
Loading
Loading