Skip to content

Commit

Permalink
[import] handle readonly mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran committed Jan 20, 2024
1 parent a3b3afe commit 10eb989
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
11 changes: 8 additions & 3 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,23 @@ func Build(db *gorm.DB, enableCompression bool) *gin.Engine {
})

router.POST("/api/templates/upsert", func(c *gin.Context) {
if config.GetConfig().Readonly {
c.JSON(200, gin.H{"saved": false, "message": "Readonly mode"})
return
}

var t template.Template
if err := c.ShouldBindJSON(&t); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

c.JSON(200, template.Upsert(t.Name, t.Content))
c.JSON(200, gin.H{"template": template.Upsert(t.Name, t.Content), "saved": true})
})

router.POST("/api/templates/delete", func(c *gin.Context) {
if config.GetConfig().Readonly {
c.JSON(200, gin.H{})
c.JSON(200, gin.H{"success": false, "message": "Readonly mode"})
return
}

Expand All @@ -347,7 +352,7 @@ func Build(db *gorm.DB, enableCompression bool) *gin.Engine {
}

template.Delete(t.Name)
c.JSON(200, gin.H{})
c.JSON(200, gin.H{"success": true})
})

router.GET("/api/goals", func(c *gin.Context) {
Expand Down
7 changes: 5 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,11 @@ export function ajax(
export function ajax(
route: "/api/templates/upsert",
options?: RequestOptions
): Promise<ImportTemplate>;
export function ajax(route: "/api/templates/delete", options?: RequestOptions): Promise<void>;
): Promise<{ saved: boolean; message?: string; template: ImportTemplate }>;
export function ajax(
route: "/api/templates/delete",
options?: RequestOptions
): Promise<{ success: boolean; message?: string }>;

export function ajax(
route: "/api/editor/files",
Expand Down
26 changes: 23 additions & 3 deletions src/routes/(app)/ledger/import/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,26 @@
$: saveAsNameDuplicate = !!_.find(templates, { name: saveAsName, template_type: "custom" });
async function save() {
const { id } = await ajax("/api/templates/upsert", {
const { template, saved, message } = await ajax("/api/templates/upsert", {
method: "POST",
body: JSON.stringify({
name: saveAsName,
content: templateEditor.state.doc.toString()
}),
background: true
});
if (!saved) {
toast.toast({
message: `Failed to save ${saveAsName}. reason: ${message}`,
type: "is-danger",
duration: 10000
});
return;
}
({ templates } = await ajax("/api/templates", { background: true }));
selectedTemplate = _.find(templates, { id });
selectedTemplate = _.find(templates, { id: template.id });
saveAsName = selectedTemplate.name;
toast.toast({
message: `Saved ${saveAsName}`,
Expand All @@ -75,13 +85,23 @@
if (!confirmed) {
return;
}
await ajax("/api/templates/delete", {
const { success, message } = await ajax("/api/templates/delete", {
method: "POST",
body: JSON.stringify({
name: selectedTemplate.name
}),
background: true
});
if (!success) {
toast.toast({
message: `Failed to remove ${oldName}. reason: ${message}`,
type: "is-danger",
duration: 10000
});
return;
}
({ templates } = await ajax("/api/templates", { background: true }));
selectedTemplate = templates[0];
saveAsName = selectedTemplate.name;
Expand Down

0 comments on commit 10eb989

Please sign in to comment.