Skip to content

Commit

Permalink
Merge pull request #116 from ssciwr/fix_110_milestone_answer
Browse files Browse the repository at this point in the history
Add MilestoneAnswers
  • Loading branch information
MaHaWo authored Oct 17, 2024
2 parents 5e2c9bc + 5cc3927 commit 62ddbe4
Show file tree
Hide file tree
Showing 12 changed files with 513 additions and 57 deletions.
56 changes: 46 additions & 10 deletions frontend/src/lib/client/schemas.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,10 @@ export const ChildCreateSchema = {
birth_month: {
type: 'integer',
title: 'Birth Month'
},
has_image: {
type: 'boolean',
title: 'Has Image'
}
},
type: 'object',
required: ['birth_year', 'birth_month', 'has_image'],
required: ['birth_year', 'birth_month'],
title: 'ChildCreate'
} as const;

Expand All @@ -189,17 +185,17 @@ export const ChildPublicSchema = {
type: 'integer',
title: 'Birth Month'
},
has_image: {
type: 'boolean',
title: 'Has Image'
},
id: {
type: 'integer',
title: 'Id'
},
has_image: {
type: 'boolean',
title: 'Has Image'
}
},
type: 'object',
required: ['birth_year', 'birth_month', 'has_image', 'id'],
required: ['birth_year', 'birth_month', 'id', 'has_image'],
title: 'ChildPublic'
} as const;

Expand Down Expand Up @@ -312,6 +308,46 @@ export const MilestoneAdminSchema = {
title: 'MilestoneAdmin'
} as const;

export const MilestoneAnswerPublicSchema = {
properties: {
milestone_id: {
type: 'integer',
title: 'Milestone Id'
},
answer: {
type: 'integer',
title: 'Answer'
}
},
type: 'object',
required: ['milestone_id', 'answer'],
title: 'MilestoneAnswerPublic'
} as const;

export const MilestoneAnswerSessionPublicSchema = {
properties: {
id: {
type: 'integer',
title: 'Id'
},
created_at: {
type: 'string',
format: 'date-time',
title: 'Created At'
},
answers: {
additionalProperties: {
$ref: '#/components/schemas/MilestoneAnswerPublic'
},
type: 'object',
title: 'Answers'
}
},
type: 'object',
required: ['id', 'created_at', 'answers'],
title: 'MilestoneAnswerSessionPublic'
} as const;

