mirror of
https://github.com/lemeow125/StudE-Frontend.git
synced 2024-11-17 06:19:25 +08:00
Allow editing of user info
This commit is contained in:
parent
f24b84139b
commit
8de8e67070
3 changed files with 80 additions and 32 deletions
|
@ -4,7 +4,9 @@ import {
|
||||||
ActivationParams,
|
ActivationParams,
|
||||||
LoginParams,
|
LoginParams,
|
||||||
OnboardingParams,
|
OnboardingParams,
|
||||||
|
PatchStudentData,
|
||||||
RegistrationParams,
|
RegistrationParams,
|
||||||
|
StudentData,
|
||||||
} from "../../interfaces/Interfaces";
|
} from "../../interfaces/Interfaces";
|
||||||
|
|
||||||
let debug = true;
|
let debug = true;
|
||||||
|
@ -43,6 +45,16 @@ export async function setRefreshToken(refresh: string) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Header Config Template for REST
|
||||||
|
export async function GetConfig() {
|
||||||
|
const accessToken = await getAccessToken();
|
||||||
|
return {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${accessToken}`,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// User APIs
|
// User APIs
|
||||||
export function UserRegister(register: RegistrationParams) {
|
export function UserRegister(register: RegistrationParams) {
|
||||||
console.log(JSON.stringify(register));
|
console.log(JSON.stringify(register));
|
||||||
|
@ -105,13 +117,9 @@ export async function TokenRefresh() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
export async function UserInfo() {
|
export async function UserInfo() {
|
||||||
const accessToken = await getAccessToken();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.get("/api/v1/accounts/users/me/", {
|
.get("/api/v1/accounts/users/me/", config)
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${accessToken}`,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
// console.log(JSON.stringify(response.data));
|
// console.log(JSON.stringify(response.data));
|
||||||
return [true, response.data];
|
return [true, response.data];
|
||||||
|
@ -124,6 +132,23 @@ export async function UserInfo() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function PatchUserInfo(info: PatchStudentData) {
|
||||||
|
const config = await GetConfig();
|
||||||
|
return instance
|
||||||
|
.patch("/api/v1/accounts/users/me/", info, config)
|
||||||
|
.then((response) => {
|
||||||
|
console.log(JSON.stringify(response.data));
|
||||||
|
return [true, response.data];
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
let error_message = "";
|
||||||
|
if (error.response) error_message = error.response.data;
|
||||||
|
else error_message = "Unable to reach servers";
|
||||||
|
console.log(error_message);
|
||||||
|
return [false, error_message];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function UserActivate(activation: ActivationParams) {
|
export function UserActivate(activation: ActivationParams) {
|
||||||
return instance
|
return instance
|
||||||
.post("/api/v1/accounts/users/activation/", activation)
|
.post("/api/v1/accounts/users/activation/", activation)
|
||||||
|
@ -178,13 +203,9 @@ export async function GetSemesters() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GetYearLevels() {
|
export async function GetYearLevels() {
|
||||||
const accessToken = await getAccessToken();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.get("/api/v1/year_levels/", {
|
.get("/api/v1/year_levels/", config)
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${accessToken}`,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
// console.log(JSON.stringify(response.data));
|
// console.log(JSON.stringify(response.data));
|
||||||
return [true, response.data];
|
return [true, response.data];
|
||||||
|
@ -198,12 +219,9 @@ export async function GetYearLevels() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function OnboardingUpdateStudentInfo(info: OnboardingParams) {
|
export async function OnboardingUpdateStudentInfo(info: OnboardingParams) {
|
||||||
const accessToken = await getAccessToken();
|
const config = await GetConfig();
|
||||||
const headers = {
|
|
||||||
Authorization: `Bearer ${accessToken}`,
|
|
||||||
};
|
|
||||||
return instance
|
return instance
|
||||||
.patch("/api/v1/accounts/users/me/", info, { headers })
|
.patch("/api/v1/accounts/users/me/", info, config)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
console.log(JSON.stringify(response.data));
|
console.log(JSON.stringify(response.data));
|
||||||
return [true, response.data];
|
return [true, response.data];
|
||||||
|
|
|
@ -77,6 +77,15 @@ export interface OnboardingParams {
|
||||||
semester: string;
|
semester: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface PatchStudentData {
|
||||||
|
course?: string | null;
|
||||||
|
first_name?: string | null;
|
||||||
|
last_name?: string | null;
|
||||||
|
semester?: string | null;
|
||||||
|
subjects?: any[] | null; // To-do, replace 'any' with your actual type
|
||||||
|
year_level?: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
export interface StudentData {
|
export interface StudentData {
|
||||||
avatar: string;
|
avatar: string;
|
||||||
course: string;
|
course: string;
|
||||||
|
@ -87,15 +96,6 @@ export interface StudentData {
|
||||||
semester: string;
|
semester: string;
|
||||||
student_id_number: string;
|
student_id_number: string;
|
||||||
subjects: any[]; // To-do
|
subjects: any[]; // To-do
|
||||||
user_status: {
|
|
||||||
active: boolean;
|
|
||||||
landmark: string;
|
|
||||||
location: any; // To-do
|
|
||||||
study_group: any[]; // To-do
|
|
||||||
subject: string;
|
|
||||||
timestamp: string;
|
|
||||||
user: string;
|
|
||||||
};
|
|
||||||
username: string;
|
username: string;
|
||||||
year_level: string;
|
year_level: string;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,11 +18,15 @@ import { TouchableOpacity, Image } from "react-native";
|
||||||
import { ScrollView } from "react-native-gesture-handler";
|
import { ScrollView } from "react-native-gesture-handler";
|
||||||
import SelectDropdown from "react-native-select-dropdown";
|
import SelectDropdown from "react-native-select-dropdown";
|
||||||
import DropdownIcon from "../../icons/DropdownIcon/DropdownIcon";
|
import DropdownIcon from "../../icons/DropdownIcon/DropdownIcon";
|
||||||
import { useQuery } from "react-query";
|
import { useMutation, useQuery, useQueryClient } from "react-query";
|
||||||
import { UserInfo as GetUserInfo } from "../../components/Api/Api";
|
import {
|
||||||
|
UserInfo as GetUserInfo,
|
||||||
|
PatchUserInfo,
|
||||||
|
} from "../../components/Api/Api";
|
||||||
import { colors } from "../../styles";
|
import { colors } from "../../styles";
|
||||||
|
|
||||||
export default function UserInfo() {
|
export default function UserInfo() {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
const UserInfo = useQuery("user", GetUserInfo, {
|
const UserInfo = useQuery("user", GetUserInfo, {
|
||||||
onSuccess: (data: UserInfoParams) => {
|
onSuccess: (data: UserInfoParams) => {
|
||||||
setUser({
|
setUser({
|
||||||
|
@ -34,10 +38,18 @@ export default function UserInfo() {
|
||||||
course: data[1].course,
|
course: data[1].course,
|
||||||
avatar: data[1].avatar,
|
avatar: data[1].avatar,
|
||||||
});
|
});
|
||||||
|
setDisplayName({
|
||||||
|
first_name: data[1].first_name,
|
||||||
|
last_name: data[1].last_name,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const mutation = useMutation({
|
||||||
|
mutationFn: PatchUserInfo,
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries("user");
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const navigation = useNavigation<RootDrawerParamList>();
|
|
||||||
const [isEditable, setIsEditable] = useState(false);
|
const [isEditable, setIsEditable] = useState(false);
|
||||||
const subjectOptions = ["", "", "", ""];
|
const subjectOptions = ["", "", "", ""];
|
||||||
const [user, setUser] = useState({
|
const [user, setUser] = useState({
|
||||||
|
@ -48,6 +60,10 @@ export default function UserInfo() {
|
||||||
course: "",
|
course: "",
|
||||||
avatar: "",
|
avatar: "",
|
||||||
});
|
});
|
||||||
|
const [displayName, setDisplayName] = useState({
|
||||||
|
first_name: "",
|
||||||
|
last_name: "",
|
||||||
|
});
|
||||||
function Avatar() {
|
function Avatar() {
|
||||||
if (user.avatar) {
|
if (user.avatar) {
|
||||||
return <Image source={{ uri: user.avatar }} style={styles.profile} />;
|
return <Image source={{ uri: user.avatar }} style={styles.profile} />;
|
||||||
|
@ -64,7 +80,9 @@ export default function UserInfo() {
|
||||||
<ScrollView style={styles.background}>
|
<ScrollView style={styles.background}>
|
||||||
<AnimatedContainer>
|
<AnimatedContainer>
|
||||||
<Text style={styles.text_white_medium_large}>
|
<Text style={styles.text_white_medium_large}>
|
||||||
{(user.first_name || "Undefined") + " " + (user.last_name || "User")}
|
{(displayName.first_name || "Undefined") +
|
||||||
|
" " +
|
||||||
|
(displayName.last_name || "User")}
|
||||||
</Text>
|
</Text>
|
||||||
<View>
|
<View>
|
||||||
<Avatar />
|
<Avatar />
|
||||||
|
@ -185,7 +203,19 @@ export default function UserInfo() {
|
||||||
</View>
|
</View>
|
||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
style={styles.button_template}
|
style={styles.button_template}
|
||||||
onPress={() => setIsEditable(!isEditable)}
|
onPress={() => {
|
||||||
|
if (isEditable) {
|
||||||
|
mutation.mutate({
|
||||||
|
first_name: user.first_name,
|
||||||
|
last_name: user.last_name,
|
||||||
|
course: user.course,
|
||||||
|
semester: user.semester,
|
||||||
|
subjects: [],
|
||||||
|
year_level: user.year_level,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
setIsEditable(!isEditable);
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<Text style={styles.text_white_small}>
|
<Text style={styles.text_white_small}>
|
||||||
{isEditable && UserInfo.isSuccess ? "Save" : "Edit Profile"}
|
{isEditable && UserInfo.isSuccess ? "Save" : "Edit Profile"}
|
||||||
|
|
Loading…
Reference in a new issue