Skip to content

Commit 99148e9

Browse files
authored
Merge pull request #52514 from nextcloud/feat/template-field-extraction-improvements
feat: only get template fields once selected
2 parents 5129a79 + 9a349ed commit 99148e9

File tree

18 files changed

+275
-14
lines changed

18 files changed

+275
-14
lines changed

apps/files/appinfo/routes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@
136136
'url' => '/api/v1/templates',
137137
'verb' => 'GET'
138138
],
139+
[
140+
'name' => 'Template#listTemplateFields',
141+
'url' => '/api/v1/templates/fields/{fileId}',
142+
'verb' => 'GET'
143+
],
139144
[
140145
'name' => 'Template#create',
141146
'url' => '/api/v1/templates/create',

apps/files/lib/Controller/TemplateController.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ public function list(): DataResponse {
5252
}, $this->templateManager->listTemplates()));
5353
}
5454

55+
/**
56+
* List the fields for the template specified by the given file ID
57+
*
58+
* @param int $fileId File ID of the template
59+
* @return DataResponse<Http::STATUS_OK, array<string, FilesTemplateField>, array{}>
60+
*
61+
* 200: Fields returned
62+
*/
63+
#[NoAdminRequired]
64+
public function listTemplateFields(int $fileId): DataResponse {
65+
$fields = $this->templateManager->listTemplateFields($fileId);
66+
67+
return new DataResponse(
68+
array_merge([], ...$fields),
69+
Http::STATUS_OK
70+
);
71+
}
72+
5573
/**
5674
* Create a template
5775
*

apps/files/openapi.json

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,80 @@
12331233
}
12341234
}
12351235
},
1236+
"/ocs/v2.php/apps/files/api/v1/templates/fields/{fileId}": {
1237+
"get": {
1238+
"operationId": "template-list-template-fields",
1239+
"summary": "List the fields for the template specified by the given file ID",
1240+
"tags": [
1241+
"template"
1242+
],
1243+
"security": [
1244+
{
1245+
"bearer_auth": []
1246+
},
1247+
{
1248+
"basic_auth": []
1249+
}
1250+
],
1251+
"parameters": [
1252+
{
1253+
"name": "fileId",
1254+
"in": "path",
1255+
"description": "File ID of the template",
1256+
"required": true,
1257+
"schema": {
1258+
"type": "integer",
1259+
"format": "int64"
1260+
}
1261+
},
1262+
{
1263+
"name": "OCS-APIRequest",
1264+
"in": "header",
1265+
"description": "Required to be true for the API request to pass",
1266+
"required": true,
1267+
"schema": {
1268+
"type": "boolean",
1269+
"default": true
1270+
}
1271+
}
1272+
],
1273+
"responses": {
1274+
"200": {
1275+
"description": "Fields returned",
1276+
"content": {
1277+
"application/json": {
1278+
"schema": {
1279+
"type": "object",
1280+
"required": [
1281+
"ocs"
1282+
],
1283+
"properties": {
1284+
"ocs": {
1285+
"type": "object",
1286+
"required": [
1287+
"meta",
1288+
"data"
1289+
],
1290+
"properties": {
1291+
"meta": {
1292+
"$ref": "#/components/schemas/OCSMeta"
1293+
},
1294+
"data": {
1295+
"type": "object",
1296+
"additionalProperties": {
1297+
"$ref": "#/components/schemas/TemplateField"
1298+
}
1299+
}
1300+
}
1301+
}
1302+
}
1303+
}
1304+
}
1305+
}
1306+
}
1307+
}
1308+
}
1309+
},
12361310
"/ocs/v2.php/apps/files/api/v1/templates/create": {
12371311
"post": {
12381312
"operationId": "template-create",

apps/files/src/services/Templates.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export const getTemplates = async function() {
1111
return response.data.ocs.data
1212
}
1313

14+
export const getTemplateFields = async function(fileId) {
15+
const response = await axios.get(generateOcsUrl(`apps/files/api/v1/templates/fields/${fileId}`))
16+
return response.data.ocs.data
17+
}
18+
1419
/**
1520
* Create a new file from a specified template
1621
*

apps/files/src/views/TemplatePicker.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import { translate as t } from '@nextcloud/l10n'
5757
import { generateRemoteUrl } from '@nextcloud/router'
5858
import { normalize, extname, join } from 'path'
5959
import { defineComponent } from 'vue'
60-
import { createFromTemplate, getTemplates } from '../services/Templates.js'
60+
import { createFromTemplate, getTemplates, getTemplateFields } from '../services/Templates.js'
6161
6262
import NcEmptyContent from '@nextcloud/vue/components/NcEmptyContent'
6363
import NcModal from '@nextcloud/vue/components/NcModal'
@@ -215,7 +215,7 @@ export default defineComponent({
215215
}
216216
},
217217
218-
async createFile(templateFields) {
218+
async createFile(templateFields = []) {
219219
const currentDirectory = new URL(window.location.href).searchParams.get('dir') || '/'
220220
221221
// If the file doesn't have an extension, add the default one
@@ -274,9 +274,12 @@ export default defineComponent({
274274
},
275275
276276
async onSubmit() {
277-
if (this.selectedTemplate?.fields?.length > 0) {
277+
const fileId = this.selectedTemplate?.fileid
278+
const fields = await getTemplateFields(fileId)
279+
280+
if (fields.length > 0) {
278281
spawnDialog(TemplateFiller, {
279-
fields: this.selectedTemplate.fields,
282+
fields,
280283
onSubmit: this.createFile,
281284
})
282285
} else {

dist/4052-4052.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/4052-4052.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/4052-4052.js.map.license

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4052-4052.js.license

dist/7950-7950.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)