-
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.
panel entity update design type rotate through fetcher template and api
- Loading branch information
Showing
9 changed files
with
475 additions
and
7 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
app/models/design-type/design-type.update.rotate.server.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,122 @@ | ||
import { type IntentActionArgs } from '#app/definitions/intent-action-args' | ||
import { | ||
type DesignRotateUpdateSchemaType, | ||
EditDesignRotateBasisSchema, | ||
EditDesignRotateValueSchema, | ||
} from '#app/schema/rotate' | ||
import { ValidateDesignParentSubmissionStrategy } from '#app/strategies/validate-submission.strategy' | ||
import { validateEntitySubmission } from '#app/utils/conform-utils' | ||
import { findFirstRotateInstance } from '#app/utils/prisma-extensions-rotate' | ||
import { type IDesign } from '../design.server' | ||
import { type IRotate } from '../rotate.server' | ||
|
||
export interface IDesignTypeRotateUpdatedResponse { | ||
success: boolean | ||
message?: string | ||
updatedRotate?: IRotate | ||
} | ||
|
||
const validateUpdateSubmission = async ({ | ||
userId, | ||
formData, | ||
schema, | ||
}: IntentActionArgs & { | ||
schema: DesignRotateUpdateSchemaType | ||
}) => { | ||
const strategy = new ValidateDesignParentSubmissionStrategy() | ||
|
||
return await validateEntitySubmission({ | ||
userId, | ||
formData, | ||
schema, | ||
strategy, | ||
}) | ||
} | ||
|
||
export async function validateDesignTypeUpdateRotateValueSubmission( | ||
args: IntentActionArgs, | ||
) { | ||
return validateUpdateSubmission({ | ||
...args, | ||
schema: EditDesignRotateValueSchema, | ||
}) | ||
} | ||
|
||
export async function validateDesignTypeUpdateRotateBasisSubmission( | ||
args: IntentActionArgs, | ||
) { | ||
return validateUpdateSubmission({ | ||
...args, | ||
schema: EditDesignRotateBasisSchema, | ||
}) | ||
} | ||
|
||
const getRotateInstance = async ({ id }: { id: IRotate['id'] }) => { | ||
return await findFirstRotateInstance({ | ||
where: { id }, | ||
}) | ||
} | ||
|
||
// updating instance instead of regular prism update | ||
// this may not be easier, but it's more explicit | ||
export const updateDesignTypeRotateValue = async ({ | ||
id, | ||
designId, | ||
value, | ||
}: { | ||
id: IRotate['id'] | ||
designId: IDesign['id'] | ||
value: number | ||
}): Promise<IDesignTypeRotateUpdatedResponse> => { | ||
const rotate = await getRotateInstance({ id }) | ||
if (!rotate) return { success: false } | ||
|
||
try { | ||
const data = EditDesignRotateValueSchema.parse({ id, designId, value }) | ||
rotate.value = data.value | ||
rotate.updatedAt = new Date() | ||
await rotate.save() | ||
|
||
return { success: true, updatedRotate: rotate } | ||
} catch (error) { | ||
// consider how to handle this error where this is called | ||
console.log('updateDesignTypeRotateValue error:', error) | ||
const errorType = error instanceof Error | ||
const errorMessage = errorType ? error.message : 'An unknown error occurred' | ||
return { | ||
success: false, | ||
message: errorMessage, | ||
} | ||
} | ||
} | ||
|
||
export const updateDesignTypeRotateBasis = async ({ | ||
id, | ||
designId, | ||
basis, | ||
}: { | ||
id: IRotate['id'] | ||
designId: IDesign['id'] | ||
basis: string | ||
}): Promise<IDesignTypeRotateUpdatedResponse> => { | ||
const rotate = await getRotateInstance({ id }) | ||
if (!rotate) return { success: false } | ||
|
||
try { | ||
const data = EditDesignRotateBasisSchema.parse({ id, designId, basis }) | ||
rotate.basis = data.basis | ||
rotate.updatedAt = new Date() | ||
await rotate.save() | ||
|
||
return { success: true, updatedRotate: rotate } | ||
} catch (error) { | ||
// consider how to handle this error where this is called | ||
console.log('updateDesignTypeRotateBasis error:', error) | ||
const errorType = error instanceof Error | ||
const errorMessage = errorType ? error.message : 'An unknown error occurred' | ||
return { | ||
success: false, | ||
message: errorMessage, | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
app/routes/resources+/api.v1+/design.type.rotate.update.basis.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,53 @@ | ||
import { | ||
type LoaderFunctionArgs, | ||
json, | ||
type DataFunctionArgs, | ||
} from '@remix-run/node' | ||
import { redirectBack } from 'remix-utils/redirect-back' | ||
import { | ||
updateDesignTypeRotateBasis, | ||
validateDesignTypeUpdateRotateBasisSubmission, | ||
} from '#app/models/design-type/design-type.update.rotate.server' | ||
import { validateNoJS } from '#app/schema/form-data' | ||
import { requireUserId } from '#app/utils/auth.server' | ||
|
||
// https://www.epicweb.dev/full-stack-components | ||
|
||
export async function loader({ request }: LoaderFunctionArgs) { | ||
await requireUserId(request) | ||
return json({}) | ||
} | ||
|
||
export async function action({ request }: DataFunctionArgs) { | ||
const userId = await requireUserId(request) | ||
const formData = await request.formData() | ||
const noJS = validateNoJS({ formData }) | ||
|
||
let updateSuccess = false | ||
const { status, submission } = | ||
await validateDesignTypeUpdateRotateBasisSubmission({ | ||
userId, | ||
formData, | ||
}) | ||
|
||
if (status === 'success') { | ||
const { success } = await updateDesignTypeRotateBasis({ | ||
userId, | ||
...submission.value, | ||
}) | ||
updateSuccess = success | ||
} | ||
|
||
if (noJS) { | ||
throw redirectBack(request, { | ||
fallback: '/', | ||
}) | ||
} | ||
|
||
return json( | ||
{ status, submission }, | ||
{ | ||
status: status === 'error' || !updateSuccess ? 404 : 200, | ||
}, | ||
) | ||
} |
53 changes: 53 additions & 0 deletions
53
app/routes/resources+/api.v1+/design.type.rotate.update.value.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,53 @@ | ||
import { | ||
type LoaderFunctionArgs, | ||
json, | ||
type DataFunctionArgs, | ||
} from '@remix-run/node' | ||
import { redirectBack } from 'remix-utils/redirect-back' | ||
import { | ||
updateDesignTypeRotateValue, | ||
validateDesignTypeUpdateRotateValueSubmission, | ||
} from '#app/models/design-type/design-type.update.rotate.server' | ||
import { validateNoJS } from '#app/schema/form-data' | ||
import { requireUserId } from '#app/utils/auth.server' | ||
|
||
// https://www.epicweb.dev/full-stack-components | ||
|
||
export async function loader({ request }: LoaderFunctionArgs) { | ||
await requireUserId(request) | ||
return json({}) | ||
} | ||
|
||
export async function action({ request }: DataFunctionArgs) { | ||
const userId = await requireUserId(request) | ||
const formData = await request.formData() | ||
const noJS = validateNoJS({ formData }) | ||
|
||
let updateSuccess = false | ||
const { status, submission } = | ||
await validateDesignTypeUpdateRotateValueSubmission({ | ||
userId, | ||
formData, | ||
}) | ||
|
||
if (status === 'success') { | ||
const { success } = await updateDesignTypeRotateValue({ | ||
userId, | ||
...submission.value, | ||
}) | ||
updateSuccess = success | ||
} | ||
|
||
if (noJS) { | ||
throw redirectBack(request, { | ||
fallback: '/', | ||
}) | ||
} | ||
|
||
return json( | ||
{ status, submission }, | ||
{ | ||
status: status === 'error' || !updateSuccess ? 404 : 200, | ||
}, | ||
) | ||
} |
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
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,68 @@ | ||
import { type User } from '@sentry/remix' | ||
import { | ||
type IDesignTypeRotateUpdatedResponse, | ||
updateDesignTypeRotateValue, | ||
updateDesignTypeRotateBasis, | ||
} from '#app/models/design-type/design-type.update.rotate.server' | ||
import { type IDesign } from '#app/models/design.server' | ||
import { type IRotate } from '#app/models/rotate.server' | ||
|
||
export const updateDesignTypeRotateValueService = async ({ | ||
userId, | ||
id, | ||
designId, | ||
value, | ||
}: { | ||
userId: User['id'] | ||
id: IRotate['id'] | ||
designId: IDesign['id'] | ||
value: number | ||
}): Promise<IDesignTypeRotateUpdatedResponse> => { | ||
try { | ||
return await updateDesignTypeRotateValue({ | ||
id, | ||
designId, | ||
value, | ||
}) | ||
// later will be adding Activity class | ||
// i.e, edit history so you can undo changes and/or see who made them | ||
} catch (error) { | ||
console.log('updateDesignTypeRotateValueService error:', error) | ||
const errorType = error instanceof Error | ||
const errorMessage = errorType ? error.message : 'An unknown error occurred' | ||
return { | ||
success: false, | ||
message: errorMessage, | ||
} | ||
} | ||
} | ||
|
||
export const updateDesignTypeRotateBasisService = async ({ | ||
userId, | ||
id, | ||
designId, | ||
basis, | ||
}: { | ||
userId: User['id'] | ||
id: IRotate['id'] | ||
designId: IDesign['id'] | ||
basis: string | ||
}): Promise<IDesignTypeRotateUpdatedResponse> => { | ||
try { | ||
return await updateDesignTypeRotateBasis({ | ||
id, | ||
designId, | ||
basis, | ||
}) | ||
// later will be adding Activity class | ||
// i.e, edit history so you can undo changes and/or see who made them | ||
} catch (error) { | ||
console.log('updateDesignTypeRotateBasisService error:', error) | ||
const errorType = error instanceof Error | ||
const errorMessage = errorType ? error.message : 'An unknown error occurred' | ||
return { | ||
success: false, | ||
message: errorMessage, | ||
} | ||
} | ||
} |
Oops, something went wrong.