Frontend

For the frontend, Vite (React) was used along with TypeScript. We use Redux Toolkitarrow-up-right to manage the state with the Redux Persistorarrow-up-right to persist store. We also use React Routerarrow-up-right.

I use redux toolkit createAsyncThunkarrow-up-right to create callback function which allows you to manage data in communication with the API and the store. This allows you to create queries efficiently. I use the Axiosarrow-up-right library to create HTTP request.

const { data, status } = await axios.post(
  `${apiUrl}/users/${userId}/projects`,
  {
    name: name.trim(),
  },
  {
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${jwt}`,
    },
  },
);

I am using JSON Web Tokensarrow-up-right for authentication in my project. For this purpose, when querying the protected endpoint, I add the header: Bearer ${jwt} .

export const fetchAllUserTasks = createAsyncThunk(
  "tasks/fetchAllUserTasks",
  async (projectName: string, { getState, rejectWithValue }) => {
    const { user, auth } = getState() as RootState;
    const userId = user.userId;
    const token = auth.token;

    if (!userId || !token) {
      return rejectWithValue("Please login first");
    }

    try {
      const response = await axios.get(
        `${apiUrl}/users/${userId}/projects/${projectName}/tasks`,
        {
          headers: {
            Authorization: `Bearer ${token}`,
          },
        },
      );

      if (response.status !== 200) {
        throw new Error("Something went wrong!");
      }

      return await response.data;
    } catch (err) {
      if (err instanceof Error) {
        toast.error(err.message);
        return rejectWithValue(err.message);
      }
      return rejectWithValue("Something went wrong!");
    }
  },
);

Last updated