Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(next): adds support for resetting preferences #10304

Merged
merged 9 commits into from
Jan 3, 2025
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@import '../../../../scss/styles.scss';

@layer payload-default {
.reset-preferences-modal {
@include blur-bg;
display: flex;
align-items: center;
justify-content: center;
height: 100%;

&__wrapper {
z-index: 1;
position: relative;
display: flex;
flex-direction: column;
gap: base(2);
padding: base(2);
}

&__content {
display: flex;
flex-direction: column;
gap: base(1);

> * {
margin: 0;
}
}

&__controls {
display: flex;
gap: base(0.4);

.btn {
margin: 0;
akhrarovsaid marked this conversation as resolved.
Show resolved Hide resolved
margin-block: 0;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use client'
import { Button, Modal, useModal, useTranslation } from '@payloadcms/ui'

import './index.scss'

const baseClass = 'reset-preferences-modal'

export const ConfirmResetModal: React.FC<{
readonly onConfirm: () => void
readonly slug: string
}> = ({ slug, onConfirm }) => {
const { closeModal } = useModal()
const { t } = useTranslation()

const handleClose = () => closeModal(slug)

const handleConfirm = () => {
handleClose()
if (typeof onConfirm === 'function') {
onConfirm()
}
}

return (
<Modal className={baseClass} slug={slug}>
<div className={`${baseClass}__wrapper`}>
<div className={`${baseClass}__content`}>
<h1>{t('general:resetPreferences')}?</h1>
<p>{t('general:resetPreferencesDescription')}</p>
</div>
<div className={`${baseClass}__controls`}>
<Button buttonStyle="secondary" onClick={handleClose} size="large">
{t('general:cancel')}
</Button>
<Button onClick={handleConfirm} size="large">
{t('general:confirm')}
</Button>
</div>
</div>
</Modal>
)
}
74 changes: 74 additions & 0 deletions packages/next/src/views/Account/ResetPreferences/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use client'
import type { User } from 'payload'

import { Button, LoadingOverlay, toast, useModal, useTranslation } from '@payloadcms/ui'
import * as qs from 'qs-esm'
import { useCallback, useState } from 'react'

import { ConfirmResetModal } from './ConfirmResetModal/index.js'

const confirmResetModalSlug = 'confirm-reset-modal'

export const ResetPreferencesButton: React.FC<{
akhrarovsaid marked this conversation as resolved.
Show resolved Hide resolved
readonly apiRoute: string
readonly user?: User
}> = ({ apiRoute, user }) => {
const { openModal } = useModal()
const { t } = useTranslation()

const [loading, setLoading] = useState(false)

const handleResetPreferences = useCallback(async () => {
if (!user || loading) {
return
}
setLoading(true)

const stringifiedQuery = qs.stringify(
{
depth: 0,
where: {
user: {
id: {
equals: user.id,
},
},
},
},
{ addQueryPrefix: true },
)

try {
const res = await fetch(`${apiRoute}/payload-preferences${stringifiedQuery}`, {
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
method: 'DELETE',
})

const json = await res.json()
const message = json.message

if (res.ok) {
toast.success(message)
} else {
toast.error(message)
}
} catch (e) {
// swallow error
} finally {
setLoading(false)
}
}, [apiRoute, loading, user])

return (
<div>
akhrarovsaid marked this conversation as resolved.
Show resolved Hide resolved
<Button buttonStyle="secondary" onClick={() => openModal(confirmResetModalSlug)}>
{t('general:resetPreferences')}
</Button>
<ConfirmResetModal onConfirm={handleResetPreferences} slug={confirmResetModalSlug} />
{loading && <LoadingOverlay loadingText={t('general:resettingPreferences')} />}
</div>
)
}
13 changes: 10 additions & 3 deletions packages/next/src/views/Account/Settings/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { I18n } from '@payloadcms/translations'
import type { Config, LanguageOptions } from 'payload'
import type { BasePayload, Config, LanguageOptions, User } from 'payload'

import { FieldLabel } from '@payloadcms/ui'
import React from 'react'

import { ToggleTheme } from '../ToggleTheme/index.js'
import { ResetPreferencesButton } from '../ResetPreferences/index.js'
import './index.scss'
import { ToggleTheme } from '../ToggleTheme/index.js'
import { LanguageSelector } from './LanguageSelector.js'

const baseClass = 'payload-settings'
Expand All @@ -14,9 +15,14 @@ export const Settings: React.FC<{
readonly className?: string
readonly i18n: I18n
readonly languageOptions: LanguageOptions
readonly payload: BasePayload
readonly theme: Config['admin']['theme']
readonly user?: User
}> = (props) => {
const { className, i18n, languageOptions, theme } = props
const { className, i18n, languageOptions, payload, theme, user } = props

let apiRoute = payload.config?.routes?.api || '/api'
akhrarovsaid marked this conversation as resolved.
Show resolved Hide resolved
apiRoute = apiRoute.endsWith('/') ? apiRoute.slice(0, -1) : apiRoute
akhrarovsaid marked this conversation as resolved.
Show resolved Hide resolved

return (
<div className={[baseClass, className].filter(Boolean).join(' ')}>
Expand All @@ -26,6 +32,7 @@ export const Settings: React.FC<{
<LanguageSelector languageOptions={languageOptions} />
</div>
{theme === 'all' && <ToggleTheme />}
<ResetPreferencesButton apiRoute={apiRoute} user={user} />
</div>
)
}
10 changes: 9 additions & 1 deletion packages/next/src/views/Account/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ export const Account: React.FC<AdminViewProps> = async ({

return (
<DocumentInfoProvider
AfterFields={<Settings i18n={i18n} languageOptions={languageOptions} theme={theme} />}
AfterFields={
<Settings
i18n={i18n}
languageOptions={languageOptions}
payload={payload}
theme={theme}
user={user}
/>
}
apiURL={`${serverURL}${api}/${userSlug}${user?.id ? `/${user.id}` : ''}`}
collectionSlug={userSlug}
currentEditor={currentEditor}
Expand Down
3 changes: 3 additions & 0 deletions packages/translations/src/clientKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ export const clientTranslationKeys = createClientTranslationKeys([
'general:reindexingAll',
'general:remove',
'general:reset',
'general:resetPreferences',
'general:resetPreferencesDescription',
'general:resettingPreferences',
'general:row',
'general:rows',
'general:save',
Expand Down
4 changes: 4 additions & 0 deletions packages/translations/src/languages/ar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ export const arTranslations: DefaultTranslationsObject = {
reindexingAll: 'جاري إعادة فهرسة جميع {{collections}}.',
remove: 'إزالة',
reset: 'إعادة تعيين',
resetPreferences: 'إعادة تعيين التفضيلات',
resetPreferencesDescription:
'سيؤدي ذلك إلى إعادة تعيين جميع تفضيلاتك إلى الإعدادات الافتراضية.',
resettingPreferences: 'إعادة تعيين التفضيلات.',
row: 'سطر',
rows: 'أسطُر',
save: 'حفظ',
Expand Down
3 changes: 3 additions & 0 deletions packages/translations/src/languages/az.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ export const azTranslations: DefaultTranslationsObject = {
reindexingAll: 'Bütün {{collections}} yenidən indekslənir.',
remove: 'Sil',
reset: 'Yenidən başlat',
resetPreferences: 'Təhlükəsizlik parametrlərini sıfırlamaq',
resetPreferencesDescription: 'Bu, bütün parametrlərinizi standart vəziyyətlərinə sıfırlayacaq.',
resettingPreferences: 'Təhlükəsizlik parametrləri sıfırlanır.',
row: 'Sətir',
rows: 'Sətirlər',
save: 'Saxla',
Expand Down
4 changes: 4 additions & 0 deletions packages/translations/src/languages/bg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ export const bgTranslations: DefaultTranslationsObject = {
reindexingAll: 'Преиндексиране на всички {{collections}}.',
remove: 'Премахни',
reset: 'Нулиране',
resetPreferences: 'Нулиране на предпочитанията',
resetPreferencesDescription:
'Това ще нулира всички ваши предпочитания до техните настройки по подразбиране.',
resettingPreferences: 'Нулиране на предпочитанията.',
row: 'ред',
rows: 'Редове',
save: 'Запази',
Expand Down
4 changes: 4 additions & 0 deletions packages/translations/src/languages/ca.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ export const caTranslations: DefaultTranslationsObject = {
reindexingAll: 'Reindexa tots el {{collections}}.',
remove: 'Elimina',
reset: 'Restableix',
resetPreferences: 'Restablir les preferències',
resetPreferencesDescription:
'Això restablirà totes les teves preferències a les configuracions per defecte.',
resettingPreferences: 'Restablint les preferències.',
row: 'Fila',
rows: 'Files',
save: 'Desa',
Expand Down
3 changes: 3 additions & 0 deletions packages/translations/src/languages/cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ export const csTranslations: DefaultTranslationsObject = {
reindexingAll: 'Přeindexování všech {{collections}}.',
remove: 'Odstranit',
reset: 'Resetovat',
resetPreferences: 'Obnovit nastavení',
resetPreferencesDescription: 'Toto obnoví všechna vaše nastavení na výchozí hodnoty.',
resettingPreferences: 'Obnovování nastavení.',
row: 'Řádek',
rows: 'Řádky',
save: 'Uložit',
Expand Down
4 changes: 4 additions & 0 deletions packages/translations/src/languages/da.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ export const daTranslations: DefaultTranslationsObject = {
reindexingAll: 'Genindekserer alle {{collections}}.',
remove: 'Fjern',
reset: 'Nulstil',
resetPreferences: 'Nulstil præferencer',
resetPreferencesDescription:
'Dette vil nulstille alle dine præferencer til standardindstillingerne.',
resettingPreferences: 'Nulstiller præferencer.',
row: 'Række',
rows: 'Rækker',
save: 'Gem',
Expand Down
3 changes: 3 additions & 0 deletions packages/translations/src/languages/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ export const deTranslations: DefaultTranslationsObject = {
reindexingAll: 'Alle {{collections}} werden neu indiziert.',
remove: 'Entfernen',
reset: 'Zurücksetzen',
resetPreferences: 'Präferenzen zurücksetzen',
resetPreferencesDescription: 'Dies setzt alle Ihre Präferenzen auf die Standardwerte zurück.',
resettingPreferences: 'Präferenzen werden zurückgesetzt.',
row: 'Zeile',
rows: 'Zeilen',
save: 'Speichern',
Expand Down
4 changes: 4 additions & 0 deletions packages/translations/src/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ export const enTranslations = {
reindexingAll: 'Reindexing all {{collections}}.',
remove: 'Remove',
reset: 'Reset',
resetPreferences: 'Reset Preferences',
resetPreferencesDescription:
'This will reset all of your preferences to their default settings.',
resettingPreferences: 'Resetting Preferences.',
row: 'Row',
rows: 'Rows',
save: 'Save',
Expand Down
4 changes: 4 additions & 0 deletions packages/translations/src/languages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ export const esTranslations: DefaultTranslationsObject = {
reindexingAll: 'Reindexando todas las {{collections}}.',
remove: 'Remover',
reset: 'Reiniciar',
resetPreferences: 'Restablecer preferencias',
resetPreferencesDescription:
'Esto restablecerá todas tus preferencias a la configuración predeterminada.',
resettingPreferences: 'Restableciendo preferencias.',
row: 'Fila',
rows: 'Filas',
save: 'Guardar',
Expand Down
3 changes: 3 additions & 0 deletions packages/translations/src/languages/et.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ export const etTranslations = {
reindexingAll: 'Indekseerin uuesti kõik {{collections}}.',
remove: 'Eemalda',
reset: 'Lähtesta',
resetPreferences: 'Lähtesta eelistused',
resetPreferencesDescription: 'See lähtestab kõik teie eelistused vaikeväärtustele.',
resettingPreferences: 'Lähtestan eelistusi.',
row: 'Rida',
rows: 'Read',
save: 'Salvesta',
Expand Down
3 changes: 3 additions & 0 deletions packages/translations/src/languages/fa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ export const faTranslations: DefaultTranslationsObject = {
reindexingAll: 'در حال بازایندکس همه {{collections}}.',
remove: 'برداشتن',
reset: 'بازنشانی',
resetPreferences: 'بازنشانی تنظیمات',
resetPreferencesDescription: 'این تمام تنظیمات شما را به تنظیمات پیش‌فرض بازنشانی خواهد کرد.',
resettingPreferences: 'در حال بازنشانی تنظیمات.',
row: 'ردیف',
rows: 'ردیف‌ها',
save: 'ذخیره',
Expand Down
4 changes: 4 additions & 0 deletions packages/translations/src/languages/fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ export const frTranslations: DefaultTranslationsObject = {
reindexingAll: 'Réindexation de toutes les {{collections}}.',
remove: 'Retirer',
reset: 'Réinitialiser',
resetPreferences: 'Réinitialiser les préférences',
resetPreferencesDescription:
'Cela réinitialisera toutes vos préférences aux paramètres par défaut.',
resettingPreferences: 'Réinitialisation des préférences.',
row: 'Ligne',
rows: 'Lignes',
save: 'Sauvegarder',
Expand Down
3 changes: 3 additions & 0 deletions packages/translations/src/languages/he.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ export const heTranslations: DefaultTranslationsObject = {
reindexingAll: 'החזרת אינדקס לכל {{collections}}.',
remove: 'הסר',
reset: 'איפוס',
resetPreferences: 'איפוס העדפות',
resetPreferencesDescription: 'זאת תאפס את כל ההעדפות שלך להגדרות ברירת המחדל.',
resettingPreferences: 'מאפס העדפות.',
row: 'שורה',
rows: 'שורות',
save: 'שמירה',
Expand Down
3 changes: 3 additions & 0 deletions packages/translations/src/languages/hr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ export const hrTranslations: DefaultTranslationsObject = {
reindexingAll: 'Ponovno indeksiranje svih {{collections}}.',
remove: 'Ukloni',
reset: 'Ponovno postavi',
resetPreferences: 'Ponovno postavljanje postavki',
resetPreferencesDescription: 'Ovo će vratiti sve vaše postavke na zadane vrijednosti.',
resettingPreferences: 'Ponovno postavljanje postavki.',
row: 'Red',
rows: 'Redovi',
save: 'Spremi',
Expand Down
4 changes: 4 additions & 0 deletions packages/translations/src/languages/hu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ export const huTranslations: DefaultTranslationsObject = {
reindexingAll: 'Az összes {{collections}} újraindexálása folyamatban.',
remove: 'Törlés',
reset: 'Visszaállítás',
resetPreferences: 'Beállítások visszaállítása',
resetPreferencesDescription:
'Ez visszaállítja az összes beállítást az alapértelmezett értékekre.',
resettingPreferences: 'Beállítások visszaállítása.',
row: 'Sor',
rows: 'Sorok',
save: 'Mentés',
Expand Down
4 changes: 4 additions & 0 deletions packages/translations/src/languages/it.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ export const itTranslations: DefaultTranslationsObject = {
reindexingAll: "Rifacendo l'indice di tutte le {{collections}}.",
remove: 'Rimuovi',
reset: 'Ripristina',
resetPreferences: 'Ripristina le preferenze',
resetPreferencesDescription:
'Questo ripristinerà tutte le tue preferenze alle impostazioni predefinite.',
resettingPreferences: 'Ripristinando le preferenze.',
row: 'Riga',
rows: 'Righe',
save: 'Salva',
Expand Down
Loading
Loading