issues-95 get saving and forking to work
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { createUserInsecure } from 'src/services/users/users.js'
|
import { createUserInsecure } from 'src/services/users/users.js'
|
||||||
import { db } from 'src/lib/db'
|
import { db } from 'src/lib/db'
|
||||||
import { enforceAlphaNumeric } from 'src/services/helpers'
|
import { enforceAlphaNumeric, generateUniqueString } from 'src/services/helpers'
|
||||||
|
|
||||||
export const handler = async (req, _context) => {
|
export const handler = async (req, _context) => {
|
||||||
const body = JSON.parse(req.body)
|
const body = JSON.parse(req.body)
|
||||||
@@ -58,26 +58,12 @@ export const handler = async (req, _context) => {
|
|||||||
|
|
||||||
if (eventType === 'signup') {
|
if (eventType === 'signup') {
|
||||||
roles.push('user')
|
roles.push('user')
|
||||||
// const hi = {
|
const isUniqueCallback = async (seed) =>
|
||||||
// email: 'kurt.hutten@gmail.com',
|
db.user.findOne({
|
||||||
// image: '',
|
|
||||||
// bio: ''
|
|
||||||
// }
|
|
||||||
|
|
||||||
const generateUniqueUserName = async (seed, count = 0) => {
|
|
||||||
const isUnique = !(await db.user.findOne({
|
|
||||||
where: { userName: seed },
|
where: { userName: seed },
|
||||||
}))
|
})
|
||||||
if (isUnique) {
|
|
||||||
return seed
|
|
||||||
}
|
|
||||||
count += 1
|
|
||||||
const newSeed =
|
|
||||||
count === 1 ? `${seed}_${count}` : seed.slice(0, -1) + count
|
|
||||||
return generateUniqueUserName(newSeed, count)
|
|
||||||
}
|
|
||||||
const userNameSeed = enforceAlphaNumeric(email.split('@')[0])
|
const userNameSeed = enforceAlphaNumeric(email.split('@')[0])
|
||||||
const userName = await generateUniqueUserName(userNameSeed) // TODO maybe come up with a better default userName?
|
const userName = await generateUniqueString(userNameSeed, isUniqueCallback) // TODO maybe come up with a better default userName?
|
||||||
const input = {
|
const input = {
|
||||||
email,
|
email,
|
||||||
userName,
|
userName,
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ export const schema = gql`
|
|||||||
|
|
||||||
type Mutation {
|
type Mutation {
|
||||||
createPart(input: CreatePartInput!): Part!
|
createPart(input: CreatePartInput!): Part!
|
||||||
|
forkPart(input: CreatePartInput!): Part!
|
||||||
updatePart(id: String!, input: UpdatePartInput!): Part!
|
updatePart(id: String!, input: UpdatePartInput!): Part!
|
||||||
deletePart(id: String!): Part!
|
deletePart(id: String!): Part!
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,3 +14,17 @@ export const foreignKeyReplacement = (input) => {
|
|||||||
|
|
||||||
export const enforceAlphaNumeric = (string) =>
|
export const enforceAlphaNumeric = (string) =>
|
||||||
string.replace(/([^a-zA-Z\d_:])/g, '-')
|
string.replace(/([^a-zA-Z\d_:])/g, '-')
|
||||||
|
|
||||||
|
export const generateUniqueString = async (
|
||||||
|
seed,
|
||||||
|
isUniqueCallback,
|
||||||
|
count = 0
|
||||||
|
) => {
|
||||||
|
const isUnique = !(await isUniqueCallback(seed))
|
||||||
|
if (isUnique) {
|
||||||
|
return seed
|
||||||
|
}
|
||||||
|
count += 1
|
||||||
|
const newSeed = count === 1 ? `${seed}_${count}` : seed.slice(0, -1) + count
|
||||||
|
return generateUniqueString(newSeed, isUniqueCallback, count)
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { db } from 'src/lib/db'
|
|||||||
import {
|
import {
|
||||||
foreignKeyReplacement,
|
foreignKeyReplacement,
|
||||||
enforceAlphaNumeric,
|
enforceAlphaNumeric,
|
||||||
|
generateUniqueString,
|
||||||
} from 'src/services/helpers'
|
} from 'src/services/helpers'
|
||||||
import { requireAuth } from 'src/lib/auth'
|
import { requireAuth } from 'src/lib/auth'
|
||||||
import { requireOwnership } from 'src/lib/owner'
|
import { requireOwnership } from 'src/lib/owner'
|
||||||
@@ -38,6 +39,25 @@ export const createPart = async ({ input }) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const forkPart = async ({ input }) => {
|
||||||
|
// Only difference between create nda clone part is that clone part will generate a unique title
|
||||||
|
// (for the user) if there is a conflict
|
||||||
|
const isUniqueCallback = async (seed) =>
|
||||||
|
db.part.findOne({
|
||||||
|
where: {
|
||||||
|
title_userId: {
|
||||||
|
title: seed,
|
||||||
|
userId: input.userId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
const title = await generateUniqueString(input.title, isUniqueCallback)
|
||||||
|
// TODO change the description to `forked from userName/partName ${rest of description}`
|
||||||
|
return db.part.create({
|
||||||
|
data: foreignKeyReplacement({ ...input, title }),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export const updatePart = async ({ id, input }) => {
|
export const updatePart = async ({ id, input }) => {
|
||||||
requireAuth()
|
requireAuth()
|
||||||
await requireOwnership({ partId: id })
|
await requireOwnership({ partId: id })
|
||||||
|
|||||||
Submodule web/src/cascade updated: 4849635f04...3cf6e98dd7
@@ -60,7 +60,18 @@ const IdeCascadeStudio = ({ part, saveCode, loading, error }) => {
|
|||||||
<IdeToolbar
|
<IdeToolbar
|
||||||
canEdit={canEdit}
|
canEdit={canEdit}
|
||||||
isChanges={isChanges && !loading}
|
isChanges={isChanges && !loading}
|
||||||
onSave={() => {}}
|
onSave={() => {
|
||||||
|
saveCode({
|
||||||
|
input: {
|
||||||
|
code,
|
||||||
|
title: part?.title,
|
||||||
|
userId: currentUser?.sub,
|
||||||
|
description: part?.description,
|
||||||
|
},
|
||||||
|
id: part.id,
|
||||||
|
isFork: !canEdit,
|
||||||
|
})
|
||||||
|
}}
|
||||||
onExport={(type) => threejsViewport[`saveShape${type}`]()}
|
onExport={(type) => threejsViewport[`saveShape${type}`]()}
|
||||||
/>
|
/>
|
||||||
<div id="topnav" className="topnav hidden">
|
<div id="topnav" className="topnav hidden">
|
||||||
|
|||||||
@@ -26,6 +26,17 @@ const UPDATE_PART_MUTATION = gql`
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
const FORK_PART_MUTATION = gql`
|
||||||
|
mutation ForkPartMutation($input: CreatePartInput!) {
|
||||||
|
forkPart(input: $input) {
|
||||||
|
id
|
||||||
|
title
|
||||||
|
user {
|
||||||
|
userName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
export const Loading = () => <div>Loading...</div>
|
export const Loading = () => <div>Loading...</div>
|
||||||
|
|
||||||
@@ -39,10 +50,25 @@ export const Success = ({ part, refetch }) => {
|
|||||||
addMessage('Part updated.', { classes: 'rw-flash-success' })
|
addMessage('Part updated.', { classes: 'rw-flash-success' })
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
const [forkPart] = useMutation(FORK_PART_MUTATION, {
|
||||||
|
onCompleted: ({ forkPart }) => {
|
||||||
|
navigate(
|
||||||
|
routes.ide({
|
||||||
|
userName: forkPart?.user?.userName,
|
||||||
|
partTitle: forkPart?.title,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
addMessage('Part Forked.', { classes: 'rw-flash-success' })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
const saveCode = (input, id) => {
|
const saveCode = ({ input, id, isFork }) => {
|
||||||
updatePart({ variables: { id, input } })
|
if (!isFork) {
|
||||||
refetch()
|
updatePart({ variables: { id, input } })
|
||||||
|
refetch()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
forkPart({ variables: { input } })
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<IdeCascadeStudio
|
<IdeCascadeStudio
|
||||||
|
|||||||
Reference in New Issue
Block a user