Skip to content

Commit

Permalink
bugfix 564 / fix for 500 errors (#159)
Browse files Browse the repository at this point in the history
* bump version

* add optional sha parameter to save

* bump version

---------

Co-authored-by: PhotoNomad0 <[email protected]>
  • Loading branch information
PhotoNomad0 and PhotoNomad0 authored Nov 16, 2023
1 parent bc99eb0 commit 8716063
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gitea-react-toolkit",
"version": "2.3.0",
"version": "2.4.0",
"license": "MIT",
"description": "A Gitea API React Toolkit Component Library",
"homepage": "https://gitea-react-toolkit.netlify.com/",
Expand Down
37 changes: 25 additions & 12 deletions src/components/file/useEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,20 @@ export default function useEdit({
* @async
* @param {string} _branch - branch name to save content to
* @param {string} newContent - Stringified content to be saved to DCS
* @param {string|null} fileSha - optional file sha to be used, if not passed then sha will be used
* @returns {Promise<Object>} - Response after saving the content.
*/
async function saveContent(_branch, newContent) {
async function saveContent(_branch, newContent, fileSha = null) {
setState((prevState) => ({
...prevState,
editResponse: null,
isEditing: true,
isError: false,
}))


const _sha = fileSha || sha
const response = await updateContent({
sha,
sha: _sha,
repo,
owner,
config,
Expand All @@ -82,18 +84,19 @@ export default function useEdit({
* during the save.
* @async
* @param {string} _branch - branch name to save content to
* @param {string} newContent - Stringified content to be saved to DCS
* @param {string} newContent - optional Stringified content to be saved to DCS, if not passed then value in content will be used
* @param {string|null} fileSha - optional file sha to be used, if not passed then sha will be used
* @returns {Promise<boolean>} - Returns true if successful, otherwise false.
*/
async function onSaveEdit(_branch, newContent='') {
async function onSaveEdit(_branch, newContent='', fileSha = null) {
try {
if (newContent) {
if (content && content === newContent) {
return true
}
await saveContent(_branch, newContent)
await saveContent(_branch, newContent, fileSha)
} else if (content) {
await saveContent(_branch, content)
await saveContent(_branch, content, fileSha)
} else {
console.warn('Content and newContent values are empty')
}
Expand All @@ -109,10 +112,20 @@ export default function useEdit({
}
}

async function onSaveEditPatch(_branch) {
/**
* Saves a patch to given branch and catches any errors that happen
* during the save.
* @async
* @param {string} _branch - branch name to save content to
* @param {string} newContent - optional patched content to be saved to DCS, if not passed then value in content will be used
* @param {string|null} fileSha - optional file sha to be used, if not passed then sha will be used
* @returns {Promise<boolean>} - Returns true if successful, otherwise false.
*/
async function onSaveEditPatch(_branch, newContent= '', fileSha = null) {
try {
// content is the updated string or dirty content.
if (content) {
const _content = newContent || content
const _sha = fileSha || sha
if (_content) {
// clear state to remove left over state from a previous edit.
setState((prevState) => ({
...prevState,
Expand All @@ -122,13 +135,13 @@ export default function useEdit({
}))

const response = await patchContent({
sha,
sha: _sha,
repo,
owner,
config,
author,
email,
content,
content: _content,
filepath,
message: _message,
// Use branch passed to function or branch passed to custom hook.
Expand Down

0 comments on commit 8716063

Please sign in to comment.