issues-95 get saving and forking to work

This commit is contained in:
Kurt Hutten
2020-11-14 19:44:40 +11:00
parent 7abdd87f03
commit 955f4c9e83
7 changed files with 82 additions and 24 deletions

View File

@@ -1,6 +1,6 @@
import { createUserInsecure } from 'src/services/users/users.js'
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) => {
const body = JSON.parse(req.body)
@@ -58,26 +58,12 @@ export const handler = async (req, _context) => {
if (eventType === 'signup') {
roles.push('user')
// const hi = {
// email: 'kurt.hutten@gmail.com',
// image: '',
// bio: ''
// }
const generateUniqueUserName = async (seed, count = 0) => {
const isUnique = !(await db.user.findOne({
const isUniqueCallback = async (seed) =>
db.user.findOne({
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 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 = {
email,
userName,

View File

@@ -37,6 +37,7 @@ export const schema = gql`
type Mutation {
createPart(input: CreatePartInput!): Part!
forkPart(input: CreatePartInput!): Part!
updatePart(id: String!, input: UpdatePartInput!): Part!
deletePart(id: String!): Part!
}

View File

@@ -14,3 +14,17 @@ export const foreignKeyReplacement = (input) => {
export const enforceAlphaNumeric = (string) =>
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)
}

View File

@@ -2,6 +2,7 @@ import { db } from 'src/lib/db'
import {
foreignKeyReplacement,
enforceAlphaNumeric,
generateUniqueString,
} from 'src/services/helpers'
import { requireAuth } from 'src/lib/auth'
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 }) => {
requireAuth()
await requireOwnership({ partId: id })

View File

@@ -60,7 +60,18 @@ const IdeCascadeStudio = ({ part, saveCode, loading, error }) => {
<IdeToolbar
canEdit={canEdit}
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}`]()}
/>
<div id="topnav" className="topnav hidden">

View File

@@ -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>
@@ -39,10 +50,25 @@ export const Success = ({ part, refetch }) => {
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) => {
updatePart({ variables: { id, input } })
refetch()
const saveCode = ({ input, id, isFork }) => {
if (!isFork) {
updatePart({ variables: { id, input } })
refetch()
return
}
forkPart({ variables: { input } })
}
return (
<IdeCascadeStudio