-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Send only changed data * Add tests * Create beige-moose-greet.md --------- Co-authored-by: Patryk Andrzejewski <[email protected]> Co-authored-by: Paweł Chyła <[email protected]>
- Loading branch information
1 parent
1b7126b
commit 6b2892c
Showing
12 changed files
with
211 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
Product edition no longer change the others work when changing different fields simultaneously. This means UI sends only form fields that were changed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type FormData = Record<string, any | any[]>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
|
||
import { useChangedData } from "./useChangedData"; | ||
|
||
describe("useForm / useChangedData", () => { | ||
it("returns all changed fields", () => { | ||
// Arrange | ||
const { result } = renderHook(() => | ||
useChangedData({ | ||
"field-1": "value-1", | ||
"field-2": "value-2", | ||
"field-3": "value-3", | ||
"field-4": "value-4", | ||
}), | ||
); | ||
|
||
// Act | ||
result.current.add("field-1"); | ||
result.current.add("field-2"); | ||
|
||
// Assert | ||
expect(result.current.data).toEqual({ | ||
"field-1": "value-1", | ||
"field-2": "value-2", | ||
}); | ||
}); | ||
|
||
it("clears changed fields", () => { | ||
// Arrange | ||
const { result } = renderHook(() => | ||
useChangedData({ | ||
"field-1": "value-1", | ||
"field-2": "value-2", | ||
"field-3": "value-3", | ||
"field-4": "value-4", | ||
}), | ||
); | ||
|
||
// Act | ||
result.current.add("field-1"); | ||
result.current.clean(); | ||
|
||
// Assert | ||
expect(result.current.data).toEqual({}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useState } from "react"; | ||
|
||
import { FormData } from "./types"; | ||
|
||
export const useChangedData = <T extends FormData>(formData: T) => { | ||
const [dirtyFields, setDirtyFields] = useState<string[]>([]); | ||
|
||
const add = (name: string) => { | ||
setDirtyFields(fields => { | ||
return Array.from(new Set(fields.concat(name))); | ||
}); | ||
}; | ||
|
||
const clean = () => { | ||
setDirtyFields([]); | ||
}; | ||
|
||
const data = Object.entries(formData) | ||
.filter(([key]) => dirtyFields.includes(key)) | ||
.reduce((p, [key, value]) => ({ ...p, [key]: value }), {} as T); | ||
|
||
return { | ||
add, | ||
clean, | ||
data, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.