Skip to content

Commit

Permalink
Feature 533/speedup saving (#156)
Browse files Browse the repository at this point in the history
* adding patch support

* revert changes

* fix diffpatch uploads

* bump version

---------

Co-authored-by: PhotoNomad0 <[email protected]>
  • Loading branch information
PhotoNomad0 and PhotoNomad0 authored Sep 13, 2023
1 parent 7aaa1d2 commit ba59861
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 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.2.5-beta",
"version": "2.2.6",
"license": "MIT",
"description": "A Gitea API React Toolkit Component Library",
"homepage": "https://gitea-react-toolkit.netlify.com/",
Expand Down
51 changes: 50 additions & 1 deletion src/components/file/useEdit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from 'react';
import { updateContent } from '../..';
import { patchContent, updateContent } from '../..';

/**
* Custom hook for editing content of translation helps resources
Expand All @@ -10,6 +10,7 @@ import { updateContent } from '../..';
* @param {object} config - config settings for fetches (timeout, cache, etc.)
* @param {string} branch - branch name.
* @param {string} author - author of the edit.
* @param {string} email - email of the author.
* @param {string} content - Edited/updated content.
* @param {string} message - Optional commit message.
* @param {string} filePath - file path, file path for the file being edited.
Expand All @@ -23,6 +24,7 @@ export default function useEdit({
config,
branch,
author,
email,
content,
message,
filepath,
Expand Down Expand Up @@ -82,11 +84,58 @@ export default function useEdit({
}
}

async function onSaveEditPatch(_branch) {
try {
// content is the updated string or dirty content.
if (content) {
// clear state to remove left over state from a previous edit.
setState((prevState) => ({
...prevState,
editResponse: null,
isEditing: true,
isError: false,
}))

const response = await patchContent({
sha,
repo,
owner,
config,
author,
email,
content,
filepath,
message: _message,
// Use branch passed to function or branch passed to custom hook.
branch: _branch || branch,
});

setState((prevState) => ({
...prevState,
editResponse: response,
isEditing: false,
}))
return true
} else {
console.warn('Content value is empty')
}
} catch (error) {
setState((prevState) => ({
...prevState,
isError: true,
error,
isEditing: false,
}))
return false
}
}

return {
error,
isError,
isEditing,
onSaveEdit,
editResponse,
onSaveEditPatch,
}
}
42 changes: 42 additions & 0 deletions src/core/gitea-api/repos/contents/contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface ModifyContentOptions {
content?: string;
message: string;
author: Author;
email?: string;
sha?: string;
onOpenValidation?: (filename: string, content: string, url: string) => never;
};
Expand Down Expand Up @@ -114,6 +115,47 @@ export const readContent = async ({
return contentObject;
};

// POST /api/v1/repos/{owner}/{repo}/diffpatch
export const patchContent = async ({
config, owner, repo, branch, filepath, content, message, author, email, sha,
}: ModifyContentOptions): Promise<ContentObject> => {
const url = Path.join(apiPath, 'repos', owner, repo, 'diffpatch');
let contentObject: ContentObject;
const author_ = {
email: email || '',
name: author,
}
var date = new Date();
var isoDate = date.toISOString();

try {
const _payload =
{
author: author_,
branch,
committer: author_,
content: content || '',
from_path: ".",
dates: {
author: isoDate,
committer: isoDate
},
message,
sha,
signoff: true,
}
const response = await post({
url, payload: _payload, config,
});
contentObject = response.content;
} catch (e) {
console.warn('Failed to upload to user branch', e);
throw e;
}

return contentObject;
};

// PUT /api/v1/repos/{owner}/{repo}/contents/{filepath}
export const updateContent = async ({
config, owner, repo, branch, filepath, content, message, author, sha,
Expand Down

0 comments on commit ba59861

Please sign in to comment.