-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from goodeats/28-polymorphic-designs-on-artboa…
…rd-pt-2 polymorphic designs on artboard pt 2
- Loading branch information
Showing
31 changed files
with
1,501 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { type Design } from '@prisma/client' | ||
import { | ||
type selectArgsType, | ||
type findDesignArgsType, | ||
type whereArgsType, | ||
} from '#app/schema/design' | ||
import { prisma } from '#app/utils/db.server' | ||
import { type IPalette } from './palette.server' | ||
|
||
export interface IDesignWithType { | ||
id: string | ||
type: string | ||
visible: boolean | ||
createdAt: Date | string | ||
nextId: string | null | ||
prevId: string | null | ||
ownerId: string | ||
artboardId: string | null | ||
palette: IPalette | null | ||
} | ||
|
||
export interface IDesignWithPalette extends IDesignWithType { | ||
palette: IPalette | ||
} | ||
|
||
export const findManyDesignsWithType = async ({ | ||
where, | ||
}: { | ||
where: whereArgsType | ||
}) => { | ||
const designs = await prisma.design.findMany({ | ||
where, | ||
include: { | ||
palette: true, | ||
}, | ||
orderBy: { | ||
type: 'asc', | ||
}, | ||
}) | ||
return designs | ||
} | ||
|
||
export const findFirstDesign = async ({ | ||
where, | ||
select, | ||
}: findDesignArgsType): Promise<Design | null> => { | ||
return await prisma.design.findFirst({ | ||
where, | ||
select, | ||
}) | ||
} | ||
|
||
export const findDesignByIdAndOwner = async ({ | ||
id, | ||
ownerId, | ||
select, | ||
}: { | ||
id: whereArgsType['id'] | ||
ownerId: whereArgsType['ownerId'] | ||
select?: selectArgsType | ||
}): Promise<Design | null> => { | ||
const where = { id, ownerId } | ||
return await findFirstDesign({ where, select }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface IPalette { | ||
id: string | ||
format: string | ||
value: string | ||
opacity: number | ||
createdAt: Date | string | ||
updatedAt: Date | string | ||
designId: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
app/routes/sketch+/artboards+/$slug_+/actions/artboard-design-palette.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { json } from '@remix-run/node' | ||
import { type IntentActionArgs } from '#app/definitions/intent-action-args' | ||
import { NewArtboardDesignSchema, designSchema } from '#app/schema/design' | ||
import { EditArtboardPaletteSchema } from '#app/schema/palette' | ||
import { | ||
notSubmissionResponse, | ||
submissionErrorResponse, | ||
} from '#app/utils/conform-utils' | ||
import { prisma } from '#app/utils/db.server' | ||
import { findFirstPaletteInstance } from '#app/utils/prisma-extensions-palette' | ||
import { parseArtboardDesignSubmission } from './utils' | ||
|
||
export async function artboardDesignNewPaletteAction({ | ||
userId, | ||
formData, | ||
}: IntentActionArgs) { | ||
// validation | ||
const submission = await parseArtboardDesignSubmission({ | ||
userId, | ||
formData, | ||
schema: NewArtboardDesignSchema, | ||
}) | ||
if (submission.intent !== 'submit') { | ||
return notSubmissionResponse(submission) | ||
} | ||
if (!submission.value) { | ||
return submissionErrorResponse(submission) | ||
} | ||
|
||
// changes | ||
const { artboardId } = submission.value | ||
|
||
// start transaction so we can create design and palette together | ||
// palette is 1:1 with design which belongs to an artboard | ||
try { | ||
await prisma.$transaction(async prisma => { | ||
// new designs are appended to the end of the list | ||
// find the last design in the list by type | ||
// we know the artboard already exists for the user by this point | ||
const lastArtboardDesignPalette = await prisma.design.findFirst({ | ||
where: { type: 'palette', artboardId, nextId: null }, | ||
}) | ||
|
||
// create design before palette | ||
const designData = designSchema.parse({ | ||
type: 'palette', | ||
ownerId: userId, | ||
artboardId, | ||
}) | ||
const design = await prisma.design.create({ | ||
data: designData, | ||
}) | ||
|
||
// then create palette after design | ||
await prisma.palette.create({ | ||
data: { | ||
designId: design.id, | ||
}, | ||
}) | ||
|
||
// if the artboard already has a palette | ||
// link the new palette to the last one | ||
// and the last one to the new one | ||
if (lastArtboardDesignPalette) { | ||
await prisma.design.update({ | ||
where: { id: design.id }, | ||
data: { prevId: lastArtboardDesignPalette.id }, | ||
}) | ||
|
||
await prisma.design.update({ | ||
where: { id: lastArtboardDesignPalette.id }, | ||
data: { nextId: design.id }, | ||
}) | ||
} | ||
}) | ||
} catch (error) { | ||
console.log(error) | ||
return submissionErrorResponse(submission) | ||
} | ||
|
||
return json({ status: 'success', submission } as const) | ||
} | ||
|
||
export async function artboardDesignEditPaletteAction({ | ||
userId, | ||
formData, | ||
}: IntentActionArgs) { | ||
// validation | ||
const submission = await parseArtboardDesignSubmission({ | ||
userId, | ||
formData, | ||
schema: EditArtboardPaletteSchema, | ||
}) | ||
if (submission.intent !== 'submit') { | ||
return notSubmissionResponse(submission) | ||
} | ||
if (!submission.value) { | ||
return submissionErrorResponse(submission) | ||
} | ||
|
||
// changes | ||
const { id, value } = submission.value | ||
const palette = await findFirstPaletteInstance({ | ||
where: { id }, | ||
}) | ||
if (!palette) return submissionErrorResponse(submission) | ||
|
||
palette.value = value | ||
palette.updatedAt = new Date() | ||
await palette.save() | ||
|
||
return json({ status: 'success', submission } as const) | ||
} |
Oops, something went wrong.