Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a60e4de

Browse files
author
Rankvet-Developer
committedSep 11, 2022
added the tag feature so that now it's totally sync with server and that's a cool feature I have ever seen
1 parent 2c242fe commit a60e4de

File tree

2 files changed

+61
-42
lines changed

2 files changed

+61
-42
lines changed
 

‎src/components/Footer.js

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,82 @@ const numberOfTodos = (no_of_todos) => {
1515
export default function Footer() {
1616

1717
const [status,setStatus] = useState("All");
18+
const [colors,setColors] = useState([]);
1819

19-
const {data:todos,isSuccess} = useGetTodosQuery(status);
20+
const {data:todos,isSuccess} = useGetTodosQuery({status: status,colors: colors});
2021

2122
// const filters = useSelector((state) => state.filters);
2223

2324
const todosRemaining = isSuccess && todos.filter((todo) => !todo.completed).length;
2425
// const { status, colors } = filters;
2526

2627
const handleStatusChange = (statusName) => {
28+
console.log(statusName);
2729
setStatus(statusName);
2830
};
2931

30-
// const handleColorChange = (color) => {
31-
// if (colors.includes(color)) {
32-
// dispatch(colorChanged(color, "removed"));
33-
// } else {
34-
// dispatch(colorChanged(color, "added"));
35-
// }
36-
// };
32+
const handleColorChange = (color) => {
33+
if (colors.includes(color)) {
34+
// remove color
35+
setColors(() => {
36+
return [...colors].filter(existingColor => existingColor !== color)
37+
})
38+
} else {
39+
// add color...
40+
const newColors = [...colors,color];
41+
setColors(newColors);
42+
}
43+
};
3744

3845
return (
3946
<div className="mt-4 flex justify-between text-xs text-gray-500">
4047
<p>{numberOfTodos(todosRemaining)} left</p>
4148
<ul className="flex space-x-1 items-center text-xs">
4249
<li
43-
// className={`cursor-pointer ${
44-
// status === "All" && "font-bold"
45-
// }`}
50+
className={`cursor-pointer ${
51+
status === "All" && "font-bold"
52+
}`}
4653
onClick={() => handleStatusChange("All")}
4754
>
4855
All
4956
</li>
5057
<li>|</li>
5158
<li
52-
// className={`cursor-pointer ${
53-
// status === "Incomplete" && "font-bold"
54-
// }`}
59+
className={`cursor-pointer ${
60+
status === "Incomplete" && "font-bold"
61+
}`}
5562
onClick={() => handleStatusChange("Incomplete")}
5663
>
5764
Incomplete
5865
</li>
5966
<li>|</li>
6067
<li
61-
// className={`cursor-pointer ${
62-
// status === "Complete" && "font-bold"
63-
// }`}
68+
className={`cursor-pointer ${
69+
status === "Complete" && "font-bold"
70+
}`}
6471
onClick={() => handleStatusChange("Complete")}
6572
>
6673
Complete
6774
</li>
6875
<li></li>
6976
<li></li>
7077
<li
71-
// className={`h-3 w-3 border-2 border-green-500 md:hover:bg-green-500 rounded-full cursor-pointer ${
72-
// colors.includes("green") && "bg-green-500"
73-
// }`}
74-
// onClick={() => handleColorChange("green")}
78+
className={`h-3 w-3 border-2 border-green-500 md:hover:bg-green-500 rounded-full cursor-pointer ${
79+
colors.includes("green") && "bg-green-500"
80+
}`}
81+
onClick={() => handleColorChange("green")}
7582
></li>
7683
<li
77-
// className={`h-3 w-3 border-2 border-red-500 md:hover:bg-red-500 rounded-full cursor-pointer ${
78-
// colors.includes("red") && "bg-red-500"
79-
// }`}
80-
// onClick={() => handleColorChange("red")}
84+
className={`h-3 w-3 border-2 border-red-500 md:hover:bg-red-500 rounded-full cursor-pointer ${
85+
colors.includes("red") && "bg-red-500"
86+
}`}
87+
onClick={() => handleColorChange("red")}
8188
></li>
8289
<li
83-
// className={`h-3 w-3 border-2 border-yellow-500 md:hover:bg-yellow-500 rounded-full cursor-pointer ${
84-
// colors.includes("yellow") && "bg-yellow-500"
85-
// }`}
86-
// onClick={() => handleColorChange("yellow")}
90+
className={`h-3 w-3 border-2 border-yellow-500 md:hover:bg-yellow-500 rounded-full cursor-pointer ${
91+
colors.includes("yellow") && "bg-yellow-500"
92+
}`}
93+
onClick={() => handleColorChange("yellow")}
8794
></li>
8895
</ul>
8996
</div>

‎src/features/api/apiSlice.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,37 @@ export const apiSlice = createApi({
55
baseQuery: fetchBaseQuery({
66
baseUrl: "http://localhost:8000"
77
}),
8+
tagTypes: ["Todos"],
89
endpoints: (builder) => ({
910
getTodos: builder.query({
10-
query: (status="") => {
11+
query: (data) => {
1112
let queryString = "";
13+
if(data){
14+
const {status,colors} = data;
1215

13-
if(status !== ""){
14-
if(status === "All"){
15-
queryString = "";
16+
if(status !== ""){
17+
if(status === "All"){
18+
queryString = "";
19+
}
20+
if(status === "Incomplete"){
21+
queryString = "completed=false&";
22+
}
23+
if(status === "Complete"){
24+
queryString = "completed=false&";
25+
}
1626
}
17-
if(status === "Incomplete"){
18-
queryString = "completed=false";
19-
}
20-
if(status === "Complete"){
21-
queryString = "completed=false";
27+
28+
if(colors.length > 0){
29+
queryString += colors.map(color => `color=${color}`).join("&");
2230
}
2331
}
2432

2533
return {
2634
url: `/todos?${queryString}`,
2735
method: 'get'
2836
}
29-
}
37+
},
38+
providesTags: ["Todos"]
3039
}),
3140
addTodo: builder.mutation({
3241
query: (data) => ({
@@ -36,7 +45,8 @@ export const apiSlice = createApi({
3645
text: data,
3746
completed: false,
3847
}
39-
})
48+
}),
49+
invalidatesTags: ["Todos"]
4050
}),
4151
updateTodo: builder.mutation({
4252
query: ({id,data}) => ({
@@ -49,7 +59,8 @@ export const apiSlice = createApi({
4959
query: (id) => ({
5060
url: `/todos/${id}`,
5161
method: "DELETE"
52-
})
62+
}),
63+
invalidatesTags: ["Todos"]
5364
}),
5465
toogleTodo: builder.mutation({
5566
query:({id,toogleValue}) => ({
@@ -58,7 +69,8 @@ export const apiSlice = createApi({
5869
body:{
5970
completed: !toogleValue
6071
}
61-
})
72+
}),
73+
invalidatesTags: ["Todos"]
6274
}),
6375
updateTodoColor: builder.mutation({
6476
query: ({id,color}) => ({

0 commit comments

Comments
 (0)
Please sign in to comment.