Skip to content

Commit 94477ea

Browse files
feat: ✨ added move and search functions for terms implementation
1 parent d063441 commit 94477ea

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

lib/entity.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,16 @@ export const fetchAll = (http, wrapperCollection, params = {}) => {
235235
}
236236
}
237237

238-
export function parseData (response, stackHeaders, contentTypeUID, taxonomyUID) {
238+
export function parseData (response, stackHeaders, contentTypeUID, taxonomy_uid) {
239239
const data = response.data || {}
240240
if (stackHeaders) {
241241
data.stackHeaders = stackHeaders
242242
}
243243
if (contentTypeUID) {
244244
data.content_type_uid = contentTypeUID
245245
}
246-
if (taxonomyUID) {
247-
data.taxonomy_uid = taxonomyUID
246+
if (taxonomy_uid) {
247+
data.taxonomy_uid = taxonomy_uid
248248
}
249249
return data
250250
}
@@ -270,3 +270,35 @@ export async function get (http, url, params, data) {
270270
throw error(err)
271271
}
272272
}
273+
274+
export const move = (http, type, force = false, params = {}) => {
275+
return async function (param = {}) {
276+
try {
277+
let updateData = {}
278+
const json = cloneDeep(this)
279+
delete json.parent_uid
280+
if (type) {
281+
updateData[type] = json
282+
} else {
283+
updateData = json
284+
}
285+
const headers = {
286+
headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) },
287+
params: {
288+
...cloneDeep(param)
289+
}
290+
} || {}
291+
if (force === true) {
292+
headers.params.force = true
293+
}
294+
const response = await http.put(`${this.urlPath}/move`, updateData, headers)
295+
if (response.data) {
296+
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid))
297+
} else {
298+
throw error(response)
299+
}
300+
} catch (err) {
301+
throw error(err)
302+
}
303+
}
304+
}

lib/stack/taxonomy/terms/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
update,
66
query,
77
deleteEntity,
8+
move,
89
parseData
910
} from '../../../entity'
1011

@@ -89,6 +90,7 @@ export function Terms (http, data) {
8990
throw err
9091
}
9192
}
93+
this.move = move(http, 'term')
9294
} else {
9395
/**
9496
* @description The Create terms call is used to create a terms.
@@ -126,6 +128,18 @@ export function Terms (http, data) {
126128
*/
127129
this.query = query({ http: http, wrapperCollection: TermsCollection })
128130
}
131+
this.search = async (term = '', params = {}) => {
132+
try {
133+
const headers = {
134+
headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) }
135+
}
136+
const response = await http.get(`taxonomies/${this.taxonomy_uid}/terms?term=${term}`, headers)
137+
return parseData(response, this.stackHeaders)
138+
} catch (err) {
139+
console.error(err)
140+
throw err
141+
}
142+
}
129143
}
130144
export function TermsCollection (http, data) {
131145
const obj = cloneDeep(data.terms) || []

0 commit comments

Comments
 (0)