Skip to content

Commit

Permalink
Prevent creation of multiple projects by multiple clicks (#3480)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewwallacespeckle authored Nov 12, 2024
1 parent 992c957 commit dd46709
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions packages/frontend-2/components/projects/AddDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ const emit = defineEmits<{
const isWorkspacesEnabled = useIsWorkspacesEnabled()
const createProject = useCreateProject()
const router = useRouter()
const { handleSubmit, meta } = useForm<FormValues>()
const logger = useLogger()
const { handleSubmit, meta, isSubmitting } = useForm<FormValues>()
const { result: workspaceResult } = useQuery(projectWorkspaceSelectQuery, null, () => ({
enabled: isWorkspacesEnabled.value
}))
Expand All @@ -127,45 +128,62 @@ const visibility = ref(ProjectVisibility.Unlisted)
const selectedWorkspace = ref<ProjectsAddDialog_WorkspaceFragment>()
const showConfirmDialog = ref(false)
const confirmActionType = ref<'navigate' | 'close' | null>(null)
const isClosing = ref(false)
const open = defineModel<boolean>('open', { required: true })
const mp = useMixpanel()
const onSubmit = handleSubmit(async (values) => {
const workspaceId = props.workspaceId || selectedWorkspace.value?.id
await createProject({
name: values.name,
description: values.description,
visibility: visibility.value,
...(workspaceId ? { workspaceId } : {})
})
emit('created')
mp.track('Stream Action', {
type: 'action',
name: 'create',
// eslint-disable-next-line camelcase
workspace_id: props.workspaceId
})
open.value = false
if (isClosing.value) return // Prevent submission while closing
try {
isClosing.value = true
const workspaceId = props.workspaceId || selectedWorkspace.value?.id
await createProject({
name: values.name,
description: values.description,
visibility: visibility.value,
...(workspaceId ? { workspaceId } : {})
})
emit('created')
mp.track('Stream Action', {
type: 'action',
name: 'create',
// eslint-disable-next-line camelcase
workspace_id: props.workspaceId
})
open.value = false
} catch (error) {
isClosing.value = false
logger.error('Failed to create project:', error)
}
})
const workspaces = computed(
() => workspaceResult.value?.activeUser?.workspaces.items ?? []
)
const hasWorkspaces = computed(() => workspaces.value.length > 0)
const dialogButtons = computed((): LayoutDialogButton[] => {
const isDisabled = isSubmitting.value || isClosing.value
return [
{
text: 'Cancel',
props: { color: 'outline' },
props: {
color: 'outline',
disabled: isDisabled
},
onClick: confirmCancel
},
{
text: 'Create',
props: {
submit: true
submit: true,
loading: isDisabled,
disabled: isDisabled
},
onClick: onSubmit
}
Expand Down Expand Up @@ -206,6 +224,7 @@ const handleConfirmAction = () => {
watch(open, (newVal, oldVal) => {
if (newVal && !oldVal) {
selectedWorkspace.value = undefined
isClosing.value = false
}
})
</script>

0 comments on commit dd46709

Please sign in to comment.