-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to add school as a chapter
- Loading branch information
Showing
6 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
apps/admin-dashboard/app/routes/_dashboard.schools.$id.chapter.create.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,82 @@ | ||
import { | ||
type ActionFunctionArgs, | ||
json, | ||
type LoaderFunctionArgs, | ||
redirect, | ||
} from '@remix-run/node'; | ||
import { | ||
Form as RemixForm, | ||
useActionData, | ||
useLoaderData, | ||
} from '@remix-run/react'; | ||
|
||
import { createChapter } from '@oyster/core/chapter'; | ||
import { getSchool } from '@oyster/core/education'; | ||
import { Button, Modal } from '@oyster/ui'; | ||
|
||
import { Route } from '@/shared/constants'; | ||
import { | ||
commitSession, | ||
ensureUserAuthenticated, | ||
toast, | ||
} from '@/shared/session.server'; | ||
|
||
export async function loader({ params, request }: LoaderFunctionArgs) { | ||
await ensureUserAuthenticated(request); | ||
|
||
const school = await getSchool({ | ||
select: ['name'], | ||
where: { id: params.id as string }, | ||
}); | ||
|
||
if (!school) { | ||
throw new Response(null, { status: 404 }); | ||
} | ||
|
||
return json({ | ||
school, | ||
}); | ||
} | ||
|
||
export async function action({ params, request }: ActionFunctionArgs) { | ||
const session = await ensureUserAuthenticated(request); | ||
|
||
await createChapter({ | ||
schoolId: params.id as string, | ||
}); | ||
|
||
toast(session, { | ||
message: 'Created chapter.', | ||
}); | ||
|
||
return redirect(Route['/schools'], { | ||
headers: { | ||
'Set-Cookie': await commitSession(session), | ||
}, | ||
}); | ||
} | ||
|
||
export default function CreateChapterModal() { | ||
const { school } = useLoaderData<typeof loader>(); | ||
|
||
useActionData<typeof action>(); | ||
|
||
return ( | ||
<Modal onCloseTo={Route['/schools']}> | ||
<Modal.Header> | ||
<Modal.Title>Activate Chapter for {school.name}</Modal.Title> | ||
<Modal.CloseButton /> | ||
</Modal.Header> | ||
|
||
<Modal.Description> | ||
Are you sure you want to create a chapter for {school.name}? | ||
</Modal.Description> | ||
|
||
<RemixForm className="form" method="post"> | ||
<Button.Group> | ||
<Button.Submit>Create chapter</Button.Submit> | ||
</Button.Group> | ||
</RemixForm> | ||
</Modal> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { db } from '@oyster/db'; | ||
|
||
import { type CreateChapterInput } from '@/modules/chapter/chapter.types'; | ||
|
||
// Add chapter | ||
export async function createChapter({ schoolId }: CreateChapterInput) { | ||
await db.insertInto('chapters').values({ schoolId }).execute(); | ||
} |
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,20 @@ | ||
import { z } from 'zod'; | ||
|
||
import { Entity } from '@oyster/types'; | ||
|
||
// schema | ||
|
||
export const Chapter = Entity.extend({ | ||
createdAt: z.date(), | ||
schoolId: z.string(), | ||
}); | ||
|
||
// Use Cases | ||
|
||
export const CreateChapterInput = Chapter.pick({ | ||
schoolId: true, | ||
}); | ||
|
||
//Types | ||
|
||
export type CreateChapterInput = z.infer<typeof CreateChapterInput>; |