-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
58 changed files
with
2,760 additions
and
74 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
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
167 changes: 167 additions & 0 deletions
167
apps/member-profile/app/routes/_profile.resources.$id_.edit.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,167 @@ | ||
import { | ||
type ActionFunctionArgs, | ||
json, | ||
type LoaderFunctionArgs, | ||
redirect, | ||
} from '@remix-run/node'; | ||
import { | ||
Form as RemixForm, | ||
useActionData, | ||
useLoaderData, | ||
useSearchParams, | ||
} from '@remix-run/react'; | ||
|
||
import { type ResourceType, UpdateResourceInput } from '@oyster/core/resources'; | ||
import { getResource, updateResource } from '@oyster/core/resources.server'; | ||
import { | ||
Button, | ||
Divider, | ||
Form, | ||
getActionErrors, | ||
Modal, | ||
validateForm, | ||
} from '@oyster/ui'; | ||
|
||
import { | ||
ResourceDescriptionField, | ||
ResourceLinkField, | ||
ResourceProvider, | ||
ResourceTagsField, | ||
ResourceTitleField, | ||
} from '@/shared/components/resource-form'; | ||
import { Route } from '@/shared/constants'; | ||
import { | ||
commitSession, | ||
ensureUserAuthenticated, | ||
toast, | ||
} from '@/shared/session.server'; | ||
|
||
export async function loader({ params, request }: LoaderFunctionArgs) { | ||
await ensureUserAuthenticated(request); | ||
|
||
const record = await getResource({ | ||
select: [ | ||
'resources.description', | ||
'resources.link', | ||
'resources.title', | ||
'resources.type', | ||
], | ||
where: { id: params.id as string }, | ||
}); | ||
|
||
if (!record) { | ||
throw new Response(null, { status: 404 }); | ||
} | ||
|
||
const resource = { | ||
...record, | ||
type: record.type as ResourceType, | ||
}; | ||
|
||
return json({ | ||
resource, | ||
}); | ||
} | ||
|
||
export async function action({ params, request }: ActionFunctionArgs) { | ||
const session = await ensureUserAuthenticated(request); | ||
|
||
const form = await request.formData(); | ||
|
||
const { data, errors } = validateForm( | ||
UpdateResourceInput, | ||
Object.fromEntries(form) | ||
); | ||
|
||
if (!data) { | ||
return json({ | ||
error: '', | ||
errors, | ||
}); | ||
} | ||
|
||
await updateResource(params.id as string, { | ||
description: data.description, | ||
link: data.link, | ||
tags: data.tags, | ||
title: data.title, | ||
}); | ||
|
||
toast(session, { | ||
message: 'Edited resource!', | ||
type: 'success', | ||
}); | ||
|
||
// TODO: Include query params... | ||
|
||
return redirect(Route['/resources'], { | ||
headers: { | ||
'Set-Cookie': await commitSession(session), | ||
}, | ||
}); | ||
} | ||
|
||
const keys = UpdateResourceInput.keyof().enum; | ||
|
||
export default function EditResourceModal() { | ||
const { resource } = useLoaderData<typeof loader>(); | ||
const { error, errors } = getActionErrors(useActionData<typeof action>()); | ||
const [searchParams] = useSearchParams(); | ||
|
||
return ( | ||
<Modal | ||
onCloseTo={{ | ||
pathname: Route['/resources'], | ||
search: searchParams.toString(), | ||
}} | ||
> | ||
<Modal.Header> | ||
<Modal.Title>Edit Resource</Modal.Title> | ||
<Modal.CloseButton /> | ||
</Modal.Header> | ||
|
||
<RemixForm className="form" method="post"> | ||
<ResourceProvider type={resource.type}> | ||
<ResourceTitleField | ||
defaultValue={resource.title || undefined} | ||
error={errors.title} | ||
name={keys.title} | ||
/> | ||
<ResourceDescriptionField | ||
defaultValue={resource.description || undefined} | ||
error={errors.description} | ||
name={keys.description} | ||
/> | ||
<ResourceTagsField | ||
defaultValue={(resource.tags || []).map((tag) => { | ||
return { | ||
label: tag.name, | ||
value: tag.id, | ||
}; | ||
})} | ||
error={errors.tags} | ||
name={keys.tags} | ||
/> | ||
|
||
{resource.link && ( | ||
<> | ||
<Divider /> | ||
|
||
<ResourceLinkField | ||
defaultValue={resource.link || undefined} | ||
error={errors.link} | ||
name={keys.link} | ||
/> | ||
</> | ||
)} | ||
</ResourceProvider> | ||
|
||
<Form.ErrorMessage>{error}</Form.ErrorMessage> | ||
|
||
<Button.Group> | ||
<Button.Submit>Save</Button.Submit> | ||
</Button.Group> | ||
</RemixForm> | ||
</Modal> | ||
); | ||
} |
Oops, something went wrong.