Wordpress backend adapter implemented.

This commit is contained in:
HenriT
2021-08-26 16:13:39 +03:00
parent f6b85595df
commit 2bfebb7017
7 changed files with 113 additions and 22 deletions

15
package-lock.json generated
View File

@ -2199,6 +2199,21 @@
"integrity": "sha512-zg7Hz2k5lI8kb7U32998pRRFin7zJlkfezGJjUc2heaD4Pw2wObakCDVzkKztTm/Ln7eiVvYsjqak0Ed4LkMDA==",
"dev": true
},
"axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"requires": {
"follow-redirects": "^1.10.0"
},
"dependencies": {
"follow-redirects": {
"version": "1.14.2",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.2.tgz",
"integrity": "sha512-yLR6WaE2lbF0x4K2qE2p9PEXKLDjUjnR/xmjS3wHAYxtlsI9MLLBJUZirAHKzUZDGLxje7w/cXR49WOUo4rbsA=="
}
}
},
"babel-code-frame": {
"version": "6.26.0",
"resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz",

View File

@ -11,6 +11,7 @@
"@panter/vue-i18next": "^0.15.2",
"@vuex-orm/core": "^0.36.3",
"@vuex-orm/plugin-change-flags": "https://github.com/mareksmakosz/plugin-change-flags.git",
"axios": "^0.21.1",
"bootstrap": "^4.5.2",
"bootstrap-vue": "^2.17.3",
"core-js": "^2.6.5",

View File

@ -0,0 +1,4 @@
export default {
storageType: 'local',
wordpress_url: 'http://tih.test/wp-json/wp/v2/',
};

View File

@ -5,7 +5,3 @@ storage.config({
version: 1.0,
storeName: 'default',
});
export default {
type: 'local',
};

View File

@ -23,7 +23,9 @@ class LocalAdapter {
removeVuexORMFlags(data);
items.push(data);
return storage.setItem(uri, items);
await storage.setItem(uri, items);
return data;
}
async patch(uri, data) {
@ -36,7 +38,9 @@ class LocalAdapter {
removeVuexORMFlags(data);
items[index] = data;
return storage.setItem(parts[0], items);
await storage.setItem(parts[0], items);
return data;
}
return null;

View File

@ -1,4 +1,88 @@
import axios from 'axios';
import { removeVuexORMFlags } from '@/utils/helpers';
import config from '@/config/app.config';
const http = axios.create({
baseURL: config.wordpress_url,
});
class WordpressAdapter {
async get(uri) {
const parts = uri.split('/');
if (parts.length === 1) {
const res = await http.get(`si_${parts[0]}`, {
params: {
context: 'edit',
_fields: 'content.raw',
},
})
.then(r => r.data);
if (parts[0] === 'team') {
return res.length > 0 ? JSON.parse(res[0].content.raw) : null;
}
return res.map(item => JSON.parse(item.content.raw));
}
if (parts.length === 2) {
const res = await http.get(`si_${parts[0]}/${parts[1]}`, {
params: {
context: 'edit',
_fields: 'content.raw',
},
})
.then(r => r.data);
return JSON.parse(res.content.raw);
}
return null;
}
async post(uri, data) {
removeVuexORMFlags(data);
await http.post(`si_${uri}`, {
content: JSON.stringify(data),
status: 'publish',
slug: data.id,
});
return data;
}
async patch(uri, data) {
removeVuexORMFlags(data);
const parts = uri.split('/');
if (parts.length === 2) {
await http.post(`si_${parts[0]}/${parts[1]}`, {
content: JSON.stringify(data),
});
return data;
}
return null;
}
async put(uri, data) {
if (uri === 'team') {
const res = await this.get('team');
if (res) {
return this.patch(`${uri}/${res.id}`, data);
}
return this.post(uri, data);
}
return null;
}
async delete(uri) {
const parts = uri.split('/');
if (parts.length === 2) {
return http.delete(`si_${parts[0]}/${parts[1]}`);
}
return null;
}
}
export default new WordpressAdapter();

View File

@ -1,25 +1,12 @@
import data from '@/services/data.service';
import Team from '@/store/models/team';
class TeamService {
async getTeam() {
let team = await data.get('team');
if (!team) {
team = {
company_name: null,
company_address: null,
company_postal_code: null,
company_country: null,
company_county: null,
company_city: null,
website: null,
contact_email: null,
contact_phone: null,
invoice_late_fee: null,
invoice_due_days: null,
updated_at: null,
created_at: null,
logo_url: null,
};
team = new Team();
await this.updateTeam(team);
}
return team;