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

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;