Skip to content

Commit a50b8d4

Browse files
committed
feat(settings): Add notification settings functionality
1 parent 2ac95da commit a50b8d4

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

assets/core/ts/components/form.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ export const form = (config: FormControlConfig & { id?: string } = {}) => {
375375
const isCheckbox = type === 'checkbox';
376376
const isFile = type === 'file';
377377

378-
const defaultValue = this.values[name] ?? (isCheckbox ? false : '');
378+
const defaultValue = this.values[name] ?? (isCheckbox ? element?.checked ?? false : '');
379379

380380
this.fields[name] = {
381381
name,

assets/src/js/frontend/dashboard/pages/settings.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const settings = () => {
7777
saveBillingInfoMutation: null as MutationState<TutorMutationResponse<string>> | null,
7878
saveWithdrawMethodMutation: null as MutationState<TutorMutationResponse<string>> | null,
7979
resetPasswordMutation: null as MutationState<TutorMutationResponse<string>> | null,
80+
handleUpdateNotification: null as MutationState<unknown, unknown> | null,
8081

8182
init() {
8283
if (!this.$el) {
@@ -91,6 +92,15 @@ const settings = () => {
9192
this.handleSaveWithdrawMethod = this.handleSaveWithdrawMethod.bind(this);
9293
this.handleResetPassword = this.handleResetPassword.bind(this);
9394

95+
this.handleUpdateNotification = this.query.useMutation(this.updateNotification, {
96+
onSuccess: (data: TutorMutationResponse<string>) => {
97+
this.toast.success(data?.message ?? __('Successfully updated profile', 'tutor'));
98+
},
99+
onError: (error: Error) => {
100+
this.toast.error(convertToErrorMessage(error) || __('Failed to update profile', 'tutor'));
101+
},
102+
});
103+
94104
this.fetchCountriesQuery = this.query.useQuery('fetch-countries', () => this.fetchCountries());
95105

96106
this.uploadProfilePhotoMutation = this.query.useMutation(this.uploadProfilePhoto, {
@@ -151,6 +161,34 @@ const settings = () => {
151161
});
152162
},
153163

164+
async updateNotification(payload: Record<string, boolean>) {
165+
// Transform boolean values to "on"/"off" strings and restructure keys
166+
const transformedPayload = Object.keys(payload).reduce(
167+
(formattedPayload, key) => {
168+
const value = payload[key];
169+
const stringValue = typeof value === 'boolean' ? (value ? 'on' : 'off') : value;
170+
171+
// Check if key contains double underscore
172+
if (key.includes('__')) {
173+
const [firstPart, secondPart] = key.split('__');
174+
if (firstPart && secondPart) {
175+
const transformedKey = `tutor_notification_preference[email][${firstPart}][${secondPart}]`;
176+
formattedPayload[transformedKey] = stringValue;
177+
}
178+
} else {
179+
const transformedKey = `tutor_notification_preference[${key}]`;
180+
formattedPayload[transformedKey] = stringValue;
181+
}
182+
183+
return formattedPayload;
184+
},
185+
{} as Record<string, string>,
186+
);
187+
188+
console.log('noti', transformedPayload);
189+
return wpAjaxInstance.post(endpoints.UPDATE_PROFILE_NOTIFICATION, transformedPayload).then((res) => res.data);
190+
},
191+
154192
async fetchCountries() {
155193
return await axios.get(`${tutorConfig.tutor_url}${endpoints.FETCH_COUNTRIES}`).then((res) => res.data);
156194
},

assets/src/js/v3/shared/utils/endpoints.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ const endpoints = {
181181
SAVE_BILLING_INFO: 'tutor_save_billing_info',
182182
SAVE_WITHDRAW_METHOD: 'tutor_save_withdraw_account',
183183
RESET_PASSWORD: 'tutor_profile_password_reset',
184+
UPDATE_PROFILE_NOTIFICATION: 'tutor_save_notification_preference'
184185
} as const;
185186

186187
export default endpoints;

0 commit comments

Comments
 (0)