generated from mantinedev/next-pages-template
-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
4 changed files
with
210 additions
and
11 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/components/Dashboard/Modals/RenameBoard/RenameBoardModal.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 { ContextModalProps, modals } from '@mantine/modals'; | ||
import { Alert, Button, TextInput } from '@mantine/core'; | ||
import { api } from '~/utils/api'; | ||
import { useForm, zodResolver } from '@mantine/form'; | ||
import { z } from 'zod'; | ||
import { IconAlertCircle } from '@tabler/icons-react'; | ||
|
||
type RenameBoardModalProps = { | ||
configName: string; | ||
configNames: string[]; | ||
} | ||
|
||
export const RenameBoardModal = ({ context, id, innerProps }: ContextModalProps<RenameBoardModalProps>) => { | ||
const utils = api.useUtils(); | ||
const { mutateAsync: mutateRenameBoardAsync, isLoading, isError, error } = api.boards.renameBoard.useMutation({ | ||
onSettled: () => { | ||
void utils.boards.all.invalidate(); | ||
} | ||
}); | ||
|
||
const form = useForm({ | ||
initialValues: { | ||
newName: '', | ||
}, | ||
validate: zodResolver(z.object({ | ||
newName: z.string().min(1).refine(value => !innerProps.configNames.includes(value)), | ||
})), | ||
validateInputOnBlur: true, | ||
validateInputOnChange: true, | ||
}); | ||
|
||
const handleSubmit = () => { | ||
mutateRenameBoardAsync({ | ||
oldName: innerProps.configName, | ||
newName: form.values.newName, | ||
}).then(() => { | ||
modals.close(id); | ||
}); | ||
}; | ||
|
||
return ( | ||
<form onSubmit={form.onSubmit(handleSubmit)}> | ||
{isError && error && ( | ||
<Alert icon={<IconAlertCircle size={"1rem"} />} mb={"md"}> | ||
{error.message} | ||
</Alert> | ||
)} | ||
<TextInput | ||
label="New name" | ||
placeholder="New board name" | ||
data-autofocus | ||
{...form.getInputProps('newName')} /> | ||
<Button | ||
loading={isLoading} | ||
fullWidth | ||
mt="md" | ||
type={'submit'} | ||
variant={"light"}> | ||
Submit | ||
</Button> | ||
</form> | ||
); | ||
}; |
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