forked from jamesp/sasa-membership
d024bf7fa3
- ui has been made 'kinda better' (after making it worse for a while lol - ESP rfid readers are now supported [ill upload the code for them in another repo later] - admin system has been secured a bit better and seems to be working well
67 lines
1.5 KiB
TypeScript
67 lines
1.5 KiB
TypeScript
export type ProfileQuestionAnswerValue = string | number | boolean | null;
|
|
|
|
export interface EditableProfileQuestion {
|
|
id: number;
|
|
admin_only_edit: boolean;
|
|
can_edit: boolean;
|
|
}
|
|
|
|
export interface DependentProfileQuestion {
|
|
id: number;
|
|
depends_on_question_id: number | null;
|
|
depends_on_value: string | null;
|
|
}
|
|
|
|
export const answerToComparable = (value: ProfileQuestionAnswerValue): string | null => {
|
|
if (value === null || value === undefined) {
|
|
return null;
|
|
}
|
|
|
|
if (typeof value === 'boolean') {
|
|
return value ? 'true' : 'false';
|
|
}
|
|
|
|
return String(value);
|
|
};
|
|
|
|
export const canEditProfileQuestion = (
|
|
question: EditableProfileQuestion,
|
|
allowAdminManagedEdit = false
|
|
): boolean => {
|
|
if (allowAdminManagedEdit) {
|
|
return true;
|
|
}
|
|
|
|
if (!question.can_edit) {
|
|
return false;
|
|
}
|
|
|
|
if (question.admin_only_edit) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
export const isProfileQuestionVisible = <TQuestion extends DependentProfileQuestion>(
|
|
question: TQuestion,
|
|
questionsById: Map<number, TQuestion>,
|
|
answers: Record<number, ProfileQuestionAnswerValue>
|
|
): boolean => {
|
|
if (!question.depends_on_question_id) {
|
|
return true;
|
|
}
|
|
|
|
const parentQuestion = questionsById.get(question.depends_on_question_id);
|
|
if (!parentQuestion) {
|
|
return true;
|
|
}
|
|
|
|
const parentAnswer = answerToComparable(answers[parentQuestion.id] ?? null);
|
|
if (question.depends_on_value === null || question.depends_on_value === undefined) {
|
|
return parentAnswer !== null && parentAnswer !== '';
|
|
}
|
|
|
|
return parentAnswer === question.depends_on_value;
|
|
};
|