Skip to content

Commit

Permalink
fix: improve unsaved notifaction and repo url update
Browse files Browse the repository at this point in the history
  • Loading branch information
dmijatovic committed Feb 4, 2022
1 parent dc9f314 commit 8f9faa2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 96 deletions.
1 change: 0 additions & 1 deletion frontend/components/form/MarkdownInputWithPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export default function MarkdownInputWithPreview({markdown,register,disabled=tru
rows={20}
className="text-secondary w-full h-full py-4 px-8 font-mono text-sm"
onInput={({target}:{target:any}) => {
// debugger
target.style.height = ''
target.style.height = target.scrollHeight + 'px'
}}
Expand Down
1 change: 0 additions & 1 deletion frontend/components/software/CitationDownload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export default function CitationFormat({citation}: { citation: SoftwareCitationC

function onFormatChange({target}:{target:SelectChangeEvent['target']}){
if (target?.value) {
debugger
setFormat({
v: target?.value,
f: options[parseInt(target.value)].format,
Expand Down
1 change: 0 additions & 1 deletion frontend/components/software/edit/SoftwareInformation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ export default function SoftwareInformation({slug,token}:{slug:string,token: str
)

function onSubmit(formData: SoftwareItem) {
debugger
updateSoftwareInfo({
software: formData,
token
Expand Down
7 changes: 6 additions & 1 deletion frontend/components/software/edit/editSoftwareContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ export function editSoftwareReducer(state: EditSoftwareState = initialState, act
case EditSoftwareActionType.SET_EDIT_STEP:
return {
...state,
step: action.payload
// default values
isDirty: false,
isValid: true,
loading: true,
// new step
step: action.payload,
}
case EditSoftwareActionType.SET_SOFTWARE_INFO:
return {
Expand Down
170 changes: 78 additions & 92 deletions frontend/utils/editSoftware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,27 @@ export async function getSoftwareToEdit({slug, token, baseUrl}:
export async function updateSoftwareInfo({software, token}:
{software:SoftwareItem,token:string}) {
try {
// debugger
// NOTE! update this list when
const softwareTable = getPropsFromObject(software, SoftwarePropsToSave)
const repoTable = {
id: software?.repository_url[0].id,
software: software.id,
url: software?.repository_url[0].url
}
const promises = [updateSoftwareTable({software: softwareTable, token})]
// decide on repo table action
if (repoTable.url != '') {
if (repoTable.id){
promises.push(updateRepositoryTable({data: repoTable, token}))
}else {
promises.push(addToRepositoryTable({data: repoTable, token}))
}
} else if (repoTable.url === '' && repoTable.id) {
// not possible to foreign key relations - do nothing for now
// promises.push(deleteFromRepositoryTable({data: repoTable, token}))
}

const [respSoftware,respRepo] = await Promise.all([
updateSoftwareTable({
software:softwareTable, token
}),
repoTable.id
? updateRepositoryTable({data: repoTable, token})
: addToRepositoryTable({data: repoTable,token})
])
const [respSoftware, respRepo] = await Promise.all(promises)

// both OK
if ([200, 204].includes(respSoftware.status) &&
Expand Down Expand Up @@ -133,34 +137,9 @@ export async function updateSoftwareTable({software, token}:
},
body: JSON.stringify(software)
})
// OK
if ([200,204].includes(resp.status)) {
// just return id
return {
status: 200,
message: software.id
}
}
// not authorized, 404 seem to be returned mostly
if ([401,403,404].includes(resp.status)) {
return {
status: resp.status,
message: `
${resp.statusText}.
You might not have sufficient priveleges to edit this software.
Please contact site administrators.
`
}
} else {
return {
status: resp.status,
message: `
Failed to save changes.
${resp.statusText}.
Please contact site administrators.
`
}
}

return extractReturnMessage(resp, software.id)

} catch (e: any) {
logger(`updateSoftware: ${e?.message}`, 'error')
return {
Expand All @@ -183,34 +162,9 @@ export async function updateRepositoryTable({data, token}:
},
body: JSON.stringify(data)
})
// OK
if ([200, 204].includes(resp.status)) {
// just return id
return {
status: 200,
message: data.id
}
}
// not authorized, 404 seem to be returned mostly
if ([401, 403, 404].includes(resp.status)) {
return {
status: resp.status,
message: `
${resp.statusText}.
You might not have sufficient priveleges to edit this software.
Please contact site administrators.
`
}
} else {
return {
status: resp.status,
message: `
Failed to save changes.
${resp.statusText}.
Please contact site administrators.
`
}
}

return extractReturnMessage(resp, data.id ?? '')

} catch (e: any) {
logger(`updateSoftware: ${e?.message}`, 'error')
return {
Expand All @@ -233,34 +187,33 @@ export async function addToRepositoryTable({data, token}:
},
body: JSON.stringify(data)
})
// OK
if ([200, 201].includes(resp.status)) {
// just return id
return {
status: 200,
message: data.id
}
}
// not authorized, 404 seem to be returned mostly
if ([401, 403, 404].includes(resp.status)) {
return {
status: resp.status,
message: `
${resp.statusText}.
You might not have sufficient priveleges to edit this software.
Please contact site administrators.
`
}
} else {
return {
status: resp.status,
message: `
Failed to save changes.
${resp.statusText}.
Please contact site administrators.
`
}

return extractReturnMessage(resp, data.id ?? '')

} catch (e: any) {
logger(`updateSoftware: ${e?.message}`, 'error')
return {
status: 500,
message: e?.message
}
}
}

export async function deleteFromRepositoryTable({data, token}:
{ data: RepositoryUrl, token: string }) {
try {
// PATCH
const url = `/api/v1/repository_url?id=eq.${data.id}`
const resp = await fetch(url, {
method: 'DELETE',
headers: {
...createHeaders(token)
},
body: JSON.stringify(data)
})

return extractReturnMessage(resp, data.id ?? '')

} catch (e: any) {
logger(`updateSoftware: ${e?.message}`, 'error')
return {
Expand All @@ -270,6 +223,7 @@ export async function addToRepositoryTable({data, token}:
}
}


export async function isMaintainerOfSoftware({slug, uid, token}:
{slug: string, uid: string, token: string}) {
try {
Expand Down Expand Up @@ -300,3 +254,35 @@ export async function isMaintainerOfSoftware({slug, uid, token}:
return false
}
}


function extractReturnMessage(resp:Response, dataId:string) {
// OK
if ([200,201,204].includes(resp.status)) {
// just return id
return {
status: 200,
message: dataId
}
}
// not authorized, 404 seem to be returned mostly
if ([401, 403, 404].includes(resp.status)) {
return {
status: resp.status,
message: `
${resp.statusText}.
You might not have sufficient priveleges to edit this software.
Please contact site administrators.
`
}
} else {
return {
status: resp.status,
message: `
Failed to save changes.
${resp.statusText}.
Please contact site administrators.
`
}
}
}

0 comments on commit 8f9faa2

Please sign in to comment.