Skip to content

Commit

Permalink
Merge pull request #100 from ssciwr/use_new_frontend_api
Browse files Browse the repository at this point in the history
Use new auto-generated client API in frontend admin interface
  • Loading branch information
lkeegan authored Oct 9, 2024
2 parents aeb25ce + c3382dc commit aaa5152
Show file tree
Hide file tree
Showing 22 changed files with 342 additions and 607 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
Expand All @@ -9,7 +9,7 @@ repos:
- id: check-toml

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.8
rev: v0.6.9
hooks:
- id: ruff
types_or: [python, pyi, jupyter]
Expand All @@ -30,7 +30,7 @@ repos:
]

- repo: https://github.com/rhysd/actionlint
rev: "v1.7.2"
rev: "v1.7.3"
hooks:
- id: actionlint

Expand Down
315 changes: 11 additions & 304 deletions frontend/src/lib/admin.ts
Original file line number Diff line number Diff line change
@@ -1,321 +1,28 @@
import { milestoneGroups, userQuestions } from '$lib/stores/adminStore';
import { getMilestoneGroupsAdmin, getUserQuestionsAdmin } from '$lib/client/services.gen';

export async function refreshMilestoneGroups() {
console.log('refreshMilestoneGroups...');
try {
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestone-groups/`, {
method: 'GET',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
});
const json = await res.json();
console.log(json);
if (res.status === 200) {
milestoneGroups.set(json);
} else {
console.log('Failed to get MilestoneGroups');
milestoneGroups.set([]);
}
} catch (e) {
console.error(e);
const { data, error } = await getMilestoneGroupsAdmin();
if (error || data == undefined) {
console.log('Failed to get MilestoneGroups');
milestoneGroups.set([]);
}
}

export async function newMilestoneGroup() {
console.log('newMilestoneGroup...');
try {
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestone-groups/`, {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
});
if (res.status === 200) {
const newGroup = await res.json();
console.log(newGroup);
await refreshMilestoneGroups();
return newGroup;
} else {
console.log('Failed to create new MilestoneGroup');
}
} catch (e) {
console.error(e);
}
return null;
}

export async function updateMilestoneGroup(milestoneGroup) {
console.log('updateMilestoneGroup...');
console.log(milestoneGroup);
try {
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestone-groups/`, {
method: 'PUT',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
},
body: JSON.stringify(milestoneGroup)
});
if (res.status === 200) {
const updatedGroup = await res.json();
console.log(updatedGroup);
await refreshMilestoneGroups();
return updatedGroup;
} else {
console.log('Failed to create new MilestoneGroup');
}
} catch (e) {
console.error(e);
}
return null;
}

export async function uploadMilestoneGroupImage(milestoneGroupId: number, file) {
console.log('uploadMilestoneGroupImage...');
try {
const formData = new FormData();
formData.append('file', file);
const res = await fetch(
`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestone-group-images/${milestoneGroupId}`,
{
method: 'PUT',
credentials: 'include',
body: formData
}
);
console.log(await res.json());
} catch (e) {
console.error(e);
}
}

export async function deleteMilestoneGroup(milestoneGroupId: number | null) {
try {
const res = await fetch(
`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestone-groups/${milestoneGroupId}`,
{
method: 'DELETE',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
}
);
const json = await res.json();
console.log(json);
if (res.status === 200) {
console.log(`Deleted MilestoneGroup with id ${milestoneGroupId}.`);
await refreshMilestoneGroups();
} else {
console.log(`Error deleting MilestoneGroup with id ${milestoneGroupId}.`);
}
} catch (e) {
console.error(e);
} else {
milestoneGroups.set(data);
}
}

export function milestoneGroupImageUrl(id: number) {
return `${import.meta.env.VITE_MONDEY_API_URL}/static/mg${id}.jpg`;
}

export async function newMilestone(milestoneGroupId: number) {
console.log('newMilestone...');
try {
const res = await fetch(
`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestones/${milestoneGroupId}`,
{
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
}
);
if (res.status === 200) {
const newMilestone = await res.json();
console.log(newMilestone);
await refreshMilestoneGroups();
return newMilestone;
} else {
console.log('Failed to create new Milestone');
}
} catch (e) {
console.error(e);
}
return null;
}

export async function updateMilestone(milestone) {
console.log('updateMilestone...');
console.log(milestone);
try {
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestones/`, {
method: 'PUT',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
},
body: JSON.stringify(milestone)
});
if (res.status === 200) {
const updatedMilestone = await res.json();
console.log(updatedMilestone);
return updatedMilestone;
} else {
console.log('Failed to update Milestone');
}
} catch (e) {
console.error(e);
}
return null;
}

