Save username upon change
This commit is contained in:
parent
e0b48ebe40
commit
1afea69854
4 changed files with 38 additions and 1 deletions
28
src/api.ts
28
src/api.ts
|
@ -52,6 +52,28 @@ class Model {
|
||||||
throw new APIError(response.statusText, dataOrErrors);
|
throw new APIError(response.statusText, dataOrErrors);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected async put(endpoint: string, token: string, data: any) {
|
||||||
|
const init = {
|
||||||
|
headers: new Headers(),
|
||||||
|
method: "PATCH",
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
};
|
||||||
|
const csrfToken = Cookies.get("csrftoken");
|
||||||
|
if (csrfToken != undefined) {
|
||||||
|
init.headers.set("X-CSRFToken", csrfToken);
|
||||||
|
}
|
||||||
|
init.headers.set("Authorization", `Token ${token}`);
|
||||||
|
init.headers.set("Accept", "application/json");
|
||||||
|
init.headers.set("Content-Type", "application/json");
|
||||||
|
const response = await fetch(`/api/${endpoint}/`, init);
|
||||||
|
const dataOrErrors = await response.json();
|
||||||
|
if (response.status === 200) {
|
||||||
|
return dataOrErrors;
|
||||||
|
} else {
|
||||||
|
throw new APIError(response.statusText, dataOrErrors);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface UserData {
|
interface UserData {
|
||||||
|
@ -87,6 +109,12 @@ export class User extends Model implements UserData {
|
||||||
this.isAuthenticated = true;
|
this.isAuthenticated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async save(): Promise<void> {
|
||||||
|
await super.put("users/me", this.token, {
|
||||||
|
username: this.username,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async signup(): Promise<void> {
|
async signup(): Promise<void> {
|
||||||
await Model.create("users", { email: this.email, password: this.password });
|
await Model.create("users", { email: this.email, password: this.password });
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Benutzername</td>
|
<td>Benutzername</td>
|
||||||
<td><InlineEditor v-model="user.username" /></td>
|
<td><InlineEditor v-model="user.username" @input="user.save()" /></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Vertrauensperson</td>
|
<td>Vertrauensperson</td>
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
from djoser.serializers import UserSerializer as BaseUserSerializer
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from userausfall.models import AccountRequest
|
from userausfall.models import AccountRequest
|
||||||
|
@ -7,3 +8,8 @@ class AccountRequestSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = AccountRequest
|
model = AccountRequest
|
||||||
fields = ('url', 'email', 'confidant_email', 'username', 'is_verified', 'is_trustable')
|
fields = ('url', 'email', 'confidant_email', 'username', 'is_verified', 'is_trustable')
|
||||||
|
|
||||||
|
|
||||||
|
class UserSerializer(BaseUserSerializer):
|
||||||
|
class Meta(BaseUserSerializer.Meta):
|
||||||
|
fields = ('email', 'username')
|
||||||
|
|
|
@ -143,6 +143,9 @@ MEDIA_URL = '/media/'
|
||||||
DJOSER = {
|
DJOSER = {
|
||||||
"ACTIVATION_URL": "confirm/{uid}/{token}",
|
"ACTIVATION_URL": "confirm/{uid}/{token}",
|
||||||
"SEND_ACTIVATION_EMAIL": True,
|
"SEND_ACTIVATION_EMAIL": True,
|
||||||
|
"SERIALIZERS": {
|
||||||
|
"current_user": "userausfall.rest_api.serializers.UserSerializer",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in a new issue