export const MilestoneGroupAdminSchema = {
properties: {
id: {
Expand Down
62 changes: 50 additions & 12 deletions frontend/src/lib/client/services.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ import type {
UsersDeleteUserResponse,
GetChildrenError,
GetChildrenResponse,
CreateChildData,
CreateChildError,
CreateChildResponse,
UpdateChildData,
UpdateChildError,
UpdateChildResponse,
CreateChildData,
CreateChildError,
CreateChildResponse,
DeleteChildData,
DeleteChildError,
DeleteChildResponse,
Expand All @@ -97,6 +97,12 @@ import type {
UploadChildImageData,
UploadChildImageError,
UploadChildImageResponse,
GetCurrentMilestoneAnswerSessionData,
GetCurrentMilestoneAnswerSessionError,
GetCurrentMilestoneAnswerSessionResponse,
UpdateMilestoneAnswerData,
UpdateMilestoneAnswerError,
UpdateMilestoneAnswerResponse,
AuthCookieLoginData,
AuthCookieLoginError,
AuthCookieLoginResponse,
Expand Down Expand Up @@ -558,26 +564,26 @@ export const getChildren = <ThrowOnError extends boolean = false>(
};

/**
* Create Child
* Update Child
*/
export const createChild = <ThrowOnError extends boolean = false>(
options: Options<CreateChildData, ThrowOnError>
export const updateChild = <ThrowOnError extends boolean = false>(
options: Options<UpdateChildData, ThrowOnError>
) => {
return (options?.client ?? client).post<CreateChildResponse, CreateChildError, ThrowOnError>({
return (options?.client ?? client).put<UpdateChildResponse, UpdateChildError, ThrowOnError>({
...options,
url: '/users/children/'
});
};

/**
* Update Child
* Create Child
*/
export const updateChild = <ThrowOnError extends boolean = false>(
options: Options<UpdateChildData, ThrowOnError>
export const createChild = <ThrowOnError extends boolean = false>(
options: Options<CreateChildData, ThrowOnError>
) => {
return (options?.client ?? client).put<UpdateChildResponse, UpdateChildError, ThrowOnError>({
return (options?.client ?? client).post<CreateChildResponse, CreateChildError, ThrowOnError>({
...options,
url: '/users/children'
url: '/users/children/'
});
};

Expand Down Expand Up @@ -626,6 +632,38 @@ export const uploadChildImage = <ThrowOnError extends boolean = false>(
});
};

/**
* Get Current Milestone Answer Session
*/
export const getCurrentMilestoneAnswerSession = <ThrowOnError extends boolean = false>(
options: Options<GetCurrentMilestoneAnswerSessionData, ThrowOnError>
) => {
return (options?.client ?? client).get<
GetCurrentMilestoneAnswerSessionResponse,
GetCurrentMilestoneAnswerSessionError,
ThrowOnError
>({
...options,
url: '/users/milestone-answers/{child_id}'
});
};

/**
* Update Milestone Answer
*/
export const updateMilestoneAnswer = <ThrowOnError extends boolean = false>(
options: Options<UpdateMilestoneAnswerData, ThrowOnError>
) => {
return (options?.client ?? client).put<
UpdateMilestoneAnswerResponse,
UpdateMilestoneAnswerError,
ThrowOnError
>({
...options,
url: '/users/milestone-answers/{milestone_answer_session_id}'
});
};

/**
* Auth:Cookie.Login
*/
Expand Down
53 changes: 43 additions & 10 deletions frontend/src/lib/client/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ export type ChildCreate = {
name?: string;
birth_year: number;
birth_month: number;
has_image: boolean;
};

export type ChildPublic = {
name?: string;
birth_year: number;
birth_month: number;
has_image: boolean;
id: number;
has_image: boolean;
};

export type ErrorModel = {
Expand Down Expand Up @@ -85,6 +84,19 @@ export type MilestoneAdmin = {
images?: Array<MilestoneImage>;
};

export type MilestoneAnswerPublic = {
milestone_id: number;
answer: number;
};

export type MilestoneAnswerSessionPublic = {
id: number;
created_at: string;
answers: {
[key: string]: MilestoneAnswerPublic;
};
};

export type MilestoneGroupAdmin = {
id: number;
order: number;
Expand Down Expand Up @@ -434,14 +446,6 @@ export type GetChildrenResponse = Array<ChildPublic>;

export type GetChildrenError = unknown;

export type CreateChildData = {
body: ChildCreate;
};

export type CreateChildResponse = ChildPublic;

export type CreateChildError = HTTPValidationError;

export type UpdateChildData = {
body: ChildPublic;
};
Expand All @@ -450,6 +454,14 @@ export type UpdateChildResponse = ChildPublic;

export type UpdateChildError = HTTPValidationError;

export type CreateChildData = {
body: ChildCreate;
};

export type CreateChildResponse = ChildPublic;

export type CreateChildError = HTTPValidationError;

export type DeleteChildData = {
path: {
child_id: number;
Expand Down Expand Up @@ -481,6 +493,27 @@ export type UploadChildImageResponse = unknown;

export type UploadChildImageError = HTTPValidationError;

export type GetCurrentMilestoneAnswerSessionData = {
path: {
child_id: number;
};
};

export type GetCurrentMilestoneAnswerSessionResponse = MilestoneAnswerSessionPublic;

export type GetCurrentMilestoneAnswerSessionError = HTTPValidationError;

export type UpdateMilestoneAnswerData = {
body: MilestoneAnswerPublic;
path: {
milestone_answer_session_id: number;
};
};

export type UpdateMilestoneAnswerResponse = MilestoneAnswerPublic;

export type UpdateMilestoneAnswerError = HTTPValidationError;

export type AuthCookieLoginData = {
body: Body_auth_cookie_login_auth_login_post;
};
Expand Down
58 changes: 43 additions & 15 deletions frontend/src/lib/components/Milestone.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,42 @@
ArrowRightOutline,
ArrowLeftOutline
} from 'flowbite-svelte-icons';
import type { MilestoneGroupPublic, MilestonePublic } from '$lib/client/types.gen';
import type {
MilestoneGroupPublic,
MilestonePublic,
MilestoneAnswerSessionPublic
} from '$lib/client/types.gen';
import { updateMilestoneAnswer } from '$lib/client/services.gen';
import MilestoneButton from '$lib/components/MilestoneButton.svelte';
import { lang_id } from '$lib/stores/langStore';
import { _ } from 'svelte-i18n';
let {
milestoneGroup = undefined,
milestoneAnswers = {}
}: { milestoneGroup?: MilestoneGroupPublic; milestoneAnswers?: Record<string, number> } =
$props();
milestoneAnswerSession = undefined
}: {
milestoneGroup?: MilestoneGroupPublic;
milestoneAnswerSession?: MilestoneAnswerSessionPublic;
} = $props();
let currentMilestoneIndex = $state(0);
let currentMilestone = $state(undefined as MilestonePublic | undefined);
let selectedAnswer = $state(undefined as number | undefined);
let selectedAnswer = $derived(
milestoneAnswerSession?.answers?.[`${currentMilestone?.id}`]?.answer
);
let autoGoToNextMilestone = $state(false);
let currentImageIndex = $state(0);
onMount(() => {
console.log(milestoneGroup);
if (milestoneGroup && milestoneGroup.milestones) {
currentMilestone = milestoneGroup.milestones[currentMilestoneIndex];
selectedAnswer = milestoneAnswers[currentMilestone.id];
}
});
const imageInterval = 5000;
setInterval(() => {
if (currentMilestone && currentMilestone.images) {
if (currentMilestone && currentMilestone.images && currentMilestone.images.length > 0) {
currentImageIndex = (currentImageIndex + 1) % currentMilestone.images.length;
}
}, imageInterval);
Expand All @@ -54,34 +62,54 @@
return;
}
currentMilestoneIndex -= 1;
currentImageIndex = 0;
currentMilestone = milestoneGroup.milestones[currentMilestoneIndex];
selectedAnswer = milestoneAnswers[currentMilestone.id];
}
function nextMilestone() {
async function nextMilestone() {
if (
!milestoneGroup ||
!milestoneGroup.milestones ||
!currentMilestone ||
selectedAnswer === undefined
selectedAnswer === undefined ||
!milestoneAnswerSession
) {
return;
}
milestoneAnswers[currentMilestone.id] = selectedAnswer;
// todo: API call to submit answer
console.log(`TODO: submit answer ${selectedAnswer} for milestone ${currentMilestoneIndex}`);
const { data, error } = await updateMilestoneAnswer({
body: { milestone_id: currentMilestone.id, answer: selectedAnswer },
path: {
milestone_answer_session_id: milestoneAnswerSession.id
}
});
if (error) {
console.log(error);
return;
}
milestoneAnswerSession.answers[`${currentMilestone.id}`] = data;
if (currentMilestoneIndex + 1 == milestoneGroup.milestones.length) {
console.log(`TODO: redirect to next milestone group or back to group overview`);
// todo: redirect to bereichuebersicht? or go to next set of milestones?
return;
}
currentMilestoneIndex += 1;
currentImageIndex = 0;
currentMilestone = milestoneGroup.milestones[currentMilestoneIndex];
selectedAnswer = milestoneAnswers[currentMilestone.id];
}
function selectAnswer(answer: number | undefined) {
selectedAnswer = answer;
if (!currentMilestone) {
console.log('selectAnswer: missing currentMilestone');
return;
}
if (!milestoneAnswerSession) {
console.log('selectAnswer: missing milestoneAnswerSession');
return;
}
milestoneAnswerSession.answers[`${currentMilestone.id}`] = {
milestone_id: currentMilestone.id,
answer: answer
};
if (selectedAnswer !== undefined && autoGoToNextMilestone) {
nextMilestone();
}
Expand Down
Loading

0 comments on commit 62ddbe4

Please sign in to comment.