export async function uploadMilestoneImage(milestoneId: number, file) {
console.log('uploadMilestoneImage...');
try {
const formData = new FormData();
formData.append('file', file);
const res = await fetch(
`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestone-images/${milestoneId}`,
{
method: 'POST',
credentials: 'include',
body: formData
}
);
console.log(await res.json());
} catch (e) {
console.error(e);
}
}

export async function deleteMilestone(milestoneId: number | null) {
try {
const res = await fetch(
`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestones/${milestoneId}`,
{
method: 'DELETE',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
}
);
const json = await res.json();
console.log(json);
if (res.status === 200) {
console.log(`Deleted Milestone with id ${milestoneId}.`);
await refreshMilestoneGroups();
} else {
console.log(`Error deleting Milestone with id ${milestoneId}.`);
}
} catch (e) {
console.error(e);
}
}

export async function refreshUserQuestions() {
console.log('refreshQuestions...');
try {
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/user-questions/`, {
method: 'GET',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
});
const json = await res.json();
console.log(json);
if (res.status === 200) {
userQuestions.set(json);
} else {
console.log('Failed to get UserQuestions');
userQuestions.set([]);
}
} catch (e) {
console.error(e);
const { data, error } = await getUserQuestionsAdmin();
if (error || data === undefined) {
console.log('Failed to get UserQuestions');
userQuestions.set([]);
} else {
userQuestions.set(data);
}
}

export async function newUserQuestion() {
try {
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/user-questions/`, {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json'
}
});
if (res.status === 200) {
const newUserQuestion = await res.json();
console.log(newUserQuestion);
await refreshUserQuestions();
return newUserQuestion;
} else {
console.log('Failed to create new Question');
}
} catch (e) {
console.error(e);
}
}

export async function updateUserQuestion(userQuestion) {
console.log('updateUserQuestion...');
console.log(userQuestion);
try {
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/user-questions/`, {
method: 'PUT',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
},
body: JSON.stringify(userQuestion)
});
if (res.status === 200) {
const updatedUserQuestion = await res.json();
console.log(updatedUserQuestion);
await refreshUserQuestions();
return updatedUserQuestion;
} else {
console.log('Failed to create new UserQuestion');
}
} catch (e) {
console.error(e);
}
return null;
}

export async function deleteUserQuestion(id: number) {
try {
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/user-questions/${id}`, {
method: 'DELETE',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
});
if (res.status === 200) {
await refreshUserQuestions();
} else {
console.log('Failed to delete Question');
}
} catch (e) {
console.error(e);
}
return null;
}
34 changes: 1 addition & 33 deletions frontend/src/lib/client/schemas.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,39 +245,7 @@ export const MilestoneAdminSchema = {
title: 'MilestoneAdmin'
} as const;

export const MilestoneGroupAdmin_InputSchema = {
properties: {
id: {
type: 'integer',
title: 'Id'
},
order: {
type: 'integer',
title: 'Order'
},
text: {
additionalProperties: {
$ref: '#/components/schemas/MilestoneGroupText'
},
type: 'object',
title: 'Text',
default: {}
},
milestones: {
items: {
$ref: '#/components/schemas/MilestoneAdmin'
},
type: 'array',
title: 'Milestones',
default: []
}
},
type: 'object',
required: ['id', 'order'],
title: 'MilestoneGroupAdmin'
} as const;

export const MilestoneGroupAdmin_OutputSchema = {
export const MilestoneGroupAdminSchema = {
properties: {
id: {
type: 'integer',
Expand Down
Loading

0 comments on commit aaa5152

Please sign in to comment.