Skip to content

Commit

Permalink
feat(web): create new option modal
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Jan 2, 2023
1 parent 042fe53 commit dbd93fe
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 12 deletions.
13 changes: 13 additions & 0 deletions apps/web/components/kanban-ui/create-new-option-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Button, IconPlus } from '@egodb/ui'
import { useSetAtom } from 'jotai'
import { createNewOptionOpened } from './create-new-option.atom'

export const CreateNewOptionButton: React.FC = () => {
const setOpened = useSetAtom(createNewOptionOpened)

return (
<Button onClick={() => setOpened(true)} w={300} variant="white" leftIcon={<IconPlus />}>
New Stack
</Button>
)
}
55 changes: 55 additions & 0 deletions apps/web/components/kanban-ui/create-new-option-modal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { ICreateOptionSchema } from '@egodb/core'
import { OptionId } from '@egodb/core'
import { createOptionSchema } from '@egodb/core'
import { Button, Divider, Group, Modal, Stack, TextInput, useForm, zodResolver } from '@egodb/ui'
import { useAtom } from 'jotai'
import { trpc } from '../../trpc'
import type { ITableBaseProps } from '../table/table-base-props'
import { createNewOptionOpened } from './create-new-option.atom'

export const CreateNewOptionModal: React.FC<ITableBaseProps> = ({ table }) => {
const [opened, setOpened] = useAtom(createNewOptionOpened)
const form = useForm<ICreateOptionSchema>({
initialValues: {
name: '',
},
validate: zodResolver(createOptionSchema),
})

const utils = trpc.useContext()

const createOption = trpc.table.createOption.useMutation({
onSuccess() {
setOpened(false)
form.reset()
utils.table.get.refetch()
},
})

const onSubmit = form.onSubmit((values) => {
values.id = OptionId.create().value
createOption.mutate({
tableId: table.id.value,
option: values,
})
})

return (
<Modal title="Create New Option" opened={opened} onClose={() => setOpened(false)}>
<form onSubmit={onSubmit}>
<Stack>
<TextInput placeholder="option name" {...form.getInputProps('name')} />
<Divider />
<Group position="right">
<Button variant="white" onClick={() => setOpened(false)}>
Cancel
</Button>
<Button type="submit" disabled={!form.isValid()}>
Done
</Button>
</Group>
</Stack>
</form>
</Modal>
)
}
3 changes: 3 additions & 0 deletions apps/web/components/kanban-ui/create-new-option.atom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { atom } from 'jotai'

export const createNewOptionOpened = atom(false)
9 changes: 0 additions & 9 deletions apps/web/components/kanban-ui/new-stack-lane.tsx

This file was deleted.

6 changes: 4 additions & 2 deletions apps/web/components/kanban-ui/select-board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import { trpc } from '../../trpc'
import type { ITableBaseProps } from '../table/table-base-props'
import { openKanbanEditFieldAtom } from './kanban-edit-field.atom'
import { KanbanLane, SortableKanbanLane } from './kanban-lane'
import { NewStackLane } from './new-stack-lane'
import { CreateNewOptionButton } from './create-new-option-button'
import { SelectKanbanField } from './select-kanban-field'
import { CreateNewOptionModal } from './create-new-option-modal'

interface IProps extends ITableBaseProps {
field: SelectField
Expand Down Expand Up @@ -117,7 +118,8 @@ export const SelectBoard: React.FC<IProps> = ({ table, field, records }) => {
</DragOverlay>
</DndContext>

<NewStackLane />
<CreateNewOptionModal table={table} />
<CreateNewOptionButton />
</Group>
</Container>
)
Expand Down
10 changes: 10 additions & 0 deletions packages/trpc/router/table.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { IQueryTable } from '@egodb/core'
import {
CreateFieldCommand,
CreateOptionCommand,
createOptionCommandInput,
CreateTableCommand,
createTableCommandInput,
createTableCommandOutput,
Expand Down Expand Up @@ -135,4 +137,12 @@ export const createTableRouter =
const cmd = new ReorderOptionsCommand(input)
return commandBus.execute<void>(cmd)
}),
createOption: procedure
.meta({ openapi: { method: 'POST', path: '/table.createOption', tags } })
.input(createOptionCommandInput)
.output(z.void())
.mutation(({ input }) => {
const cmd = new CreateOptionCommand(input)
return commandBus.execute<void>(cmd)
}),
})
2 changes: 1 addition & 1 deletion packages/ui/Button.tsx
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { Button, UnstyledButton, ActionIcon, ActionIconProps } from '@mantine/core'
export { Button, UnstyledButton, ActionIcon } from '@mantine/core'

0 comments on commit dbd93fe

Please sign in to comment.