-
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 #32 from goodeats/31-create-size-design
size design
- Loading branch information
Showing
20 changed files
with
590 additions
and
8 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,9 @@ | ||
export interface ISize { | ||
id: string | ||
format: string | ||
value: number | ||
basis: string | ||
createdAt: Date | string | ||
updatedAt: Date | string | ||
designId: string | ||
} |
114 changes: 114 additions & 0 deletions
114
app/routes/sketch+/artboards+/$slug_+/actions/artboard-design-size.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,114 @@ | ||
import { json } from '@remix-run/node' | ||
import { type IntentActionArgs } from '#app/definitions/intent-action-args' | ||
import { NewArtboardDesignSchema, designSchema } from '#app/schema/design' | ||
import { EditArtboardSizeSchema } from '#app/schema/size' | ||
import { | ||
notSubmissionResponse, | ||
submissionErrorResponse, | ||
} from '#app/utils/conform-utils' | ||
import { prisma } from '#app/utils/db.server' | ||
import { findFirstSizeInstance } from '#app/utils/prisma-extensions-size' | ||
import { parseArtboardDesignSubmission } from './utils' | ||
|
||
export async function artboardDesignNewSizeAction({ | ||
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 size together | ||
// size 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 lastArtboardDesignSize = await prisma.design.findFirst({ | ||
where: { type: 'size', artboardId, nextId: null }, | ||
}) | ||
|
||
// create design before size | ||
const designData = designSchema.parse({ | ||
type: 'size', | ||
ownerId: userId, | ||
artboardId, | ||
}) | ||
const design = await prisma.design.create({ | ||
data: designData, | ||
}) | ||
|
||
// then create size after design | ||
await prisma.size.create({ | ||
data: { | ||
designId: design.id, | ||
}, | ||
}) | ||
|
||
// if the artboard already has a size | ||
// link the new size to the last one | ||
// and the last one to the new one | ||
if (lastArtboardDesignSize) { | ||
await prisma.design.update({ | ||
where: { id: design.id }, | ||
data: { prevId: lastArtboardDesignSize.id }, | ||
}) | ||
|
||
await prisma.design.update({ | ||
where: { id: lastArtboardDesignSize.id }, | ||
data: { nextId: design.id }, | ||
}) | ||
} | ||
}) | ||
} catch (error) { | ||
console.log(error) | ||
return submissionErrorResponse(submission) | ||
} | ||
|
||
return json({ status: 'success', submission } as const) | ||
} | ||
|
||
export async function artboardDesignEditSizeAction({ | ||
userId, | ||
formData, | ||
}: IntentActionArgs) { | ||
// validation | ||
const submission = await parseArtboardDesignSubmission({ | ||
userId, | ||
formData, | ||
schema: EditArtboardSizeSchema, | ||
}) | ||
|
||
if (submission.intent !== 'submit') { | ||
return notSubmissionResponse(submission) | ||
} | ||
if (!submission.value) { | ||
return submissionErrorResponse(submission) | ||
} | ||
|
||
// changes | ||
const { id, value } = submission.value | ||
const size = await findFirstSizeInstance({ | ||
where: { id }, | ||
}) | ||
if (!size) return submissionErrorResponse(submission) | ||
|
||
size.value = value | ||
size.updatedAt = new Date() | ||
await size.save() | ||
|
||
return json({ status: 'success', submission } as const) | ||
} |
100 changes: 100 additions & 0 deletions
100
app/routes/sketch+/artboards+/$slug_+/components/panel-content-artboard-design-size.tsx
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,100 @@ | ||
import { | ||
Panel, | ||
PanelHeader, | ||
PanelRow, | ||
PanelRowContainer, | ||
PanelRowIconContainer, | ||
PanelRowOrderContainer, | ||
PanelRowValueContainer, | ||
PanelTitle, | ||
} from '#app/components/shared' | ||
import { type IDesignWithSize } from '#app/models/design.server' | ||
import { type PickedArtboardType } from '../queries' | ||
import { PanelFormArtboardDesignDelete } from './panel-form-artboard-design-delete' | ||
import { PanelFormArtboardDesignEditSize } from './panel-form-artboard-design-edit-size' | ||
import { PanelFormArtboardDesignNewSize } from './panel-form-artboard-design-new-size' | ||
import { PanelFormArtboardDesignReorder } from './panel-form-artboard-design-reorder' | ||
import { PanelFormArtboardDesignToggleVisibility } from './panel-form-artboard-design-toggle-visibility' | ||
import { PanelPopoverArtboardDesignSize } from './panel-popover-artboard-design-size' | ||
|
||
export const PanelContentArtboardDesignSize = ({ | ||
artboard, | ||
designSizes, | ||
}: { | ||
artboard: PickedArtboardType | ||
designSizes: IDesignWithSize[] | ||
}) => { | ||
const orderedDesignSizes = designSizes.reduce( | ||
(acc: IDesignWithSize[], designSize) => { | ||
if (!designSize.prevId) { | ||
acc.unshift(designSize) // Add the head of the list to the start | ||
} else { | ||
let currentDesignIndex = acc.findIndex(d => d.id === designSize.prevId) | ||
if (currentDesignIndex !== -1) { | ||
// Insert the designSize right after its predecessor | ||
acc.splice(currentDesignIndex + 1, 0, designSize) | ||
} else { | ||
// If predecessor is not found, add it to the end as a fallback | ||
acc.push(designSize) | ||
} | ||
} | ||
return acc | ||
}, | ||
[], | ||
) | ||
|
||
const designCount = designSizes.length | ||
return ( | ||
<Panel> | ||
<PanelHeader> | ||
<PanelTitle>Size</PanelTitle> | ||
<div className="flex flex-shrink"> | ||
<PanelFormArtboardDesignNewSize artboardId={artboard.id} /> | ||
</div> | ||
</PanelHeader> | ||
{orderedDesignSizes.map((designSize, index) => { | ||
const { id, visible, size } = designSize | ||
return ( | ||
<PanelRow key={size.id}> | ||
<PanelRowOrderContainer> | ||
<PanelFormArtboardDesignReorder | ||
id={id} | ||
artboardId={artboard.id} | ||
panelCount={designCount} | ||
panelIndex={index} | ||
direction="up" | ||
/> | ||
<PanelFormArtboardDesignReorder | ||
id={id} | ||
artboardId={artboard.id} | ||
panelCount={designCount} | ||
panelIndex={index} | ||
direction="down" | ||
/> | ||
</PanelRowOrderContainer> | ||
<PanelRowContainer> | ||
<PanelRowValueContainer> | ||
<PanelPopoverArtboardDesignSize size={size} /> | ||
<PanelFormArtboardDesignEditSize | ||
artboardId={artboard.id} | ||
size={size} | ||
/> | ||
</PanelRowValueContainer> | ||
<PanelRowIconContainer> | ||
<PanelFormArtboardDesignToggleVisibility | ||
id={id} | ||
artboardId={artboard.id} | ||
visible={visible} | ||
/> | ||
<PanelFormArtboardDesignDelete | ||
id={id} | ||
artboardId={artboard.id} | ||
/> | ||
</PanelRowIconContainer> | ||
</PanelRowContainer> | ||
</PanelRow> | ||
) | ||
})} | ||
</Panel> | ||
) | ||
} |
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
63 changes: 63 additions & 0 deletions
63
app/routes/sketch+/artboards+/$slug_+/components/panel-form-artboard-design-edit-size.tsx
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,63 @@ | ||
import { conform, useForm } from '@conform-to/react' | ||
import { getFieldsetConstraint } from '@conform-to/zod' | ||
import { type Artboard } from '@prisma/client' | ||
import { useActionData, useFetcher } from '@remix-run/react' | ||
import { type FocusEvent } from 'react' | ||
import { AuthenticityTokenInput } from 'remix-utils/csrf/react' | ||
import { Input } from '#app/components/ui/input' | ||
import { type ISize } from '#app/models/size.server' | ||
import { EditArtboardPaletteSchema } from '#app/schema/palette' | ||
import { useIsPending } from '#app/utils/misc' | ||
import { INTENT } from '../intent' | ||
import { type action } from '../route' | ||
|
||
export const PanelFormArtboardDesignEditSize = ({ | ||
artboardId, | ||
size, | ||
}: { | ||
artboardId: Artboard['id'] | ||
size: ISize | ||
}) => { | ||
const fetcher = useFetcher<typeof action>() | ||
const actionData = useActionData<typeof action>() | ||
const isPending = useIsPending() | ||
|
||
const [form, fields] = useForm({ | ||
id: `panel-form-artboard-design-edit-size-${size.id}`, | ||
constraint: getFieldsetConstraint(EditArtboardPaletteSchema), | ||
lastSubmission: actionData?.submission, | ||
defaultValue: { | ||
...size, | ||
}, | ||
}) | ||
|
||
const handleSubmit = (event: FocusEvent<HTMLInputElement>) => { | ||
fetcher.submit(event.currentTarget.form, { | ||
method: 'POST', | ||
}) | ||
} | ||
|
||
return ( | ||
<fetcher.Form method="POST" {...form.props}> | ||
<AuthenticityTokenInput /> | ||
|
||
<input type="hidden" name="id" value={size.id} /> | ||
<input type="hidden" name="artboardId" value={artboardId} /> | ||
<input type="hidden" name="designId" value={size.designId} /> | ||
<input | ||
type="hidden" | ||
name="intent" | ||
value={INTENT.artboardUpdateDesignSize} | ||
/> | ||
<Input | ||
type="number" | ||
className={'flex h-8'} | ||
onBlur={e => handleSubmit(e)} | ||
disabled={isPending} | ||
{...conform.input(fields.value, { | ||
ariaAttributes: true, | ||
})} | ||
/> | ||
</fetcher.Form> | ||
) | ||
} |
Oops, something went wrong.